7
votes

I am new to jboss and i have been asked to incorporate jboss connection pooling mechanism with an existing web application. Considering that a web application database layer is properly written i.e. all resultsets, statements and connections being closed properly when not needed, What all code changes i will have to make in my web app after i have configured the jboss datasource properly.

Can anybody please point me to a tutorial or a code sample which uses jboss datasource in a web app.

3
Configuring JBoss is one part of the deal, there are quite a few samples and tutorials available online. What i am concerned with is the changes that might be required in the code base. Can somebody please point me to a sample or tutorial where i can see how do we retrieve a connection from JBoss darasource and how it should be handled later i.e. close or free the connection. Thanks.Salman A. Kagzi

3 Answers

10
votes

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

8
votes

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);

1
votes

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).