Thursday, May 19, 2011

Detecting Connection Leaks In JBoss

Share it Please

Now a day’s applications need to access database in their daily operations. Accessing a database is quite a critical piece of functionality. In this article we will see how we can handle the connection leaks in our application using JBoss application server.
Consider the sample servlet,
public ArrayList<Connection> connectionList=new ArrayList<Connection>();
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

try {
      Context initCtx = new InitialContext();
      DataSource bean = (DataSource) initCtx.lookup("StudentDB");
      for(int i=0;i<=10;i++) {
      for(int i=0;i<=10;i++) {
      }catch(Exception e){

I have created an ArrayList which hold 10 connections. Now iam not closing the connections in my finally block. Now we are sure that the connections are not closed by us. JBoss application server sorts us out in closing the connections for the application. Once you access the application, you can see there are exceptions in the console like,
00:51:09,891 INFO  [CachedConnectionManager] Closing a connection for you.  Please close them yourself: org.jboss.resource.adapter.jdbc.j
java.lang.Throwable: STACKTRACE
at org.jboss.resource.connectionmanager.CachedConnectionManager.registerConnection( at org.jboss.resource.connectionmanager.BaseConnectionManager2.allocateConnection(     at org.jboss.resource.connectionmanager.BaseConnectionManager2$ConnectionManagerProxy.allocateConnection(  at org.jboss.resource.adapter.jdbc.WrapperDataSource.getConnection( at SamplePoolTestServlet.doPost(
        at javax.servlet.http.HttpServlet.service(
        at javax.servlet.http.HttpServlet.service(
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(  at org.apache.catalina.core.ApplicationFilterChain.doFilter(
  at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(

You can see in the exceptions that JBoss closes the connection for the application. It also gives us some more information as where the connections are created.
org.jboss.resource.adapter.jdbc.WrapperDataSource.getConnection( at SamplePoolTestServlet.doPost(
        at javax.servlet.http.HttpServlet.service(
        at javax.servlet.http.HttpServlet.service(
It says that the connections are created at in the SamplePoolTestServlet and at line 46.
The MBean that helps us in identifying the connections leaks is available in Server Instance/deploy/ jca-jboss-beans.xml.The MBean is CachedConnectionManager.The MBean looks like
<bean name="CachedConnectionManager" class="org.jboss.resource.connectionmanager.CachedConnectionManager">
<!-- Expose via JMX --> 

<!-- Whether to track unclosed connections and close them -->
<property name="debug">true</property>

<!-- Whether to throw an error for unclosed connections (true) or just log a warning (false) -->
<property name="error">false</property>
<!-- The transaction manager -->
<property name="transactionManager"><inject bean="TransactionManager" property="transactionManager"/></property>

The Main Use of this MBean is to track which application has which connections open. The attributes are self-explanatory.
The connections that are in use can also be obtained for the Jmx-console provided by JBoss application server,
Go to jboss.jca à service=CachedConnectionManager.we can check the ”listInUseConnections”  which list the stack traces showing the point of allocation for unclosed connections.

More Articles To Come .  Happy Coding ....


  1. هناك بعض العوامل الاساسية الي تهمنا
    نقل عفش بالمدينة المنورة
    جميعا عند نقل عفش بالمدينة المنورة مثل :-سيارات نقل الاثاث – عدد العماله الواجب توافرها لنقل العفش– المواد المستخدمة في عملية التغليف عفش – السعر نقل عفش – الضمان-رضا العميل-سرعه تنفيذ العمل-نقل العفش بطريقه امنه
    نقل عفش بالمدينه المنوره
    كليا. لابد أن تقوم بأخذ الضمانات الكافية التي تضمن لك حقك من شركة نقل عفش بالمدينة المنورة عند إخلال الشركة بأي من واجباتها المهنية المتعلقة بمجال عملها، أو عند إخلال الشركة
    نقل عفش المدينة المنورة
    بأي من تعهداتها التي التزمت بها لك عند تعاقدك معها وهذا يضمن حقك في حالة تقصير شركة نقل عفش في أداءها الوظيفي
    نقل العفش في المدينه المنوره
    المنوط بها.

  2. Good day! Would you mind if I share your blog with my twitter group?
    There's a lot of people that I think would really enjoy your content.

    Please let me know. Many thanks