I have a problem connecting a database with JDBC (DB2). I recently implemented transaction management (commit / rollback) in a block of code, and I use the same connection for all my queries / operations in the database. We never explicitly close the connection between each request / operation. However, at runtime we get an exception indicating that our connection is invalid (closed / null). When I debug step by step, it seems that the connection fields unexpectedly become null. This looks random and it never happens on the same line. I am sure that we ourselves do not close the connection! We use the connection pool in Tomcat.
Here is a sample code:
try {
connection = DB.getConnection();
connection.setAutoCommit(false);
savepoint = connection.setSavepoint("avantModif");
connection.commit();
}
catch(Exception e) {
if (connection != null && !connection.isClosed() && savepoint != null)
{
connection.rollback(savepoint);
}
throw e;
}
finally
{
if(connection != null){
connection.close();
}
Here is a screenshot of the debugger showing the contents of the Connection object:

Tomcat server.xml:
<Resource
name="jdbc/dbcpGlobal"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.ibm.as400.access.AS400JDBCDriver"
url="jdbc:as400://lvq400/apilib;naming=system;errors=full;date format=iso;prompt=false;trace=false;reconnect=true"
username="[here_is_our_username]" password="[here_is_our_password]"
maxIdle="7" maxActive="15" maxWait="5"
removeAbandoned="true" removeAbandonedTimeout="120" logAbandoned="true"
testOnBorrow="true" testOnReturn="true" validationQuery="select 1 from sysibm/sysdummy1" />
null, !
, JDBC ?