问题
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:
- Create the path
$JBOSS_HOME/modules/org/postgresql/main. Themodules/orgpart should already exist, make directories for the rest. In
$JBOSS_HOME/modules/org/postgresql/main/module.xmlwith the following content, changing theresource-rootentry 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>- Into the same directory as
module.xmlplacepostgresql-9.1-902.jdbc4.jar - Start JBoss AS
- Open
jboss-cliby running$JBOSS_HOME/bin/jboss-cli --connect 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)- Now create any required data sources, etc, using
postgresql-driveras 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>
来源:https://stackoverflow.com/questions/12403428/how-to-connect-jboss-as-7-1-1-with-postgresql