-->
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: Transaction not successfully started
PostPosted: Wed Aug 08, 2007 4:08 am 
Newbie

Joined: Wed Aug 08, 2007 3:51 am
Posts: 6
Hello all

I'm working with hibernate3.jar + spring

I had a transactionfilter to start and terminate a hibernate session.

Code:
public class HibernateFilter implements Filter {

   public void destroy() {
      // TODO Auto-generated method stub

   }

   public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {
      //Start a connection to the database
      HibernateUtil.currentSession();

      //perform the normal http request response flow
      chain.doFilter(request, response);
      //close the connection to the database
      HibernateUtil.closeSession();

   }

   public void init(FilterConfig filterConfig) throws ServletException {
      // TODO Auto-generated method stub

   }

}



Besides that i got an Aspect
(try)
1) starts a transaction.
2) proceeds the pointcut code
3) commits the transaction or
(catch)
4) rollback the transaction.


That aspect is pointcutted on all my service methodes that ask for :
save/delete/... to my daoobject
or
Does a specific thing for that service (like generating pdf's on a server)


Now I got this failure when i try to do a transaction in a transaction in a transaction.

Example :
open session
open transaction
pointcut code ->
in that code ->
open session
open transaction
pointcut code ->
in that code ->
open session
open transaction
pointcut code
close transaction
close session
Fault error message

apparently he can't find his previous transaction anymore.

I tried to write something to keep track of my transaction, but it doesn't work.


Code:
public class HibernateUtil {

   private static Logger _logger = Logger.getLogger(HibernateUtil.class);

   private static final SessionFactory sessionFactory;

   /**
    * Init the session factory for this class on first method call
    */
   static {
      try {
         // Create the SessionFactory
         sessionFactory = new Configuration().configure()
               .buildSessionFactory();
      } catch (Throwable ex) {
         // Make sure you log the exception, as it might be swallowed
         _logger.error("Initial SessionFactory creation failed " + ex);

         throw new ExceptionInInitializerError(ex);
      }
   }

   public static final ThreadLocal threadLocalSession = new ThreadLocal();

   public static final ThreadLocal threadLocalTransaction = new ThreadLocal();

   /**
    *
    * <p>
    * Stupid init method, nothing special. This method is called from the
    * hibernate plugin for starting the factory on application server startup.
    * </p>
    *
    */
   public static void init() {
      // just emtpy to init the static method
   }

   /**
    *
    * <p>
    * Retrieve the Hibernate Session from the current Thread or init it from
    * the SessionFactory and save it in the current thread
    * </p>
    *
    * @return
    */
   public static Session currentSession() {
      Session s = (Session) threadLocalSession.get();
      // Open a new Session, if this Thread has none yet
      if (s == null) {
         s = sessionFactory.openSession();
         threadLocalSession.set(s);
      }
      return s;
   }

   public static Transaction currentTransaction(Session session) {
      Transaction t = null;
      t = session.getTransaction();
      if (t == null) {
         t = session.beginTransaction();
         threadLocalTransaction.set(t);
      }
      return t;
   }

   /**
    *
    * <p>
    * close the current session that is in the Thread
    * </p>
    *
    */
   public static void closeSession() {
      Session s = (Session) threadLocalSession.get();
      if (s != null)
         s.close();
      threadLocalSession.set(null);
   }
}


Is there a possibility to track or save transaction or an other solution to this?


printstack trace :

Code:
Exception :
org.hibernate.TransactionException: Transaction not successfully started
   at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:100)
   at be.in.inforbusiness.aspects.TransactionAspect.wrapTransaction(TransactionAspect.java:42)
   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:597)
   at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:562)
   at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:551)
   at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:56)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:176)
   at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:176)
   at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:210)
   at $Proxy122.giveAllSaleDomains(Unknown Source)
   at be.in.inforbusiness.web.controllers.HomeController.referenceData(HomeController.java:54)
   at org.springframework.web.servlet.mvc.SimpleFormController.referenceData(SimpleFormController.java:214)
   at org.springframework.web.servlet.mvc.AbstractFormController.showForm(AbstractFormController.java:559)
   at org.springframework.web.servlet.mvc.SimpleFormController.showForm(SimpleFormController.java:198)
   at org.springframework.web.servlet.mvc.SimpleFormController.showForm(SimpleFormController.java:175)
   at org.springframework.web.servlet.mvc.AbstractFormController.showNewForm(AbstractFormController.java:323)
   at org.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal(AbstractFormController.java:263)
   at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
   at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:45)
   at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:820)
   at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:755)
   at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:396)
   at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:350)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
   at be.in.inforbusiness.web.filters.CookieFilter.doFilter(CookieFilter.java:84)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
   at be.in.inforbusiness.web.filters.HibernateFilter.doFilter(HibernateFilter.java:26)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
   at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
   at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
   at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
   at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
   at java.lang.Thread.run(Thread.java:619)


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.