-->
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.  [ 9 posts ] 
Author Message
 Post subject: transaction never commited even when tx.commit is called
PostPosted: Thu Nov 11, 2004 7:24 pm 
Newbie

Joined: Thu Oct 21, 2004 8:13 am
Posts: 8
Hi there,
I am using hibernate in stateless session beans. i have got a problem where a tx is started and it will do occasional commits in a loop within the same single session. however, during a test, i explicity rollback the tx(after a few commits) . i did a check with the database and realized that whatever i have "commited", never did get stored into the database at all. however, when i removed the tx.rollback code, all information was stored to the database.

i am using standard transaction code such as below but with the red color parts added in to test the tx.commit():

Session sess = factory.openSession();
Transaction tx;
try {
tx = sess.beginTransaction();

for(int i=0;i<5;i++){
//do some work
...

if(i==4)
tx.rollback();
else
tx.commit();


}
catch (Exception e) {
if (tx!=null) tx.rollback();
throw e;
}
finally {
sess.close();
}

I have tried this in a standalone application(outside stateless session bean) and it works but not in an ejb? does this make sense?

Another thing i need to clarify is that , is the transaction that is started for a stateless session bean somehow related to the transaction that is being used by hibernate in stateless session beans? I am using hibernate as an service mbean in jboss. its configuration is the same as recommended somewhere in the hibernate docs for installing hibernate as a jboss service

Can someone pls clarify both issues? it would be helpful for other newbies as well.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 11, 2004 8:25 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
The container is controlling the transactions so within an SLSB it is assumed that commit will be performed unless a tx roleback hint is provided. The transaction operation is normally performed when the call exists the SLSB hence at the end of the transaction boundary.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 11, 2004 10:11 pm 
Newbie

Joined: Thu Oct 21, 2004 8:13 am
Posts: 8
so are you saying that when i call tx.commit() in a SLSB, the object doesnt gets stored in the database because it is not yet the end of the transaction boundary?

another thing, if i call a single tx.rollback on the hibernate transaction object, it rollbacks the entire transaction for the single call to the SLSB method? is that why all my previous commits never get commited at all, just because of a single tx rollback()?

if the above line is true, how do i get hibernate to store the information to the database for each loop? instead of storing them to the database one shot at the end of the SLSB method call?

"The container is controlling the transactions so within an SLSB it is assumed that commit will be performed unless a tx roleback hint is provided. The transaction operation is normally performed when the call exists the SLSB hence at the end of the transaction boundary."


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 13, 2004 6:17 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Under CMT yes since the container is controlling the transaction not you.

Yes it rolls back every operation with the transaction scope, eg, from when the SLSB was entered to when it exits.

You need to re-organise you code, there are lots of options.
1) Make individual calls to the SLSB one for each loop.
2) Call a Local SLSB (from the remote) with the forced new transaction sematics.
3)... there are other options.


Top
 Profile  
 
 Post subject: SLSB transaction semantics
PostPosted: Sat Nov 13, 2004 10:23 am 
Beginner
Beginner

Joined: Mon Sep 08, 2003 10:56 am
Posts: 35
I just want to double check something. The container automatically commits transactions, when the end of the SLSB method is called. Do you still have to to grab hold of the transaction if you need to rollit back, on say a hibernate error?

Code:
Transaction tx = sess.getTransaction();

try{
   //something with hibernate
} catch(HibernateException e){
    tx.rollback();
}



The container won't handle the rollback automagically because you got an exception right? Are you supposed to obtain the transaction through the hibernate session (JBoss - Hibernate integration code)?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 13, 2004 10:36 am 
Newbie

Joined: Thu Oct 21, 2004 8:13 am
Posts: 8
Thank you very much david, it is very much appreciated :).


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 13, 2004 10:48 am 
Newbie

Joined: Thu Oct 21, 2004 8:13 am
Posts: 8
Hello destr0,
yes, you will have to call tx.rollback() on a hibernate error, its more of a flag to tell the container that this transaction is marked for rollback when the SLSB method exits. i believe the code you meant to write in your previous post is

"tx = sess.beginTransaction();"
not
"tx = sess.getTransaction();"

Another thing question that raised in my mind when i read destr0 post was that if i didnt call tx.commit() or tx.rollback() on the hibernate tx object, would the container still commit for me the transaction?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 13, 2004 10:49 am 
Newbie

Joined: Thu Oct 21, 2004 8:13 am
Posts: 8
Please correct me if i am wrong, david


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 13, 2004 7:25 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
The transaction will get commited automatically unless you provide the hint to allow the container to collback the transaction. The hint can be provided by the transaction API or directly through the containers context using the EJB API.


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