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.