1

I am using struts2-fullhibernatecore-plugin-2.2.1-GA.jar for injecting session as,

 @SessionTarget
 protected Session        hSession;

 @TransactionTarget
 protected Transaction    hTransaction;

i can see in jboss logs stacktraces that it's leaving the jdbc connections open (as unclosed). Stacktrace as:

[org.jboss.jca.core.api.connectionmanager.ccm.CachedConnectionManager]
(http--   0.0.0.0-8080-203) IJ000100: Closing a connection for you. 
Please close them yourself:       org.jboss.jca.adapters.jdbc.jdk6.WrappedConnectionJDK6@62a8c68c: 
java.lang.Throwable:   STACKTRACE
at org.jboss.jca.core.connectionmanager.ccm.CachedConnectionManagerImpl.registerConnection(CachedConnectionManagerImpl.java:265)
at org.jboss.jca.core.connectionmanager.AbstractConnectionManager.allocateConnection(AbstractConnectionManager.java:495)
at org.jboss.jca.adapters.jdbc.WrapperDataSource.getConnection(WrapperDataSource.java:129)
at  org.hibernate.connection.DatasourceConnectionProvider.getConnection(DatasourceConnectionProvider.java:92) [hibernate3.jar:]
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446) [hibernate3.jar:]
at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167) [hibernate3.jar:]
at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:142) [hibernate3.jar:]
at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:85) [hibernate3.jar:]
at com.googlecode.s2hibernate.struts2.plugin.interceptors.SessionTransactionInjectorInterceptor.injectHibernateTransactionByAnnotation(SessionTransactionInjectorInterceptor.java:572) [struts2-fullhibernatecore-plugin-2.2.1-GA.jar:]
at com.googlecode.s2hibernate.struts2.plugin.interceptors.SessionTransactionInjectorInterceptor.injectHibernateTransactionByAnnotation(SessionTransactionInjectorInterceptor.java:579) [struts2-fullhibernatecore-plugin-2.2.1-GA.jar:]
at com.googlecode.s2hibernate.struts2.plugin.interceptors.SessionTransactionInjectorInterceptor.intercept(SessionTransactionInjectorInterceptor.java:190) [struts2-fullhibernatecore-plugin-2.2.1-GA.jar:]

Coding Part, The way i am doing the things are as: Using AbstractSimpleGenericDao class:

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.googlecode.s2hibernate.struts2.plugin.annotations.SessionTarget;
import com.googlecode.s2hibernate.struts2.plugin.annotations.TransactionTarget;

@SuppressWarnings("unchecked")
public abstract class AbstractSimpleGenericDao<C, I extends Serializable> {

Class<C>                 entityClass;

@SessionTarget
protected Session        hSession;

@TransactionTarget
protected Transaction    hTransaction;

{
 entityClass = (Class<C>) ((ParameterizedType)           
 getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}

public List<C> getAll()
{
 try
{
  return hSession.createCriteria(entityClass).list();
}
 catch (HibernateException e)
 {
   throw e;
 }
 }

   public C get(I id)
   {
    try
    {
      return (C) hSession.get(entityClass, id);
     }
    catch (HibernateException e)
      {
      throw e;
      }
      }

public void save(C object)
{
 try
 {
    hSession.save(object);
 }
    catch (HibernateException e)
    {
      hTransaction.rollback();
      throw e;
    }
    }

     public void update(C object)
       {
         try
          {
            hSession.update(object);
           }
    catch (HibernateException e)
      {
       hTransaction.rollback();
       throw e;
       }
        }

  public void delete(I id)
    {
     try
    {
      C actual = get(id);
      hSession.delete(actual);
     }
     catch (HibernateException e)
     {
     hTransaction.rollback();
      throw e;
      }
      }
      }

Then extending above AbstractSimpleGenericDao class in my DAO class as:

 public class UserRoleDAO extends AbstractSimpleGenericDao<UserRole, UserRoleId> {

      public List L() {
try {
    String queryString = "from UserRole";
    Query queryObject = hSession.createQuery(queryString);
    return queryObject.list();
} catch (RuntimeException re) {
    throw re;
}
  }

Then in my struts Action class, instantiating DAO and retrieving list as:

  public class abc extends ActionSupport{
      private UserRoleDAO userRoleDao = new UserRoleDAO();
      private List ls=new ArrayList();
      public String execute()
    {
         List ls=userRoleDao.L()
         return "success";
    }
        }

Above all, I am using struts2 custom Interceptor for Authentication, and as interceptors are not thread safe. After ActionInvocation invoke() method in interceptor, getting the above error stacktrace. Does that mean interceptor, being not thread safe, could leak the hibernate session (Connections)?

Roman C
  • 49,761
  • 33
  • 66
  • 176
Ashish Kataria
  • 520
  • 3
  • 11
  • 31
  • 1. Are you using `hibernatePlugin.customSessionFactoryClass` property? 2. Are you extending hibernate-default? – prem30488 Aug 17 '14 at 04:28
  • provide the code of the functions you are using session in. Did you specify rollback in catch blocks? – prem30488 Aug 17 '14 at 05:15
  • And use 2.2.2 version. You might have used hsession in wrong way, provide some code so I can understand more about your problem. – prem30488 Aug 17 '14 at 05:18
  • Hi @ParthTrivedi, Thanks for your response. I have added the code, this way i am doing the things..but whenever i am checking the server logs, getting lot of unclosed connection's stacktraces. Anything i am doing wrong way? – Ashish Kataria Aug 17 '14 at 19:43
  • @ParthTrivedi Above all, I am using struts2 custom Interceptor for Authentication, and that is not thread safe. After ActionInvocation invoke() method in interceptor, getting the above error stacktrace. Does that mean interceptor, being not thread safe, could leak the session (Connections)? – Ashish Kataria Aug 18 '14 at 05:46
  • Why did you make your class abstract? – prem30488 Aug 18 '14 at 06:29
  • Did you specify connection pool size in `hibernate.cfg.xml`? – prem30488 Aug 18 '14 at 06:34
  • http://grepcode.com/file/repo1.maven.org/maven2/com.jgeppert.struts2.jquery/struts2-jquery-grid-showcase/3.5.1/com/jgeppert/struts2/jquery/grid/showcase/dao/AbstractSimpleGenericDao.java – Ashish Kataria Aug 18 '14 at 07:52
  • I am using jboss connection pooling, so specified only datasource name in hibernate.cfg.xml. connection pool size configured in jboss standalone.xml – Ashish Kataria Aug 18 '14 at 07:54

0 Answers0