-->
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.  [ 7 posts ] 
Author Message
 Post subject: Testing JTA Transaction Rollback
PostPosted: Wed Aug 23, 2006 12:09 am 
Newbie

Joined: Thu Aug 03, 2006 6:54 pm
Posts: 16
Hello Hibernate Users,

I have the following code to test whether a JTA Transaction really can rollback:

Code:
Customer ctmr = new Customer("John", "Doe");
ctmr.setcid(1);  //Set customer ID

Fintrans fts = new Fintrans();
fts.setAmountWillingToPay(12000.05);
fts.setCustomer(ctmr);
fts.setDatetime("06/22/2006");
fts.setGrandTotalAccrued(3213.20);
fts.setGrandTotalPaid(13651.10);
fts.setReason("No reason given");

UserTransaction tx = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");

try{
   tx.begin();
   
   factory.getCurrentSession().save(fts);
   
   tx.commit();
}catch(Exception e){

   if (tx != null)
      tx.rollback();
   e.printStackTrace();
}


I have two tables on the same database - customer and fintrans. The fintrans table has a foreign key to the customer table (cid). The customer table is empty, so when I run the above code it should rollback because cid of value 1 which I am entering into the fintrans table does not exist in the customer table. However, this is not the case. Instead I am getting the following exception and the entry is still being made in the fintrans table:


Code:
java.lang.IllegalStateException: Transaction is not active in the current thread.
   com.sun.enterprise.distributedtx.J2EETransactionManagerImpl.validateTransactionManager(J2EETransactionManagerImpl.java:877)
   com.sun.enterprise.distributedtx.J2EETransactionManagerImpl.rollback(J2EETransactionManagerImpl.java:1106)
   com.sun.enterprise.distributedtx.J2EETransactionManagerOpt.rollback(J2EETransactionManagerOpt.java:412)
   com.sun.enterprise.distributedtx.UserTransactionImpl.rollback(UserTransactionImpl.java:209)
   springapp2.controllers.OutputController.handleRequest(OutputController.java:111)
   org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:44)
   org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:717)
   org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:658)
   org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:392)
   org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:347)
   javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
   javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
   com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:73)
   com.sun.enterprise.web.VirtualServerPipeline.invoke(VirtualServerPipeline.java:120)
   org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:231)
   com.sun.enterprise.web.connector.grizzly.ProcessorTask.invokeAdapter(ProcessorTask.java:667)
   com.sun.enterprise.web.connector.grizzly.ProcessorTask.processNonBlocked(ProcessorTask.java:574)
   com.sun.enterprise.web.connector.grizzly.ProcessorTask.process(ProcessorTask.java:844)
   com.sun.enterprise.web.connector.grizzly.ReadTask.executeProcessorTask(ReadTask.java:287)
   com.sun.enterprise.web.connector.grizzly.ReadTask.doTask(ReadTask.java:212)
   com.sun.enterprise.web.connector.grizzly.TaskBase.run(TaskBase.java:252)
   com.sun.enterprise.web.connector.grizzly.WorkerThread.run(WorkerThread.java:75)


If anyone can provide any help on the matter that would be greatly appreciated.

Outlaw.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 23, 2006 1:36 am 
Newbie

Joined: Fri Aug 11, 2006 4:12 am
Posts: 13
Hi ,
Have you set the factory class for the Hibernate Transaction API to JTA in your Hibernate.cfg.xml Or Hibernate.properties .

Rgds,
Alok


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 23, 2006 1:41 am 
Regular
Regular

Joined: Fri Aug 18, 2006 2:40 pm
Posts: 51
Location: Metz, France
http://forum.hibernate.org/viewtopic.php?t=963800

_________________
Denis
Don't forget to rate ... thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 23, 2006 1:52 am 
Newbie

Joined: Fri Aug 11, 2006 4:12 am
Posts: 13
Hibernate can automatically bind the "current" Session to the current JTA transaction. This enables an easy implementation of the session-per-request strategy with the getCurrentSession() method on your SessionFactory:

try {
UserTransaction tx = (UserTransaction)new InitialContext()
.lookup("java:comp/UserTransaction");

tx.begin();

// Do some work
factory.getCurrentSession().load(...);
factory.getCurrentSession().persist(...);

tx.commit();
}
catch (RuntimeException e) {
tx.rollback();
throw e; // or display error message
}


Hope this Helps ..
P.S. Do not forget the CREDITS ...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 23, 2006 1:53 am 
Newbie

Joined: Fri Aug 11, 2006 4:12 am
Posts: 13
The "current session" refers to a Hibernate Session bound by Hibernate behind the scenes, to the transaction scope. A Session is opened when getCurrentSession() is called for the first time and closed when the transaction ends. It is also flushed automatically before the transaction commits. You can call getCurrentSession() as often and anywhere you want as long as the transaction runs. To enable this strategy in your Hibernate configuration:

set hibernate.transaction.manager_lookup_class to a lookup strategy for your JEE container
set hibernate.transaction.factory_class to org.hibernate.transaction.JTATransactionFactory


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 23, 2006 10:08 pm 
Newbie

Joined: Thu Aug 03, 2006 6:54 pm
Posts: 16
Thanks to everyone for their advice. However, I still have'nt been able to solve my problem. I have looked over my configuration file but I can't find anything wrong with it:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-configuration PUBLIC
       "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
   <session-factory>
      <property name="connection.url">zzzzzzz</property>
      <property name="connection.username">zzzzzz</property>
      <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
      <property name="dialect">org.hibernate.dialect.MySQLMyISAMDialect</property>
      <property name="connection.password">zzzzz</property>
      <property name="connection.pool_size">10</property>

      <property name="transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>

       <property name="current_session_context_class">org.hibernate.context.JTASessionContext</property>

   
      <property name="transaction.manager_lookup_class">org.hibernate.transaction.SunONETransactionManagerLookup</property>

      <property name="jta.UserTransaction">java:comp/UserTransaction</property>

       <!-- this will show us all sql statements -->
       <property name="hibernate.show_sql">true</property>
   
       <!-- this will create the database tables for us -->
       <property name="hibernate.hbm2ddl.auto">update</property>
          
   </session-factory>
</hibernate-configuration>


If anyone can provide help on the matter that would be much appreciated.

Outlaw.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 24, 2006 11:09 pm 
Beginner
Beginner

Joined: Tue Aug 08, 2006 11:53 am
Posts: 37
I had the same problem. The solution is to explicitly open the session before calling getCurrentSession

http://forum.hibernate.org/viewtopic.ph ... highlight=


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.