How to connect Jboss-as-7.1.1 with Postgresql [clo

2019-01-26 01:21发布

问题:

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

回答1:

(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>