Hi,
I use following HibernateUtil class for hibernate ejb application
I occasionally got session close exception that happen randomly
How can I fix this randm exception of session close on ejb application
The exception as a follow
"[#|2006-12-24T15:58:39.627+0700|WARNING|sun-appserver-pe9.0|javax.enterprise.system.stream.err|_ThreadID=28;_ThreadName=p: thread-pool-1; w: 29;_RequestID=09b98503-a044-473a-90d2-c2f011aaf9a4;|org.hibernate.SessionException: Session is closed
at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:116)
at org.hibernate.transaction.JDBCTransaction.commitAndResetAutoCommit(JDBCTransaction.java:139)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:115)
at com.arkalogic.util.HibernateUtil.commitTransaction(HibernateUtil.java:100)
at com.arkalogic.parkalogic.ejb.dao.UserDao.doLogout(UserDao.java:208)
at com.arkalogic.parkalogic.ejb.user.ClientFacadeBean.doLogout(ClientFacadeBean.java:303)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at com.sun.enterprise.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1050)
at com.sun.enterprise.security.SecurityUtil.invoke(SecurityUtil.java:165)
at com.sun.ejb.containers.BaseContainer.invokeTargetBeanMethod(BaseContainer.java:2766)
at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:3847)
at com.sun.ejb.containers.EJBObjectInvocationHandler.invoke(EJBObjectInvocationHandler.java:190)
at com.sun.ejb.containers.EJBObjectInvocationHandler.invoke(EJBObjectInvocationHandler.java:107)
at $Proxy150.doLogout(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at com.sun.corba.ee.impl.presentation.rmi.ReflectiveTie._invoke(ReflectiveTie.java:121)
at com.sun.corba.ee.impl.protocol.CorbaServerRequestDispatcherImpl.dispatchToServant(CorbaServerRequestDispatcherImpl.java:650)
at com.sun.corba.ee.impl.protocol.CorbaServerRequestDispatcherImpl.dispatch(CorbaServerRequestDispatcherImpl.java:193)
at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleRequestRequest(CorbaMessageMediatorImpl.java:1705)
at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleRequest(CorbaMessageMediatorImpl.java:1565)
at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleInput(CorbaMessageMediatorImpl.java:947)
at com.sun.corba.ee.impl.protocol.giopmsgheaders.RequestMessage_1_2.callback(RequestMessage_1_2.java:178)
at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleRequest(CorbaMessageMediatorImpl.java:717)
at com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl.dispatch(SocketOrChannelConnectionImpl.java:473)
at com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl.doWork(SocketOrChannelConnectionImpl.java:1270)
at com.sun.corba.ee.impl.orbutil.threadpool.ThreadPoolImpl$WorkerThread.run(ThreadPoolImpl.java:479)
"
And the HibernateUtil class as a follow
import org.hibernate.*;
import org.hibernate.cfg.*;
import javax.transaction.UserTransaction;
import javax.naming.InitialContext;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
private static final ThreadLocal threadSession = new ThreadLocal();
private static final ThreadLocal threadTransaction = new ThreadLocal();
static {
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
/**
* Return Hibernate Session
* @return Session
* @exception HibernateException
*/
public static Session getSession() {
Session ses = (Session) threadSession.get();
try {
if (ses == null) {
ses = sessionFactory.openSession();
threadSession.set(ses);
}
} catch (HibernateException he) {
he.printStackTrace();
//throw new InfrastructureException(he);
}
return ses;
}
/**
* Close Hibernate Session
* @exception HibernateException
*/
public static void closeSession(){
Session s = (Session) threadSession.get();
try {
threadSession.set(null);
} catch (HibernateException he) {
he.printStackTrace();
} finally {
if (s != null && s.isOpen()) {
s.close();
//clear();
}
}
}
/**
* Begin Hibernate Transaction
* @exception HibernateException
*/
public static void beginTransaction() {
Transaction tx = (Transaction) threadTransaction.get();
try {
if (tx == null) {
tx = getSession().beginTransaction();
threadTransaction.set(tx);
}
} catch (HibernateException he) {
he.printStackTrace();
}
}
/**
* Commit Hibernate Transaction
* @exception HibernateException
*/
public static void commitTransaction() {
Transaction tx = (Transaction) threadTransaction.get();
try {
if ( tx != null && !tx.wasCommitted()
&& !tx.wasRolledBack() ) {
tx.commit();
closeSession();
}
threadTransaction.set(null);
} catch (HibernateException he) {
rollbackTransaction();
he.printStackTrace();
}
}
/**
* Commit Hibernate Transaction, not closing session
* @exception HibernateException
*/
public static void commitTransactionNotClose() {
Transaction tx = (Transaction) threadTransaction.get();
try {
if ( tx != null && !tx.wasCommitted()
&& !tx.wasRolledBack() )
tx.commit();
threadTransaction.set(null);
} catch (HibernateException he) {
rollbackTransaction();
he.printStackTrace();
}
}
/**
* Reverse transaction into previous uncommited state
* @exception HibernateException
*/
public static void rollbackTransaction() {
Transaction tx = (Transaction) threadTransaction.get();
try {
threadTransaction.set(null);
if ( tx != null && !tx.wasCommitted() && !tx.wasRolledBack() ) {
tx.rollback();
}
} catch (HibernateException he) {
he.printStackTrace();
} finally {
closeSession();
// clear();
}
}
}
|