如何将 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 00:18:14  来源:igfitidea点击:

How to connect Jboss-as-7.1.1 with Postgresql

postgresqljboss7.x

提问by vivek rai

Does anybody know how to connect jboss-as-7.1.1to 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 deploymentsfolder or using the deploycommand 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 驱动程序模块:

  1. Create the path $JBOSS_HOME/modules/org/postgresql/main. The modules/orgpart should already exist, make directories for the rest.
  2. In $JBOSS_HOME/modules/org/postgresql/main/module.xmlwith the following content, changing the resource-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>
    
  3. Into the same directory as module.xmlplace postgresql-9.1-902.jdbc4.jar
  4. Start JBoss AS
  5. Open jboss-cliby 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-driveras the driver name.
  1. 创建路径$JBOSS_HOME/modules/org/postgresql/main。该modules/org部分应该已经存在,为其余部分创建目录。
  2. $JBOSS_HOME/modules/org/postgresql/main/module.xml以下内容中,将resource-rootPgJDBC 驱动程序的条目更改为您希望使用的驱动程序。

    <?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. 进入与module.xmlplace相同的目录postgresql-9.1-902.jdbc4.jar
  4. 启动 JBoss AS
  5. jboss-cli运行打开$JBOSS_HOME/bin/jboss-cli --connect
  6. 运行命令:

    /subsystem=datasources/jdbc-driver=postgresql-driver:add(driver-name=postgresql-driver, driver-class-name=org.postgresql.Driver, driver-module-name=org.postgresql)
    
  7. 现在创建任何所需的数据源等,postgresql-driver用作驱动程序名称。

You can create a datasource via the web ui, with jboss-cliwith the data-source createcommand (see data-source --help, data-source add --help), or by deploying a -ds.xmlfile like this:

您可以通过Web界面创建一个数据源,以jboss-clidata-source create命令(见data-source --helpdata-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>