JBoss Database Connection Pool

可紊 提交于 2019-12-03 00:27:00

The pool in JBoss is all handled in the DataSource configuration. Here is the HowTo. The web app would have to do a JNDI lookup for the datasource to get the database connection rather than doing a direct JDBC URL, and then you will have pooling.

Transactions are another story, though.

EDIT: In response to your comment about how this affects the code, this is what it looks like:

String jndiPath = "java:DataSourceJNDIName"; //The exact prefix here has a lot to do with clustering, etc., but if you are using one JBoss instance standalone, this works.
Context ctx = new InitialContext();
DataSource ds = (DataSource) PortableRemoteObject.narrow(ctx.lookup(jndiPath), DataSource.class);
Connection c = ds.getConnection();

Technically speaking the PortableRemoteObject.narrow isn't necessary in a JBoss (4.2.2 anyway) single server configuration for sure, but it is more proper J2EE standard code, as general application servers don't have to return an object of the right type just for doing a Context.lookup.

The above doesn't cover the resource utilization and error handling issues. You are supposed to close that Context object when you are done with it, and of course the database connection, although JBoss will yell at you if you forget to close the database connection and the transaction ends, and close it for you.

Anyway, that Connection object is usable just as much as DriverManager.getConnection(url);

Ram

first create an xml file by name xxx-ds.xml and place this file in server/default/deploy/xxx-ds.xml

<datasources>
<local-tx-datasource>
<jndi-name>/jdbc/Exp</jndi-name>
<type-mapping>SQL</type-mapping>
<connection-url>jdbc:microsoft:sqlserver://          </connection-url>
<driver-class>com.microsoft.jdbc.sqlserver.SQLServerDriver</driver-class>
<user-name></user-name>
<password></password>
<min-pool-size>5</min-pool-size>
<max-pool-size>1000</max-pool-size>
</local-tx-datasource>
</datasources>

jboss-web.xml

<jboss-web>
<!--  <security-domain flushOnSessionInvalidation="false"/>-->
<!--  <context-root>/BSI</context-root>-->
  <resource-ref>
        <description>Database connection resource</description>
        <res-ref-name>jdbc/Exp</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <jndi-name>java:/jdbc/Exp</jndi-name>
        <res-auth>Container</res-auth>
    </resource-ref>
</jboss-web>

web.xml

<resource-ref>
    <description>Database connection resource</description>   
    <res-ref-name>jdbc/Exp</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

and now in your .java file

javax.naming.Context ctx1 = new javax.naming.InitialContext();
javax.sql.DataSource ds = (javax.sql.DataSource) ctx1.lookup("java:comp/env/jdbc/Exp");
con = ds.getConnection();

***** make sure that resource ref name should be same in all place

You don't have to change anything. When you select the right kind of data source (local-tx-datasource / xa-datasource), connection handling and TX is done for you. In $JBoss/docs/examples/jca you will find templates for virtually every database, that you can just reuse.

If you are using XA, you need to configure Tx-recovery. See this posting on a how-to: http://management-platform.blogspot.com/2008/11/transaction-recovery-in-jbossas.html (well, perhaps not a how-to in the standalone mode, but in conjunction with the Jopr source code).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!