[JBoss.org AS7] Datasource configuration with Postgresql.

January 2, 2012

Dear all,

Well, recently I tried to make my application work on JBoss.org AS7.

Just to check if I should have to do some code modifications in my application when JBoss.com EAP 6 will be released.

FYI, you can download freely JBoss.org AS7 at the following URL –  http://www.jboss.org/jbossas – however as it is a community project you will not have any support with SLA.

In case of you would need one(s) then I would suggest you to subscribe to  JBoss EAP Solutions –   http://www.jboss.com/

So,  one of my main targets was to make AS7 work with Postgresql as its underlying database.

1] I wanted to use an updated Postgresql JDBC then I had to download it from the JDBC Postgresql website –  http://jdbc.postgresql.org/download.html

I used postgresql-9.1-901.jdbc4.jar

2]  I had to create dedicated directories and files inside the JBoss.org AS7 directory.

jboss-as-7.0.2.Final/modules/org/postgres/main

Then, to upload the postgresql-9.1-901.jdbc4.jar in the following path :   jboss-as-7.0.2.Final/modules/org/postgres/main

And then to create specific xml file named : module.xml  with the following content :

<module xmlns="urn:jboss:module:1.0" name="org.postgres">
  <resources>
    <resource-root path="postgresql-9.1-901.jdbc4.jar"/>
  </resources>
   <dependencies>
     <module name="javax.api"/>
     <module name="javax.transaction.api"/>
   </dependencies>
</module>
So in the  jboss-as-7.0.2.Final/modules/org/postgres/main path you should retrieved the following files :

> postgresql-9.1-901.jdbc4.jar – Postgresql JDBC4 driver.

> postgresql-9.1-901.jdbc4.jar.index – Index file generated by JBoss. That file should automatically created by the JBoss instance as soon as the Postgresql JDBC Driver will be discover by the instance.

> module.xml – Module Description

4] Then, I  had to add a Posgresql node in the following section of the jboss-as-7.0.2.Final/standalone/configuration/standalone.xml file.

<subsystem xmlns="urn:jboss:domain:datasources:1.0">

<datasources>

                  <datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="H2DS" enabled="true" jta="true" use-java-context="true" use-ccm="true">

                     <connection-url>
                                jdbc:h2:mem:test;DB_CLOSE_DELAY=-1
                     </connection-url>
                     <driver>
                     h2
                     </driver>
                     <pool>
                                <prefill>
                                          false
                                </prefill>
                                <use-strict-min>
                                          false
                                </use-strict-min>
                                <flush-strategy>
                                          FailingConnectionOnly
                                </flush-strategy>
                     </pool>
                     <security>
                                <user-name>
                                          sa
                                </user-name>
                                <password>
                                          sa
                                </password>
                     </security>
                  </datasource>
                  <datasource jndi-name="java:jboss/datasources/PostgresqlDS" pool-name="java:jboss/datasources/PostgresqlDS_Pool" enabled="true" jta="true" use-java-context="true" use-ccm="true">
                     <connection-url>
                                jdbc:postgresql://localhost:5432/mydb
                     </connection-url>
                     <driver>
                                postgresql
                     </driver>
                     <security>
                                <user-name>
                                          myuser
                                </user-name>
                                <password>
                                          mypassword
                                </password>
                     </security>
                  </datasource>
                <drivers>
                    <driver name="postgresql" module="org.postgres">
                        <xa-datasource-class>
                            org.postgresql.xa.PGXADataSource
                        </xa-datasource-class>
                    </driver>
                    <driver name="h2" module="com.h2database.h2">
                        <xa-datasource-class>
                            org.h2.jdbcx.JdbcDataSource
                        </xa-datasource-class>
                    </driver>
                </drivers>
</datasources>
</subsystem>
Rem: Do not forget to change the Postgresql Database user and password to yours.

5] You can check in the JBoss Administration Console your JDBC connection is rcognized – http://localhost:9990/console/App.html#server/datasources

You should see a green light if everything is OK.

6] Finally, do not forget to change the persistence -persistence.xml – file of your application to check your application works with the new Posgresql datasource.

<?xml version="1.0" encoding="UTF-8"?>

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="astroDatabase">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:jboss/datasources/PostgresqlDS</jta-data-source>
<class>com.hornain.as.astro.Users</class>
<properties>
                  <property name="hibernate.hbm2ddl.auto" value="create-drop" />
                  <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
                  <property name="hibernate.show_sql" value="false"/>
                  <property name="hibernate.format_sql" value="false"/>
                  <property name="hibernate.use_sql_comments" value="false"/>
</properties>
</persistence-unit>
</persistence>

Ref #1 : http://community.jboss.org/wiki/JBossAS7-DatasourceConfigurationForPostgresql

Ref #2 : http://community.jboss.org/thread/168958

Best Regards

Frederic

2 Responses to “[JBoss.org AS7] Datasource configuration with Postgresql.”

  1. akrsmv said

    Thanks for this post! It was very useful to me for two reasons:
    – I found aggregated info on the exact problem I had which solved it
    – It led me to the though its already a time to start blogging for such things myself too.
    P.S Btw, after I create my own blog, may I reblog you on that post🙂 ?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: