-->
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.  [ 1 post ] 
Author Message
 Post subject: Hibernate2 to Hibernate3, session closed after commit
PostPosted: Wed Feb 14, 2007 12:54 pm 
Newbie

Joined: Wed Feb 14, 2007 10:57 am
Posts: 1
Hello,
I try to migrate from Hibernate 2 to Hibernate 3.2, and my code now generates an exception.

I code this way to avoid handling tx.rollback() in every catch:
Code:
Transaction tx = null;
Session session = null;
  try{
    session = new HibernateUtil().currentSession();
    tx = session.beginTransaction();
    ... do something ...
    logger.debug("about to commit");
    tx.commit();
    logger.info("commit ok");

} catch (NullPointerException  npe){
   logger.fatal("PartnerManager.createEntity nullPointer ",npe);
   throw (npe);

} catch(Exception dimE){
   logger.warn("PartnerManager.createEntity exception ",dimE);
   throw dimE;
} finally {
           
   try {
     logger.debug("finally");
                               
     if(session!=null && session.isOpen())  logger.info("session is open");

     if(tx.isActive()) logger.debug("tx is active");
     else logger.debug("tx is not active");
                               
     if(tx!=null) tx.rollback();
     logger.debug("1");
               
     HibernateUtil.closeSession();

   } catch (HibernateException e) {
         logger.warn("error casted from Hibernate to CFECException ",e);
         throw new CFECException("error casted from Hibernate to CFECException ",e);
}


I get this exception

2007-02-14 17:50:46,171 DEBUG (PartnerManager.java:2559) - about to commit
2007-02-14 17:50:46,171 INFO (PartnerManager.java:2561) - commit ok
2007-02-14 17:50:46,171 DEBUG (PartnerManager.java:2592) - finally
2007-02-14 17:50:46,171 INFO (PartnerManager.java:2594) - session is open
2007-02-14 17:57:22,015 DEBUG (PartnerManager.java:2597) - tx is not active
2007-02-14 17:50:46,171 WARN (PartnerManager.java:2609) - error casted from Hibernate to CFECException
org.hibernate.TransactionException: Transaction not successfully started
at org.hibernate.transaction.JDBCTransaction.rollback(JDBCTransaction.java:149)
at business.partner.PartnerManager.createEntity(PartnerManager.java:2596)
at action.partner.PartnerInfoAction.create(PartnerInfoAction.java:549)
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 org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:276)
at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:196)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:226)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:415)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
at java.lang.Thread.run(Thread.java:595)
2007-02-14 17:50:46,187 FATAL (PartnerInfoAction.java:563) - une erreur inconnue s est produite : error casted from Hibernate to CFECException

The problem comes from the fact that once a commit is performed, the session seems to be closed, and I can not make a rollback.

I guess I can add a test
Code:
if(tx!=null && tx.isActive()) tx.rollback();


but I would like to understand how to setup correctly my hibernate.cfg.xml file.

I found some information is this post http://forum.hibernate.org/viewtopic.php?t=948638&highlight=releasemode.

So, I tried to use the auto_close_session and release_mode properties like this
<property name="hibernate.transaction.auto_close_session">false</property>
<property name="hibernate.connection.release_mode">on_close</property>

but I still don't manage to setup the hibernate configuration properly to have my code run.

Please find below the HibernateUtil I use:
Code:
package hibernate;

import java.sql.Connection;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


public class HibernateUtil {
   
    static Logger logger = Logger.getLogger(HibernateUtil.class);
    private static final SessionFactory sessionFactory;

    static {
        try {
            // Crée la SessionFactory
            sessionFactory = new Configuration().configure().buildSessionFactory();

        } catch (HibernateException ex) {
            throw new RuntimeException("Problème de configuration : " + ex.getMessage(), ex);
        }
    }

    private static final ThreadLocal session = new ThreadLocal();

    public static Session currentSession() throws HibernateException {
        long beg= System.currentTimeMillis();
        logger.debug("currentSession begin ==> ");
       
        Session s = (Session) session.get();
        // Ouvre une nouvelle Session, si ce Thread n'en a aucune
        if (s == null) {
            s = sessionFactory.openSession();
            session.set(s);
        }
       
        logger.debug("currentSession end ==> process="+( System.currentTimeMillis()-beg) );
       
        return s;
    }

    /**
     * creation d'une session hibernate a partir d'une connection sql pour les cas ou on melange sql et hibernate
     * si possible, utiliser la methode statique
     */
    public Session currentSession(Connection con) throws HibernateException {
        long beg= System.currentTimeMillis();
        logger.debug("currentSession begin ==> ");
       
        Session s = (Session) session.get();
            s = sessionFactory.openSession(con);
            session.set(s);           
       
        logger.debug("currentSession end ==> process="+( System.currentTimeMillis()-beg) );
       
        return s;
    }

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

}


thank you for your help
__cyrille__


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

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.