PDA

View Full Version : PROPAGATION_REQUIRES_NEW and concurrent threads


javajunky
06-02-2007, 09:43 AM
Hi,
This question relates to post (http://forum.springframework.net/showpost.php?p=7332&postcount=9 ) but I'm posting in here as I don't *think* its a spring NHibernate issue any more (I did previously). I think it is either something stupid I'm doing, or a Spring.Data... issue

I've got a singleton DAO, and singleton Service to which the DAO is injected. until recently all my service methods were marked as PROPAGATION_REQUIRED, but I've just had the need to mark one of my methods as PROPAGATION_REQUIRES_NEW (to avoid some nasty table locking issues around a web-service call), *however* since I've made this change my application has become very unstable, and I repeatedly see the exception displayed in the post referred to above.

I've now isolated it (I think) so that it only occurs when there is a concurrent access to the Service/DAO tier at the same time as the transaction that is meant to be being closed off (as per PROPAGATION_REQUIRES_NEW).

Should what I'm trying to do, be legal, is anyone else using REQUIRES_NEW in a heavily multi-threaded environment (my app tier is a web-service tier, which can also call out to other web-service tiers, sometimes back into itself!)

Many thanks, I really hope this isn't considered a cross-post, because I don't believe it to be, as this post reflects a greater understanding of the issue on my part!
- Sorry

Erich Eichinger
06-06-2007, 12:01 AM
Hi,

you mentioned spawning workerthreads. When and how do you spawn those threads? Using ThreadPool.QueueUserWorkItem()?

Do you maybe spawn them, *after* a nh-session or transaction has already been created?

An active nh-session is stored in a TLS-like structure Spring.Threading.LogicalThreadContext (see implementations of IThreadStorage for details) - after spawning the thread of course the session isn't available on the new thread.

-Erich

javajunky
06-06-2007, 08:54 AM
Hi,

you mentioned spawning workerthreads. When and how do you spawn those threads? Using ThreadPool.QueueUserWorkItem()?

Do you maybe spawn them, *after* a nh-session or transaction has already been created?

An active nh-session is stored in a TLS-like structure Spring.Threading.LogicalThreadContext (see implementations of IThreadStorage for details) - after spawning the thread of course the session isn't available on the new thread.

-Erich

I think that the worker-threads causing a problem are spawned from an init-method in a spring object. The init-method does not have a transaction.

I'm a little confused however, if a transaction (PROPAGATION_REQUIRED) begins on one thread, and then another thread is spawned, that calls methods on a service object marked as (PROPAGATION_REQUIRED) won't a new transaction be started for the new call on the spawned thread, why would it matter that it was spawned from an existing thread mid transaction ? [Please, please pardon my ignorance!]