如何将 Jboss-as-7.1.1 与 Postgresql 连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12403428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to connect Jboss-as-7.1.1 with Postgresql
提问by vivek rai
Does anybody know how to connect jboss-as-7.1.1
to PostgreSQL?
有人知道如何连接jboss-as-7.1.1
到 PostgreSQL 吗?
回答by Craig Ringer
(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.)
(请注意,这是为 JBoss AS 7.1.1 编写的;如果是更新的版本,请记住这一点,因为事情可能已经改变了。)
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.
下载 PgJDBC。我假设您使用postgresql-9.1-902.jdbc4.jar
的是撰写本文时的当前版本。如果您需要不同的版本,请调整任何文件名以匹配。
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.
现在,通过把它在部署JDBC驱动程序到JBoss AS 7deployments
文件夹或使用该deploy
命令jboss-cli
。这将适用于大多数但不是所有目的。
Alternately, you an define a PostgreSQL JDBC driver module:
或者,您可以定义一个 PostgreSQL JDBC 驱动程序模块:
- Create the path
$JBOSS_HOME/modules/org/postgresql/main
. Themodules/org
part should already exist, make directories for the rest. In
$JBOSS_HOME/modules/org/postgresql/main/module.xml
with the following content, changing theresource-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>
- Into the same directory as
module.xml
placepostgresql-9.1-902.jdbc4.jar
- Start JBoss AS
- Open
jboss-cli
by 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-driver
as the driver name.
- 创建路径
$JBOSS_HOME/modules/org/postgresql/main
。该modules/org
部分应该已经存在,为其余部分创建目录。 在
$JBOSS_HOME/modules/org/postgresql/main/module.xml
以下内容中,将resource-root
PgJDBC 驱动程序的条目更改为您希望使用的驱动程序。<?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>
- 进入与
module.xml
place相同的目录postgresql-9.1-902.jdbc4.jar
- 启动 JBoss AS
jboss-cli
运行打开$JBOSS_HOME/bin/jboss-cli --connect
运行命令:
/subsystem=datasources/jdbc-driver=postgresql-driver:add(driver-name=postgresql-driver, driver-class-name=org.postgresql.Driver, driver-module-name=org.postgresql)
- 现在创建任何所需的数据源等,
postgresql-driver
用作驱动程序名称。
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:
您可以通过Web界面创建一个数据源,以jboss-cli
用data-source create
命令(见data-source --help
,data-source add --help
或通过部署)-ds.xml
这样的文件:
<?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>