Thursday, 13 September 2012

How to connect jboss-as-7.1.1 to postgresql with JDBC driver

Download PgJDBC. postgresql-9.1-902.jdbc4.jar, the current version at time of writing. Adjust any filenames to match if you need a different version.
Now deploy the JDBC driver to JBoss AS 7 by putting it in the deployments folder or using the deploy command in jboss-cli. This will work for most, but not all, purposes.
Alternately, you an define a PostgreSQL JDBC driver module:
  1. Create the path $JBOSS_HOME/modules/org/postgresql/main. The modules/org part should already exist, make directories for the rest.
  2. In $JBOSS_HOME/modules/org/postgresql/main/modules.xml with the following content, changing the resource-root entry for the PgJDBC driver to refer to the driver you wish to use. 
       <?xml version="1.0" encoding="UTF-8"?>
      <module xmlns="urn:jboss:module:1.1" name="org.postgresql">
              <resource-root path="postgresql-9.1-902.jdbc4.jar"/>
              <module name="javax.api"/>
              <module name="javax.transaction.api"/>
              <module name="javax.servlet.api" optional="true"/>
   3. Into the same directory as modules.xml place postgresql-9.1-902.jdbc4.jar
   4. Open jboss-cli by running $JBOSS_HOME/bin/jboss-cli --connect
   5. Run the command:
       /subsystem=datasources/jdbc-driver=postgresql-driver:add(driver-name=postgresql-driver, driver-class-name=org.postgresql.Driver, driver-module-name=org.postgresql)
   6. Now create any required data sources, etc, using postgresql-driver as the driver name.
      You can create a datasource via the web ui, with jboss-cli with the data-source create command 
      (see data-source --help, data-source add --help), or by deploying a -ds.xml file like this 

      <?xml version="1.0" encoding="UTF-8"?>
            <datasource jndi-name="java:/datasources/some-ds" enabled="true" use-java-context="true"  

No comments:

Post a Comment