Connection leak problems
Connection leaks happen when the application acquires a connection from the connection pool and never closes it.
Listing 3 shows the code changes in the TradeAppServlet required to trigger a connection leak problem:
Listing 3: Code for triggering a connection leak
//For Connection Consuming
if(TradeConfig.isConnectionLeak()){
if(triggerByFrequency(TradeConfig.getLeakFrequency())){
ConnectionConsumer cc = new ConnectionConsumer();
cc.start();
}
}
class ConnectionConsumer extends Thread{
public void run() {
try{
javax.naming.InitialContext ctx = new javax.naming.InitialContext();
javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup("jdbc/TradeDataSource");
java.sql.Connection con = ds.getConnection();
}catch(Exception e){
e.printStackTrace();
}
}
}
|
In the Server tab of Daytrader home page (see Figure 10), select the Jdbc Connection Leak checkbox and click Update Configuration. The application then leaks the connection of TradeDataSource at the specified frequency.
Figure 10: Specifying a JDBC connection leak on the DayTrader configuration screen
Monitoring and investigating the problem
When the connection leak problem occurs, you see JDBC-related alerts in the dashboard (Figure 11).
Figure 11: PTT dashboard with alerts
Click the Open Monitor Page button on the overview screen, and you can notice several connection pool alerts in the alert section.
In order to get the JDBC connection allocation stack trace, you must enable the ConnLeakLogic trace. Select Enable Trace from the context menu, as shown in Figure 12.
Figure 12: Enabling trace on the server
In the dialog box, select the Connection Leak trace and click OK to enable the trace (see Figure 13).
Figure 13: Enabling connection leak tracing
Within a few minutes, the data source has quite a few leaked connections. You can observe them by choosing Show Connection Pool Contents (see Figure 14).
Figure 14: Showing connection pool contents
In the connection pool content window (Figure 15), select TradeDataSource to check status of all connections, including the allocation time, used time, used thread number and allocation stack trace. Note that the allocation stack is displayed only for the connections that were made after you enabled the trace.
Figure 15: Connection pool monitor
From the connection pool content information, you can see that many connections are in use for more than 50 seconds, but not all of them can be found in the thread dump. This implies that the threads no longer exist but the connections have not been released. We can also notice that nearly all of the leaked connections were allocated with the stack trace below.
[8/7/12 20:39:59:437 EDT] 00000039 ConnLeakLogic 3 MCWrapper id 639d639d
Managed connection WSRdbManagedConnectionImpl@1cf81cf8
State:STATE_ACTIVE_INUSE Thread Id: 0000007e
Thread Name: Thread-86 Handle count 1 in-use for 181219ms
java.lang.Throwable
at com.ibm.ejs.j2c.ConnectionManager.allocateConnection(ConnectionManager.java:895)
at com.ibm.ws.rsadapter.jdbc.WSJdbcDataSource.getConnection(WSJdbcDataSource.java:668)
at com.ibm.ws.rsadapter.jdbc.WSJdbcDataSource.getConnection(WSJdbcDataSource.java:635)
at org.apache.geronimo.samples.daytrader.web.
TradeAppServlet$ConnectionConsumer.run(TradeAppServlet.java:209)
|
This trace information clearly points out the problem code.







