View RSS Feed

My Java Tips

Wrapping JDBC Objects

Rate this Entry
by , 05-25-2011 at 09:55 AM (5001 Views)
In Glassfish connection pool has a property called "Wrap JDBC Objects" which can have value true or false. You may define it in domain.xml file.

This property is defined as: "When set to true, application will get wrapped jdbc objects for Statement, PreparedStatement, CallableStatement, ResultSet, DatabaseMetaData".

If you follow JDBC specifications, you will be see that obtaining connection using DataSource.getConnection() or other objects like statement, preparedStatement.getConnection() should be same. Do note its not the same if you use Glasshfish application server. GlassFish does not wrap others like statement, preparedStatement. Because of this, statement.getConnection() will return the physical connection rather than the one provided by GlassFish. This means that you might be using physical connection rather than connection from connection pool.

For example:

Java Code:
stat = rs.getStatement();
con = stat.getConnection();[/b]
rs.close(); rs=null;
stat.close() ; stat = null;
In the above example, you close the physical connection and not the logical connection, therefore connections are not returned to pool.

Following code will also give you a physical connection and hence will cause connection leak.

Java Code:
com.sun.appserv.jdbc.DataSource ds = .........
Connection con = ds. getConnection();
physicalCon = ds.getConnection(con);
wrap-jdbc-objects=true will make sure that statement.getConnection will be the same as DataSource.getConnection().

You may define this property via asadmin command. For instance:

Java Code:
asadmin list "*DerbyPool*"

asadmin get domain.resources.jdbc-connection-pool.DerbyPool.*
domain.resources.jdbc-connection-pool.DerbyPool.validate-atmost-once-period-in-seconds = 0
domain.resources.jdbc-connection-pool.DerbyPool.wrap-jdbc-objects = false

asadmin set domain.resources.jdbc-connection-pool.DerbyPool.wrap-jdbc-objects=true
domain.resources.jdbc-connection-pool.DerbyPool.wrap-jdbc-objects = true
I hope you find this useful.

Submit "Wrapping JDBC Objects" to Facebook Submit "Wrapping JDBC Objects" to Digg Submit "Wrapping JDBC Objects" to Submit "Wrapping JDBC Objects" to StumbleUpon Submit "Wrapping JDBC Objects" to Google