-->
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.  [ 2 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:24 am 
Regular
Regular

Joined: Fri Aug 18, 2006 2:40 pm
Posts: 51
Location: Metz, France
I guess you are not supposed to access userTransaction directly
but instead

Code:
<property name="connection.datasource">my/first/datasource</property>
....
<property name="transaction.factory_class">
            net.sf.hibernate.transaction.JTATransactionFactory
</property>
<property name="jta.UserTransaction">java:comp/UserTransaction</property>


Code:
Session session = HibernateUtil.currentSession();
Transaction tx= session.beginTransaction();

Cat princess = new Cat();
princess.setName("Princess");
princess.setSex('F');
princess.setWeight(7.4f);

session.save(princess);
tx.commit();

HibernateUtil.closeSession();


Take a look here:
http://www.hibernate.org/hib_docs/refer ... -xmlconfig

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


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