2009. 5. 28. 21:18

[CachedConnectionManger] Closing a connection for you.





CMT를 사용하지 않고 iBATIS의 트랜잭션을 이용해 수동으로 트랜잭션 관리를 하고 있었다.

그런데 JBOSS에서는 CachedconnectionManager란 녀석이 친절하게 Connection leak을 방지하기 위해 Connection을 끊어준다.
(JBOSS 입장에서는 iBATIS의 connection관리 행위를 이해하지 못하므로)

그래서 답은 CMT를 사용하거나, 해당 기능을 사용하지 않으면 된다는 것이다.
물론 그로인해 Connection leak에 대한 책임이 WAS에게서 개발자로 넘어오게 되긴 하지만 말이다.

참고글에서는 persistant framework 을 믿고 해당 기능을 끄라는데, 왠지 찜찜하다.

다음은 원글이다.

What does the message "Closing a connection for you" mean?

It means you are not closing your connections to return them to the pool. To avoid connection leaks you should have code like the following:

Connection connection = dataSource.getConnection();
try
{
// DO WORK
}
finally
{
   try
   {
       connection.close();
   }
   catch (Throwable ignored)
   {
   }
}

For jdbc you should do the same thing for Statements and ResultSets. Normally Statements are closed when the Connection is closed, but connection pooling means the close does not happen. Similarly, if you have prepared statements in a cache, ResultSets need to be closed.

Thread Local Patterns

Many persistent frameworks (hibernate/ojb) open and close connections "at random". i.e. As far as JBoss is concerned it looks like one ejb allocated the connection and another closed it.

The connection close checking does not understand this behaviour. It expects the same ejb that allocated the connection to also close it.

If you use such a pattern, you can turn off this message (see below) but you are on your own when it comes to detecting connection leaks. From 3.2.6 there is a "listInUseConnections" on the CachedConnectionManager. However, there is this known issue with some transacion demarcation semantics. This is not really a JBoss use, more that ThreadLocal patterns bypass J2EE semantics. If you do hit the problem removing the CachedConnectionInterceptor from the conf/standardjboss.xml will workaround the spurious message. You can trust hibernate at least to close connections properly provided you are ending user transactions correctly.

Turning off Connection Close Checking

In production you don't need this checking. Hopefully you have found all the connection leaks during development. You can turn it off on the CachedConnectionManager.

  • transaction-service.xml for 3.2.x
  • jbossjca-service.xml for 4.x

Change "Debug" to false.

  <mbean code="org.jboss.resource.connectionmanager.CachedConnectionManager" 
         name="jboss.jca:service=CachedConnectionManager">
    <depends optional-attribute-name="TransactionManagerServiceName">jboss:service=TransactionManager</depends>

    <!-- Enable connection close debug monitoring -->
    <attribute name="Debug">false</attribute>

Related