-->
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.  [ 3 posts ] 
Author Message
 Post subject: Not rolling back for failed Transaction
PostPosted: Sat Nov 27, 2010 11:40 pm 
Newbie

Joined: Sat Nov 27, 2010 11:07 pm
Posts: 2
Hi All,
I am new to Hibernate, JBoss Application Server and even to EJB3.0.
I am using Hibernate3.X, JBoss Application Server 6, JDK 6.0, Oracle10g.

My goal is to use CMT with Hibernate. Problem is that, first update is not rolling back and persisted into database, even if second update failing.
I have following setting with hibernate.cfg.xml
<property name="connection.datasource">java:ADPWorldDS</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.CMTTransactionFactory</property>
<property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</property>
<property name="show_sql">true</property>
<property name="hibernate.connection.autocommit">false</property>
<property name="hibernate.current_session_context_class">jta</property>

ADPWorldDS is XA Datasource setup in JBossAS.

I have defined following annotation in stateless EJB
@TransactionManagement(javax.ejb.TransactionManagementType.CONTAINER)
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) [In persist method of EJB]

Hibernate Code
Configuration config = new Configuration();
config.configure();
SessionFactory sf = config.buildSessionFactory();
session = sf.getCurrentSession();
try {
session.saveOrUpdate(adpwUtlCountry);
session.saveOrUpdate(adpwUtlFileCategory);
}catch(Exception e){
e.printStackTrace();
throw e;
}
finally{
session.flush();
session.close();
}


As per above, my update in Green color is successfull, and Red color update is failing. Hence earlier transaction(green color) should be rolled back. But that's not happening and I found that row, updated in database. I think, I have done every thing as per document but still not able to figure out the problem. Could some one pls help me our here.

Pankaj


Top
 Profile  
 
 Post subject: Re: Not rolling back for failed Transaction
PostPosted: Mon Nov 29, 2010 5:44 am 
Beginner
Beginner

Joined: Fri Nov 26, 2010 8:25 am
Posts: 21
Hi Pankaj,

Rollback is not automatic if an application exception is thrown, this only happens for runtime (system) exceptions. You can mark the transaction for rollback by calling SessionContext.setRollbackOnly(). This will let the container know that the transaction should be rolled back. To do this, you need to inject a SessionContext in your bean class:

Code:
@Resource
private SessionContext sctx;


Then call
Code:
sctx.setRollbackOnly()
in your exception handler.

The stack trace for the exception should tell you which type of exception is begin thrown. Unless it is a subclass of RuntimeException, or RemoteException, then the rollback will not happen automatically.

Hope that helps,
Kate.


Top
 Profile  
 
 Post subject: Re: Not rolling back for failed Transaction
PostPosted: Mon Nov 29, 2010 8:27 am 
Newbie

Joined: Sat Nov 27, 2010 11:07 pm
Posts: 2
Hi Kate,
Thanks for showing the interest. However, above code started working [Or in otherwords, it was working at the time of posting the question as well]. It is some silly mistake I have done and hence got confused. This happens when you are trying many new things at a time :-)
Any way, Informatio you have given below, is new to me as well, and usefull. Let me try it.

Thanks a lot.
Pankaj


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