views:

266

answers:

1

I have one process which creates a database entity and then launches a second process. It then waits on the second process to find and update the database entity before completing its processing, and thereby committing the database entity. The trouble seems to be that since the initial process which performed the entity creation has not committed the database entity by the time the second process tries to find the entity (which it can't find), the first process never completes because the second process can't complete, and things are goobered.

Some context: the first process creates an entity, launches a second process on an external machine, and sets the entity status to STARTED. The second process on the external machine makes a web service call and this web service finds the entity and updates the entity's status to READY. The first process has a loop which checks the status of the entity and once it has been changed from STARTED to READY then it does additional processing and completes. However the second process is never able to find the entity (I think) since it is never committed from the Hibernate session where it was created in the first process which has not completed by the time the second process attempts to find the entity.

What is a better way to do this so that this sort of thing won't happen? Is there a way to commit the transaction mid-way, immediately before the second process is launched, in order to have the entity present in the database for the second process to find?

Thanks for your suggestions, etc.

A: 

Hibernate sessions aren't even thread safe, let alone surviving this sort of thing. You need to use a session for a single thread's single unit of work, and in this case you have three - object creation, whatever the other process is doing, then object updating at the end. You need to use (and flush) separate sessions for all three.

David M
I'm flushing after original entity creation but the same session is not shared between the two processes which are accessing the database, and it seems that until the first process's transaction has completed the entity is not persisted and available to the second process.
James Adams
That's what you'd expect though with the transaction isn't it?
David M
What I mean is that the entity won't be visible outside of the transaction until the transaction is committed (unless you adjust the transaction isolation level accordingly). You will have to commit after step 1 of 3.
David M