Hibernate version: 3.2.6
Spring version: 2.5.3
Our product works on a batch of users to run a particular unit of work on each user. We envision a design were we are using the Hibernate batch load process (batch-size="10" specified in the .hbm) to retrieve a batch of say 10 users and then pass it to our "worker" that will then do whatever needs to be done.
Now, in order for this to work right, I have an outer transaction which does the loading of the user and makes any changes on the user that might need to be done prior to the call to the worker. Once the user is created, he is then passed to the worker and run within its own transaction boundary (a new transaction - PROPAGATION_REQUIRES_NEW). The idea being that if there was a failure with the worker's transaction boundary, then we can rollback any updates to the user inside the worker. Note, I do not want to have to rollback ALL the users in my batch in the event of a failure on one user's processing. Obviously, this also implies that the completion of my worker should save any updates to the user.
My problem is this: Since the user was "loaded" as a batch, the outer transaction (and hence, session) is what is managing the user entity. So, in order for my worker's rollback (and flush, for that matter) to act on the user entity, I find myself having to evict() the user out of the original transaction once I hand over control to my worker (so that the outer transaction does not override my worker changes) and have the worker explicitly merge() the user back so as to be managed by my worker transaction. I have this working with code, but I have concerns about this. The entire reason for this code is because I started a new transaction - which is configured via spring XML, but here I have had to code in the implications of the new transaction creation.
Have others faced this delimma and if so, how have you solved it?
I am toying with the idea of using an interceptor to do this and have it configured via spring when I set up the transaction for a worker.
Madhuri
|