# Tracking JDBC Connection Leaks in Java EE Applications

When an application is managing itself the access to the database through JDBC Datasources, the release of JDBC resources needs to be done. In some cases, the close of the connection is not done. As the JOnAS application server is using a pool to manage these accesses to the database, it means that the pool may reach its maximum size as the connections are not closed (and then put back into the pool). And if the pool reach its maximum size, new requests will go in the wait state or wll be aborted. Thus, this kind of problems is a huge problem in a production system.

Fortunately, there are some features provided by JOnAS to handle this case.

- For example there are JDBC Pooling mechanisms that will kill connections if they've not be used since a long time (that can be configured) so the pool can lowered its size. See the `max-opentime` parameter of the JDBC RA or datasources.
- There is also a new feature that will close automatically connections if they are not closed after their access. This feature is provided through the JDBC JNDI Interceptor that have to be enabled at the JOnAS starting. Once enabled, all connections that are not closed will be closed automatically. This mechanism is applied on all datasources.
- JOnAS is providing informations that allows to track the root of the problem in the source code of the application. For example, when there is a connection leak, JOnAS is able to print in the Datasource's MBeans the line of code where the connection was opened. Thus, a quick review of the code needs to be done in order to know why the close() instruction has not be done.

## Howto: Enabling JNDI interceptors

JDBC JNDI interceptors can be enabled by both:

1. adding the `jndi-intercept` service in the `jonas.services` property of the `$JONAS_BASE/conf/jonas.properties` file.
2. setting the `-jndi-interceptor` option in the `jonas start` command line.

## Howto: Monitoring JDBC datasources

JOnAS provides some monitoring informations on JDBC connections available from the JMX interface. The MBean names varies depending whether the JDBC datasource was configured with the dbm service (properties file) or the resource service (Resource Adapter or JCA connector) :

- `jonas:j2eeType=JDBCDataSource,name=mydatasource,*` for a MBean related to a dbm configuration
- `jonas:j2eeType=JCAConnectionFactory,name=mydatasource,*` for a MBean related to a RA configuration

These MBeans provides the following operations:

- `getOpenedConnections(int openDurationSec)` gets a list of opened physical connections being used for more than a given amount of time in sec (default is 30 sec).
- `getConnectionDetails(int connectionId)` gets detailed informations about a specified connection such as age.
- `forceClose(int connectionId)` attempts to force an opened physical connections to be destroyed.

Additionable informations such as the stack trace of the thread getting the connection can be provided. For that, the `observable` attribute of the JDBC datasource' MBean must be set to `true` at runtime. You can use a JMX console or the JASMINe mbeancmd tool for accessing to the MBeans.