1

Does anybody know how to connect jboss-as-7.1.1 to PostgreSQL?

CoffeeRain
  • 4,460
  • 4
  • 31
  • 50
vivek rai
  • 2,109
  • 10
  • 25
  • 21
  • What have you already tried? Have you read any of the existing documentation on the topic and, if so, what trouble did you have with it? – Craig Ringer Sep 13 '12 at 09:53
  • Also, how do you intend to connect? Using a JTA data source? Direct JDBC connection from the app via the DriverManager? What? – Craig Ringer Sep 13 '12 at 10:08

1 Answers1

23

(Note that this was written for JBoss AS 7.1.1; keep that in mind if on a newer version, as things might have changed.)

Download PgJDBC. I'm assuming you're using 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/module.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">
         <resources>
             <resource-root path="postgresql-9.1-902.jdbc4.jar"/>
         </resources>
         <dependencies>
             <module name="javax.api"/>
             <module name="javax.transaction.api"/>
             <module name="javax.servlet.api" optional="true"/>
         </dependencies>
     </module>
    
  3. Into the same directory as module.xml place postgresql-9.1-902.jdbc4.jar
  4. Start JBoss AS
  5. Open jboss-cli by running $JBOSS_HOME/bin/jboss-cli --connect
  6. 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)
    
  7. 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"?>
<datasources>
  <datasource jndi-name="java:/datasources/some-ds" enabled="true" use-java-context="true"  
        pool-name="some-ds-pool">
    <connection-url>jdbc:postgresql:dbname</connection-url>
    <driver>postgresql-driver</driver>
    <security>
      <user-name>username</user-name>
      <password>password</password>
    </security>
  </datasource>
</datasources>
Craig Ringer
  • 307,061
  • 76
  • 688
  • 778
  • 3
    @abhishek No problem. You wouldn't normally get a complete answer to a question where you made no effort to explain what you'd tried, what you needed, etc; I only answered this because I happened to know off the top of my head and have written some documentation on it recently. For better answers in future, make sure to put more effort in to your questions. Remember, *you can edit your questions to improve them after you have posted them*. – Craig Ringer Sep 13 '12 at 11:17
  • i am new in this domain that's why so next time i improve my self and also thank you so much for helping me.. – vivek rai Sep 13 '12 at 12:00
  • To get XADatasources you must install the driver as module. – martin Mar 22 '13 at 15:12
  • @CraigRinger i'm trying to use the data-source create command for creting a data-source, i'm unable to do it.. i tried in many patterns but did not work. i want to have a datasource with `(name=PostgresqlDS,connection-url=jdbc:postgresql://localhost:5432/dataBaseName,jndi-name=java:/Postgre sDS,driver-name=postgresql-driver)` – 09Q71AO534 Sep 25 '13 at 14:09
  • @user2561626 please post a new detailed question and link back to this one so people know the context. If you like post a link to your new question here and I'll take a quick look, but I don't really work with AS anymore. – Craig Ringer Oct 06 '13 at 05:40
  • 2
    Beware: the file name is module.xml (not modules.xml) – Radim Oct 07 '13 at 13:29
  • Is this postgresql-driver thing now persistent_ – Zlatko Nov 18 '13 at 12:55
  • @Zlatko Er... what? context? Link? What're you trying to say/ask? – Craig Ringer Nov 18 '13 at 13:00
  • The driver. Once I connect from cli wiht the **jboss-cli --connect**, does it persist jboss/server restarts? – Zlatko Nov 18 '13 at 13:11
  • @Zlatko Once you run the command in jboss-cli to register the driver then yes, it remains registered until you unregister it. – Craig Ringer Nov 18 '13 at 13:17
  • Isn't is required to start jboss-server before step 4? Otherwise I get "The controller is not available at localhost:9999". – Marcel Jul 10 '14 at 11:33
  • @Marcel Yes, rather. Amended. – Craig Ringer Jul 10 '14 at 14:24