-->
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: How to obtain two separate JTA transactions in same thread
PostPosted: Sun Jan 22, 2006 12:57 pm 
Regular
Regular

Joined: Tue Oct 26, 2004 3:54 pm
Posts: 60
I'm using JTA in my Hibernate application, and following the reference docs everything is working fine within JBoss.

I'm wondering, though, how can I use two separate transactions (EG: bound to separate connections, so that commit() on one doesn't affect the other) within the JTA environment.

Reading JTA docs I see that the UserTransaction is bound to the current thread, so there can be only one - I guess.

I need to the following :

- Begin TX1
- Do some stuff in TX1
- Obtain a value from the DB which needs updating a row, and must be done ANYWAY, even if TX1 fails, to prevent duplicate value. So i should start TX2, obtain the value, update, commit TX2.
- Do other stuff in TX1
- Commit TX1

I can't isolate TX2. I know that in EJB a REQUIRES_NEW concept exists. How can I replicate the same behaviour with Hibernate/JTA *without* using EJBs ?

Thanks in advance.

Giulio


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 22, 2006 2:03 pm 
Senior
Senior

Joined: Tue Aug 23, 2005 8:52 am
Posts: 181
I dont think its possible. From Reading the Javadocs and the source on Hibernate's JTATransaction, it looks like Hibernate will create one if its not there already and if there is one, it would just use it.
If you dont want to use EJBs, you can probably use Spring and use its AOP-ized approach to have a transaction for your Transaction2.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 22, 2006 3:42 pm 
Regular
Regular

Joined: Tue Oct 26, 2004 3:54 pm
Posts: 60
Thanks for the reply.

I resolved, temporarily maybe, the issue this way :

- for TX1 I use getCurrentSession(), which binds to the current JTA transaction;
- for TX2 I explicitly ask SessionFactory to provide a new Session with openSession(), which should live on a separate Connection. Then I obtain TX2 with beginTransaction() on this Session.

It should work consistently.

I hope I can avoid Spring for now, I don't like the verbosity of its XML configuration.

Thanks,

Giulio


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 23, 2006 2:28 am 
Newbie

Joined: Fri Dec 03, 2004 8:21 am
Posts: 8
Hi,

Though the issue has been resolved, we don't know whether it is the right way to do it. It would be beneficial for everyone, if someone from Hibernate responds to the problem.

From the JTA perspective, each lookup for a remote/local Bean are committed or rolled back based on the transaction configuration in ejb-jar.xml (Required, RequiresNew, etc)

If a transaction is nested within another transaction, it should behave as per the JTA rules.

Now the question is: Will the hibernate uses same connection though it is a nested transaction? If yes can it rollback the inner one (based on the transaction configuration), though the outer transaction is committed?

Do we have to do anything explicitly?

Best Regards.

_________________
joshi


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 23, 2006 3:21 am 
Regular
Regular

Joined: Tue Oct 26, 2004 3:54 pm
Posts: 60
Hi, joshi

In my solution the answer is "no", the nested transaction is on a separate connection and a failure to commit on that connection won't affect the main transaction, unless I programmatically do so. JTA is not fully involved.

I don't think it's the "right" way to do my job, but it's as good as working without EJBs.

The *best* way is, in my opinion, to use EJBs and declarative transaction demarcation.

But to implement that I'm really waiting for EJB 3.

Giulio


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 23, 2006 7:30 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
you can use suspend()/resume() on the JTA transaction manager.
This has nothing to do with hibernate though
Check org.hibernate.engine.transaction.Isolater$JtaDelegate

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 23, 2006 7:45 am 
Regular
Regular

Joined: Tue Oct 26, 2004 3:54 pm
Posts: 60
That's very interesting Emmanuel, thank you.

The JTA TransactionManager is exposed in JNDI, if I remember well.

I'll try this immediately.


Giulio


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.