Pages

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++) {
                  connectionList.add(bean.getConnection());
      }
                 
     
      for(int i=0;i<=10;i++) {
      response.getWriter().println(connectionList.get(i).isClosed()+"\t\n");
      }
                 
      }catch(Exception e){
            e.printStackTrace();
      }
           
    }

}
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
dk6.WrappedConnectionJDK6@ba2e7
java.lang.Throwable: STACKTRACE
at org.jboss.resource.connectionmanager.CachedConnectionManager.registerConnection(CachedConnectionManager.java:278) at org.jboss.resource.connectionmanager.BaseConnectionManager2.allocateConnection(BaseConnectionManager2.java:524)     at org.jboss.resource.connectionmanager.BaseConnectionManager2$ConnectionManagerProxy.allocateConnection(BaseConnectionManager2.java:941)  at org.jboss.resource.adapter.jdbc.WrapperDataSource.getConnection(WrapperDataSource.java:89) at SamplePoolTestServlet.doPost(SamplePoolTestServlet.java:46)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
  at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)

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(WrapperDataSource.java:89) at SamplePoolTestServlet.doPost(SamplePoolTestServlet.java:46)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
       
It says that the connections are created at SamplePoolTestServlet.java:46.Its 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 --> 
     <annotation>@org.jboss.aop.microcontainer.aspects.jmx.JMX
(name="jboss.jca:service=CachedConnectionManager",
exposedInterface=org.jboss.resource.connectionmanager.CachedConnectionManagerMBean.class)</annotation>

<!-- 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>

</bean>
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 comment :

  1. 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

    ReplyDelete