-->
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: Sharing transaction in multiple Threads (and Sessions)
PostPosted: Tue Apr 22, 2008 5:02 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hi all,
In this scenario:
- A CPU with N cores
- some CPU-bound operation that can easily be split in N parallel jobs
- need for repeatable-read or serializable transactions

I would love to split the job to make use of multi-threading, but I have some transactional concerns: each thread needs to open it's own thread, but I would like them to share the same transaction.
I imagine something like XA transaction semantics could be used for commits (all participants on same DB), but just for simplicity imagine my expensive N jobs are doing "read-only work".
What I would like to guarantee is that the N threads are all working on the same data:
if any other thread alive commits/flushes some change to DB I want to see all changes
either before my N threads begun transaction, or after my N threads:
I want to ensure that if any thread works on entity1 this will contain the same information that other threads are receiving.

I was thinking about some locking mechanism to be build in the sessionfactory to permit opening a N-list of sessions and let other treads wait, but locks need to be owned by one single thread, and sessions can't be opened by one thread to be handed over to another one.. right? But:
A)I've seen some ThreadLocal s in the source... I would need to share some data.
B)I still am not sharing a database transaction, so some other service not using my locked down SessionFactory could break my design.

Did somebody address this problem?
Would it be possible to use something like a single DB transaction shared among different Sessions?

I would love some api as
Code:
SessionProducer sp = SessionFactory.getNSessionsTicket(int n); //call once, transaction begins (not lazily)
Session session = sp.getASession(); //each thread gets one

This "SessionProducer" is the only one needing ThreadSafety, but when it is build in the SessionFactory all special session datastructures should be replicated in the particular ThreadLocals (I could get this effect overriding the initialize method in ThreadLocal).
Maybe some of these structures could need sharing (and consequently being guarded for concurrency)

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 23, 2008 3:16 am 
Regular
Regular

Joined: Mon Aug 06, 2007 10:49 am
Posts: 67
Location: Banska Bystrica, Slovakia
we use spring to get transaction running across threads

PlatformTransactionManager -
http://static.springframework.org/sprin ... nager.html


TransactionSynchronizationManager -http://static.springframework.org/spring/docs/2.0.x/api/org/springframework/transaction/support/TransactionSynchronizationManager.html

cant give u more info cause a dont`t know how exactly it works :)
but maybe this can help you


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 23, 2008 4:11 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
thanks a lot, I don't want to add spring as a dependency but
at least I know it is possible, and I can look at the sources :-)

_________________
Sanne
http://in.relation.to/


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.