Skip navigation

Andreas Martin Blog

February 8, 2012 Previous day Next day
  1. Download the Postgres Driver which you need as .jar-file (http://jdbc.postgresql.org/download.html)
  2. Go to JBoss directory and find modules\org folder. Create two new folders, so you get modules\org\postgresql\main and insert the .jar file there.
  3. Create in the same folder a file named module.xml with the content (set in <resource-root path="" > the name of your .jar-file):
    <?xml version="1.0" encoding="UTF-8"?>
    <module xmlns="urn:jboss:module:1.0" name="org.postgresql">
     <resources>
     <resource-root path="postgresql-Y.X-Z.jdbcV.jar"/>
     </resources>
     <dependencies>
     <module name="javax.api"/>
     <module name="javax.transaction.api"/>
     </dependencies>
    </module>
    
  4. Edit your standalone.xml and add the following code between the tags <datasources><drivers>
     <driver name=”postgresql” module=”org.postgresql”>
     <xa-datasource-class>
     org.postgresql.xa.PGXADataSource
     </xa-datasource-class>
     </driver>
    
  5. Now start the server and check wether he is loading the driver. The console should show something like: INFO [org.jboss.as.connector.subsystems.datasources] JBAS010404: Deploying non-JDBC-compliant driver class org.postgresql.Driver (version 8.4). If it nothing showing about your Postgres Driver, check the spelling and the coding (e.g. the quotation marks can be in different code and dont work, can happen if you just copy and paste). Fix this before going on.
  6. Go to your standalone.xml and set between <datasources> following code (set your connection-url, usename and password, jndi-name can also be java:/postgresDS, you can do this step through admin console of the server as well):
    <datasource jndi-name="java:jboss/postgresDS" pool-name="postgresDS" enabled="true" jta="false" use-java-context="true" use-ccm="false">
    <connection-url>
     jdbc:postgresql://192.z.y.x:5432/yourdatabase
     </connection-url>
     <driver-class>
     org.postgresql.Driver
     </driver-class>
     <driver>
     postgresql
     </driver>
     <pool>
     <min-pool-size>
     2
     </min-pool-size>
     <max-pool-size>
     20
     </max-pool-size>
     </pool>
     <security>
     <user-name>
    urusername
     </user-name>
     <password>
    urpasswort
     </password>
     </security>
     <validation>
     <validate-on-match>
     false
     </validate-on-match>
     <background-validation>
     false
     </background-validation>
     <background-validation-millis>
     0
     </background-validation-millis>
     </validation>
     <statement>
     <prepared-statement-cache-size>
     0
     </prepared-statement-cache-size>
     <share-prepared-statements>
     false
     </share-prepared-statements>
     </statement>
     </datasource>
    
    
    
  7. Start you database server
  8. Test the jdbc connection with following jsp-file (set the name of your table and the column names):
    <%@page
     import="java.util.*,javax.naming.*,javax.sql.DataSource,java.sql.*"%>
    <%
     DataSource ds = null;
     Connection con = null;
     Statement stmt = null;
     InitialContext ic;
      
     try {
     ic = new InitialContext();
     ds = (DataSource) ic.lookup("java:/postgresDS");
     con = ds.getConnection();
     stmt = con.createStatement();
     ResultSet rs = stmt.executeQuery("select * from tabname");
    
     while (rs.next()) {
     out.println("<br> " + rs.getString("onecolumn") + " | "
     + rs.getString("othercolumn"));
     }
     rs.close();
     stmt.close();
     } catch (Exception e) {
     out.println("Exception thrown :/");
     e.printStackTrace();
     } finally {
     if (con != null) {
     con.close();
     }
     }
        %>
    
  9. Done.

 

The knowledge for this tutorial i got from the following sites (big thank to them!):

https://community.jboss.org/wiki/JBossAS7-DatasourceConfigurationForPostgresql 

https://community.jboss.org/thread/168958 

http://kousikraj.wordpress.com/2011/11/25/datasource-configuration-setup-for-jboss-as-7-with-example-of-postgresql/ 

http://blog.xebia.com/2011/07/19/developing-a-jpa-application-on-jboss-as-7/ 

http://jan.zawodny.pl/blog/2011/07/jboss-7-postgresql-9 

https://community.jboss.org/thread/168644

Filter Blog

By date:
By tag: