- Environnement:
- Wildfly 10
- Java JDK 8.xx
- Postgresql 9.4
- Download Postgresql driver. See: https://jdbc.postgresql.org/download.html
- Run ./jboss-cli.sh from wildfly/bin
- From jboss-cli command-line:
connect #add module module add --name=org.postgres --resources=/tmp/postgresql-9.4.1207.jar --dependencies=javax.api,javax.transaction.api # add driver to subsystem datasources /subsystem=datasources/jdbc-driver=postgres:add(driver-name="postgres",driver-module-name="org.postgres",driver-class-name=org.postgresql.Driver) # declare datasource: here: server is on localhost, and db is postgres_db # username is postgres and password is posgres data-source add --jndi-name=java:jboss/datasources/PostGreDS --name=PostgrePool --connection-url=jdbc:postgresql://localhost/postgres_db --driver-name=postgres --user-name=postgres --password=postgres
PostgreSQL
Getting Started: EJB/Persistence (with PostgreSQL)
[DRAFT… To be continued …] last update: Feb 4th
I’ve been using PHP (especially Symfony 2.x) for quite a while in my web projects (both backend and frontend), coupled with MongoDB.
Almost a year ago, I’ve discovered that PostGreSQL supports “JSON/JSONB” and handles “requests” over Json/JsonB. Couple of months ago, I had a project involving OptaPlanner, so I had to move to J2EE world, and it not so worst.
A month ago, I started a project (on my free time) from scratch to deeply evaluate pros/cons of EJB/JPA/… with Wildfly 9.x.
So here, I will not discuss pros/cons neither compare it with PHP Symphony, Python Django, or even Ruby on Rails; just give couple of Tips/Tricks around “Getting Started”
- Install : PostgreSQL & Wildfly
– For my PoC/testing (Wildfly+Postgresql) server, I use a VPS (see OVH VPS for example: 1 Go RAM, 128Mo SWAP, 10Go HD)
– Install Java on your server
– Install PostgreSQL (9.4 or above) and starts it
– Download Wildfly, uncompress, and runs it (here in standalone mode, listening all interfaces).bin/standalone.sh -b=0.0.0.0
- Configure WildFly DataSource
This is a critical path: configure WildFly datasources. By default, an “ExampleDS”, using H2 memory database, is configured.
Have a look over here (quick tutorial).
There are plenty of ways to configured a PostgreSQL datasource using WildFly Web Administration, command-line client or simply “standalone.xml” edit. - Setup a maven project
In fact, this is where I’ve spent a lot of times: get a predefined maven project, from which I could start my tests/devs.
Here is my pom.xml - 1st tip : a generic “Reporitory” class
As I did in a previous post regarding Symfony and a kind of “super” controller, I quickly had the same approach with Persistence/EJB. I created a top repository class to quickly handle simple stuff with @Entity.
Here is my (old) very first but efficient AARootRepo.java - 2nd tip/reminder: Hibernate key events/annotations
Type Description @PrePersist Executed before the entity manager persist operation is actually executed or cascaded. This call is synchronous with the persist operation. @PreRemove Executed before the entity manager remove operation is actually executed or cascaded. This call is synchronous with the remove operation. @PostPersist Executed after the entity manager persist operation is actually executed or cascaded. This call is invoked after the database INSERT is executed. @PostRemove Executed after the entity manager remove operation is actually executed or cascaded. This call is synchronous with the remove operation. @PreUpdate Executed before the database UPDATE operation. @PostUpdate Executed after the database UPDATE operation. @PostLoad Executed after an entity has been loaded into the current persistence context or an entity has been refreshed. These events are quite useful to handle “json” data embedded into a blob/text field (through PostLoad/PreUpdate/PrePersist) for example, or to automatically update a field on persist/update (PrePersist/PreUpdate).
WildFly/JBoss / PostgreSQL : Draft
First steps of tutorial (synthesis of readings).
- Download/Uncompress Wildfly
- Download/Install PostgreSQL (9.4 here)
- Configure PostgreSQL
- Setup postgres password:
su - postgres psql -c "ALTER USER postgres WITH PASSWORD 'postgres'" -d template1 exit
- Allow authentification using md5 from local sockets
nano /etc/postgresql/9.4/main/pg_hba.conf
# BEFORE: local all all peer local all all md5 # IPv4 local connections: host all all 127.0.0.1/32 md5 # IPv6 local connections: host all all ::1/128 md5
then restart PostgreSQL service
- Create database…
- Setup postgres password:
- To start in standalone mode listening on all IP addresses : bin/standalone.sh -b0.0.0.0
- Download PostgreSQL JDBC Driver (Here for Java 1.7+) JDBC Driver (PostgreSQL 9.4) into /tmp
- Configure Wildfly (JDBC Driver && DataSource)
- Start jboss-cli
bin/jboss-cli.sh
- Install module:
module add --name=org.postgres --resources=/tmp/postgresql-9.4-1205.jdbc41.jar --dependencies=javax.api,javax.transaction.api
- Declare JDBC Driver into Application Server
/subsystem=datasources/jdbc-driver=postgres:add(driver-name="postgres",driver-module-name="org.postgres",driver-class-name=org.postgresql.Driver)
- Install datasource named “PostGreDS”
data-source add --jndi-name=java:/PostGreDS --name=PostgrePool --connection-url=jdbc:postgresql://localhost/postgres --driver-name=postgres --user-name=postgres
- Start jboss-cli
- Other stuffs
References: