-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 12 posts ] 
Author Message
 Post subject: java.sql.SQLException: Already closed
PostPosted: Sun Dec 05, 2004 10:00 pm 
Regular
Regular

Joined: Tue Jun 08, 2004 8:24 am
Posts: 57
Hibernate version:
Hibernate 2.1 rc1

Code between sessionFactory.openSession() and session.close():
Code:
/**
* Utility class used for communication with
* the persistent data.
*/
public class HibernateHelper {
    private static final SessionFactory sessionFactory;
    private static final ThreadLocal session = new ThreadLocal();
   
    static {
        try {
            // Create the SessionFactory
            sessionFactory = new Configuration().configure().buildSessionFactory();
        } catch (HibernateException ex) {
            throw new RuntimeException("Hibernate Configuration problem: " + ex.getMessage(), ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static Session currentSession() throws HibernateException {
        Session s = (Session) session.get();
        // Open a new Session, if this Thread has none yet
        if (s == null) {
            s = sessionFactory.openSession();
            session.set(s);
        }
        return s;
    }
   
    public static void init() throws HibernateException {
        currentSession();
    }

    public static void closeSession() throws HibernateException {
        Session s = (Session) session.get();
        session.set(null);
        if (s != null && s.isOpen())
            s.close();
    }
       
    public static void flush() throws HibernateException {
        HibernateHelper.currentSession().flush();
    }
   
    public static Object load(Class aClass, Serializable compare) throws HibernateException {
        return HibernateHelper.currentSession().load(aClass, compare);
    }
   
    public static void update(Object objectToSave) throws HibernateException {
        HibernateHelper.currentSession().update(objectToSave);
    }   
   
    public static Serializable save(Object objectToSave) throws HibernateException {
        return HibernateHelper.currentSession().save(objectToSave);
    }
   
    public static void saveOrUpdate(Object objectToSave) throws HibernateException {
        HibernateHelper.currentSession().saveOrUpdate(objectToSave);
    }
   
    public static List find(String aQuery) throws HibernateException {
        return HibernateHelper.currentSession().find(aQuery);
    }

    public static Query createQuery(String aQuery) throws HibernateException {
        return HibernateHelper.currentSession().createQuery(aQuery);
    }

    public static Criteria createCriteria(Class criteriaClass) throws HibernateException {
        return HibernateHelper.currentSession().createCriteria(criteriaClass);
    }
   
    public static int delete(String toDelete) throws HibernateException {
        return HibernateHelper.currentSession().delete(toDelete);
    }
   
    public static void delete(Object toDelete) throws HibernateException {
        HibernateHelper.currentSession().delete(toDelete);
    }
}

/**
* Filter that ensures the hibernate session is closed after servicing the request
*/
public class HibernateSessionFilter implements Filter {
    private static org.apache.log4j.Logger log =
        org.apache.log4j.Logger.getLogger(HibernateSessionFilter.class.getName());

    /**
     * Place this filter into service.
     *
     * @param filterConfig The filter configuration object
     */
    public void init(FilterConfig filterConfig) throws
        ServletException
    {
    }

    public void destroy() {
    }

    /**
     * Ensures a hibernate session is opened and closed
     *
     * @param request The servlet request we are processing
     * @param result The servlet response we are creating
     * @param chain The filter chain we are processing
     *
     * @exception IOException if an input/output error occurs
     * @exception ServletException if a servlet error occurs
     */
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws
        IOException,
        ServletException
    {
        try {
            Session hsession = HibernateHelper.currentSession();
            hsession.setFlushMode(FlushMode.NEVER);
            // Pass control on to the next filter
            chain.doFilter(request, response);
        }
        catch (HibernateException ex) {
            throw new ServletException(ex);
        }
        finally {
            try {
                //HibernateHelper.flush();
                HibernateHelper.closeSession();
            }
            catch (HibernateException ex) {
                throw new ServletException(ex);
            }
        }
    }
}


Full stack trace of any exception that occurs:
Quote:
2004-12-01 00:30:28 StandardWrapperValve[UserControllerServlet]: Servlet.service() for servlet UserControllerServlet threw exception
net.sf.hibernate.JDBCException: Cannot close connection
at net.sf.hibernate.impl.BatcherImpl.closeConnection(BatcherImpl.java:278)
at net.sf.hibernate.impl.SessionImpl.disconnect(SessionImpl.java:3198)
at net.sf.hibernate.impl.SessionImpl.close(SessionImpl.java:547)
at org.application.data.HibernateHelper.closeSession(HibernateHelper.java:47)
at org.application.data.HibernateSessionFilter.doFilter(HibernateSessionFilter.java:54)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:233)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:204)
at org.application.EncodingFilter.doFilter(EncodingFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:233)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:204)
at org.application.LocaleFilter.doFilter(LocaleFilter.java:73)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:233)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:204)
at com.zergsoft.common.idx.webapp.CarrierFilter.doFilter(CarrierFilter.java:42)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:233)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:204)
at com.zergsoft.common.idx.webapp.CarrierFilter.doFilter(CarrierFilter.java:42)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:233)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:204)
at com.zergsoft.common.webapp.PathFilter.doFilter(PathFilter.java:46)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:233)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:204)
at com.zergsoft.common.webapp.PathFilter.doFilter(PathFilter.java:46)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:233)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:204)
at com.zergsoft.common.util.IdentifierFilter.doFilter(IdentifierFilter.java:103)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:233)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:204)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:564)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:245)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:199)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:564)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:195)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:164)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:149)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:564)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:156)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:564)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:972)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:211)
at org.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:309)
at org.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:387)
at org.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:673)
at org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:615)
at org.apache.jk.common.SocketConnection.runIt(ChannelSocket.java:786)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:677)
at java.lang.Thread.run(Thread.java:534)
Caused by: java.sql.SQLException: Already closed.
at org.apache.commons.dbcp.PoolableConnection.close(PoolableConnection.java:111)
at org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.close(PoolingDataSource.java:225)
at net.sf.hibernate.connection.DatasourceConnectionProvider.closeConnection(DatasourceConnectionProvider.java:64)
at net.sf.hibernate.impl.BatcherImpl.closeConnection(BatcherImpl.java:275)
... 51 more


Name and version of the database you are using:
MySQL 3.23.49


Notes
The exception is being thrown in HibernateHelper.closeSession():
Code:
    public static void closeSession() throws HibernateException {
        Session s = (Session) session.get();
        session.set(null);
        if (s != null && s.isOpen())
            s.close();      <---- exception thrown from here
    }


This problem is intermittent (happens about once every 10-15 accesses).
HibernateHelper uses ThreadLocal, so there's no race condition occurring at this level.
I've also tried checking isConnected(), but it still throws the exception at that point from time to time.
This error is a real show-stopper since eventually the database connection pool is exhausted:

Quote:
2004-12-06 08:11:11 StandardWrapperValve[UserControllerServlet]: Servlet.service() for servlet UserControllerServlet threw exception
java.lang.reflect.InvocationTargetException
at sun.reflect.GeneratedMethodAccessor236.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at com.zergsoft.common.webapp.RequestEvent.execute(RequestEvent.java:79)
at com.zergsoft.common.webapp.ControllerServlet.doGet(ControllerServlet.java:93)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:743)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:284)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:204)
at com.zergsoft.common.webapp.SearchAndReplaceFilter.doFilter(SearchAndReplaceFilter.java:31)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:233)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:204)
at org.application.data.HibernateSessionFilter.doFilter(HibernateSessionFilter.java:46)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:233)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:204)
at org.application.EncodingFilter.doFilter(EncodingFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:233)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:204)
at org.application.LocaleFilter.doFilter(LocaleFilter.java:73)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:233)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:204)
at com.zergsoft.common.idx.webapp.CarrierFilter.doFilter(CarrierFilter.java:42)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:233)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:204)
at com.zergsoft.common.idx.webapp.CarrierFilter.doFilter(CarrierFilter.java:42)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:233)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:204)
at com.zergsoft.common.webapp.PathFilter.doFilter(PathFilter.java:46)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:233)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:204)
at com.zergsoft.common.webapp.PathFilter.doFilter(PathFilter.java:46)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:233)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:204)
at com.zergsoft.common.util.IdentifierFilter.doFilter(IdentifierFilter.java:103)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:233)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:204)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:564)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:245)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:199)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:564)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:195)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:164)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:149)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:564)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:156)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:564)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:972)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:211)
at org.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:309)
at org.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:387)
at org.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:673)
at org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:615)
at org.apache.jk.common.SocketConnection.runIt(ChannelSocket.java:786)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:677)
at java.lang.Thread.run(Thread.java:534)
Caused by: org.application.data.DataException: org.application.data.DataException: net.sf.hibernate.JDBCException: Cannot open connection
at org.application.data.FreeInputPagePersistence.getPagesByLanguage(FreeInputPagePersistence.java:131)
at org.application.event.user.FreeInputPageEvents.listPages(FreeInputPageEvents.java:67)
... 60 more
Caused by: org.application.data.DataException: net.sf.hibernate.JDBCException: Cannot open connection
at org.application.data.FreeInputPagePersistence.getPages(FreeInputPagePersistence.java:89)
at org.application.data.FreeInputPagePersistence.getPagesByLanguage(FreeInputPagePersistence.java:112)
... 61 more
Caused by: net.sf.hibernate.JDBCException: Cannot open connection
at net.sf.hibernate.impl.BatcherImpl.openConnection(BatcherImpl.java:265)
at net.sf.hibernate.impl.SessionImpl.connect(SessionImpl.java:3152)
at net.sf.hibernate.impl.SessionImpl.connection(SessionImpl.java:3135)
at net.sf.hibernate.impl.BatcherImpl.prepareQueryStatement(BatcherImpl.java:61)
at net.sf.hibernate.loader.Loader.prepareQueryStatement(Loader.java:699)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:180)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:128)
at net.sf.hibernate.loader.Loader.list(Loader.java:918)
at net.sf.hibernate.loader.CriteriaLoader.list(CriteriaLoader.java:109)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:3435)
at net.sf.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:177)
at org.application.data.FreeInputPagePersistence.getPages(FreeInputPagePersistence.java:87)
... 62 more
Caused by: org.apache.commons.dbcp.SQLNestedException: Cannot get a connection, pool exhausted, cause: Timeout waiting for idle object
at org.apache.commons.dbcp.PoolingDataSource.getConnection(PoolingDataSource.java:148)
at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:518)
at net.sf.hibernate.connection.DatasourceConnectionProvider.getConnection(DatasourceConnectionProvider.java:59)
at net.sf.hibernate.impl.BatcherImpl.openConnection(BatcherImpl.java:262)
... 73 more
Caused by: java.util.NoSuchElementException: Timeout waiting for idle object
at org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:801)
at org.apache.commons.dbcp.PoolingDataSource.getConnection(PoolingDataSource.java:140)
... 76 more


Does anyone know what's going wrong or how to get around it?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 06, 2004 6:50 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
use c3p0 or proxool and test again

also Hibernate 2.1 rc1 --> hibernate 2.1.7

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 09, 2004 4:06 am 
Regular
Regular

Joined: Tue Jun 08, 2004 8:24 am
Posts: 57
Running it with hibernate 2.1.7 didn't help. I'll try it with the other pooling systems...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 17, 2004 2:29 am 
Regular
Regular

Joined: Tue Jun 08, 2004 8:24 am
Posts: 57
After running it for 2 days using c3p0 as a pooling mechanism, I get pretty much the same results.
Every so often, the application throws an SQLException, saying that the connection is already closed.


The code:
Code:
    public static void closeSession() throws HibernateException {
        Session s = (Session) session.get();
        session.set(null);
        if (s != null && s.isOpen())
            s.close();
    }

is clearly checking to make sure the session is open before attempting to close it, and yet the statment s.close() randomly hits the exception.
I still question the value of throwing an exception if you try to close an already closed session. Surely this shouldn't be considered an error...

I've used DataSource before on other projects using Torque and not had these issues, so it's beginning to sound a lot like a bug in the way Hibernate is accessing the connection pool.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 30, 2004 5:26 pm 
Newbie

Joined: Thu Jun 03, 2004 2:10 am
Posts: 19
I have the same problem with hibernate 2.1.7c, dbcp, and MySQL 4.1.7...

Did you find a solution to this?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 13, 2005 12:06 am 
Beginner
Beginner

Joined: Tue Sep 02, 2003 3:25 pm
Posts: 21
Location: Kingston Jamaica, West Indies
It does sound like a bug , why doesnt Gavin look at this bug. its serious what use is an application that cant run for a couple of hours


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 13, 2005 6:04 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
1. This filter implementation is wrong, you need to compute stack depth a to close session ( the same filter can be invoked many times for request by forward/include ) .
2. Wrapp session to throw Exception on "close" (this method must throw Exception *allways* outside filter). Connection can be closed by filter only, keep unwrapped instance on filter stack to close it.
3. Test application without connection pooling first ( maxHTTPRequests < maxDBConnections must be configured for good scalability ).


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 13, 2005 8:59 pm 
Regular
Regular

Joined: Tue Jun 08, 2004 8:24 am
Posts: 57
I'm not quite sure I've understood you.

Basically, the problem with my filter is that if there's a forward, it will re-run the filter again. Is this right?

So, given Filters A, B, C, and servlet S:

Request comes in, goes through A, B, C, then S. S causes a forward, which causes a second run through A, B, C, S without unwinding the old stack. I.E. Final stack looks like this: A, B, C, S, A, B, C, S. Is this right?

I always thought that the filters were not re-run, only the servlet level was re-run. (i.e. stack goes A, B, C, S, backup, S again).

I can see how in this case the filters will run twice and cause two "opens" (which are guarded, so only one is opened really), and two closes.


If I make a ThreadLocal counter for the filter depth, will this ensure that I know when I'm at the last instance on the filter chain?

Also, I'm not sure what you mean by wrapping a session to throw Exception on close. It already throws exceptions as it is, so how would forcing it to throw an exception help any?

Would the following filter work:

Code:
public class HibernateSessionFilter implements Filter {

    static final ThreadLocal threadLocal = new ThreadLocal();

    public void init(FilterConfig filterConfig) throws ServletException {}
    public void destroy() {}

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws
        IOException,
        ServletException
    {
        try {
            incrementCount();
            Session hsession = HibernateHelper.currentSession();
            hsession.setFlushMode(FlushMode.NEVER);
            // Pass control on to the next filter
            chain.doFilter(request, response);
        }
        catch (HibernateException ex) {
            throw new ServletException(ex);
        }
        finally {
            try {
                if(decrementCount())
                    HibernateHelper.closeSession();
            }
            catch (HibernateException ex) {
                // Should I throw this or ignore?
                throw new ServletException(ex);
            }
        }
    }

    // Increment the thread-local counter
    private void incrementCount() {
        Integer callCount = (Integer)threadLocal.get();
        if(null == callCount) {
            threadLocal.set(new Integer(1));
        } else {
            threadLocal.set(new Integer(callCount.intValue() + 1));
        }
    }

    // Decrement the thread-local counter and return true only if
    // counter is 0.
    private boolean decrementCount() {
        Integer callCount = (Integer)threadLocal.get();
        if(null == callCount) // should not happen
            return false;
        threadLocal.set(new Integer(callCount.intValue() - 1));
        return callCount.intValue() == 0;
    }
}


Actually, one other thing I just remembered:

Code:
HibernateHelper:
    private static final ThreadLocal session = new ThreadLocal();

    public static Session currentSession() throws HibernateException {
        Session s = (Session) session.get();
        // Open a new Session, if this Thread has none yet
        if (s == null) {
            s = getSessionFactory().openSession();
            session.set(s);
        }
        return s;
    }

    public static void closeSession() throws HibernateException {
        Session s = (Session) session.get();
        session.set(null);
        if (s != null && s.isOpen())
            s.close();
    }


On calling closeSession on HibernateHelper, it sets the ThreadLocal value (called session.. I think I'll change that name!) to null, so the reference to the closed session is gone.
That being the case, it should be impossible for HibernateHelper to ever close the same hibernate session twice.
Not only that, but I'm very clearly checking to see if the hibernate session is open before attempting to close. If a call to Hibernate's Session.isOpen() returns true, shouldn't a Session.close() succeed without throwing an exception?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 27, 2007 11:40 pm 
Beginner
Beginner

Joined: Wed Sep 28, 2005 5:30 pm
Posts: 25
The solution is to add these two attributes to your DBCP datasource (inside context.xml):

validationQuery="SELECT 1" testOnBorrow="true"

This will ensure that DBCP only hands out good connections to Hibernate.

Gili


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 23, 2008 4:36 am 
Newbie

Joined: Tue Jul 01, 2008 7:30 am
Posts: 11
Location: Pune,India
Hi
Im facing same issue, i tried with what Koda2 has suggested but it doesnt work.
I also tried with c3p0 as well with following set up in hibernate.cfg.xml file
<property name="c3p0.acquire_increment">1</property>
<property name="c3p0.idle_test_period">100</property> <!-- seconds -->
<property name="c3p0.max_size">100</property>
<property name="c3p0.max_statements">0</property>
<property name="c3p0.min_size">10</property>
<property name="c3p0.timeout">100</property> <!-- seconds -->

but this also didnt work.

Im using hibernate 3 , MS SQL SERVER

Please let me know what more i need to do ..
im not able to replicate the issue :(

Any help is appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 25, 2008 3:17 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Are you using openSession or getSession?

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 25, 2008 7:32 am 
Newbie

Joined: Tue Jul 01, 2008 7:30 am
Posts: 11
Location: Pune,India
hi Cameron ,
im using openSession()


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 12 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.