WildFly 9 is based on the latest release of IronJacamar which contains several enhancements. One of these, is the capability to detect Connection leaks in your applications. Let's see how to get to grips with it.
In order to detect Connection leaks you have to add a couple of properties to your startup script. Under windows:
set "JAVA_OPTS=%JAVA_OPTS% -Dironjacamar.mcp=org.jboss.jca.core.connectionmanager.pool.mcp.LeakDumperManagedConnectionPool -Dironjacamar.leaklog=leaks.txt"
When using Linux:
JAVA_OPTS="$JAVA_OPTS -Dironjacamar.mcp=org.jboss.jca.core.connectionmanager.pool.mcp.LeakDumperManagedConnectionPool -Dironjacamar.leaklog=leaks.txt"
The above properties will acttivate the LeakDumperManagerConnectionPool which will trace the leaks in the file named leaks.txt.
In order to trigger the LeakDumper, you need to flush the Connections in the pool, once that you have a suspect of a Connection Leak:
Now check the file leaks.txt which has been produced:
Leak detected in pool: PostgreSQL ConnectionListener: 1c6c192 Allocation timestamp: 1438520410260 Allocation stacktrack: java.lang.Throwable: ALLOCATION LEAK at org.jboss.jca.core.connectionmanager.pool.mcp.LeakDumperManagedConnectionPool.getConnection(LeakDumperManagedConnectionPool.java:96) . . . . . org.jboss.as.connector.subsystems.datasources.WildFlyDataSource.getConnection(WildFlyDataSource.java:67) at com.sample.LeakServlet.processRequest(LeakServlet.java:40) at com.sample.LeakServlet.doGet(LeakServlet.java:72)
As you can see we have got an interesting information: the leak was produced by the com.sampleLeakServlet in the method processRequest (line 40) and it was reported at the timestamp indicated above.
By the way, you can convert the timestamp in an human readable date with few rows of Java code:
Timestamp stamp = new Timestamp(1438520410260); Date date = new Date(stamp.getTime()); System.out.println(date);