How to maintain an object for two nHibernate sessions?

I am creating a multi-threaded system that works as follows:

While there are objects:

  • Retrieves an entity from nHibernate (using the current session)

  • Starts a new thread that will work with this entity *

When I start this new thread, it should have a new session, because nHibernate is not thread safe. I create it, but the object received earlier does not work for this session.

Today I allow this situation to fetch a new Entity passing id from nHibernate. But it’s expensive, and I’m trying to save time to reach my SLA.

Is there a way to associate this object with this new session without having to make a new database call? Another session will not be closed, they will all be open until the end of the application.

+5
source share
3 answers

If you work with individual objects, you will have to bind them to the session. You can do this if you have the correct Hibernate identifiers for the objects you are working with by calling get, and then merging your copy with one Hibernate just entered into the session. Make sure you use merging because saveOrUpdate () will not remove any children that are not in the deleted object, just add new children and save the changes for existing children.

+4
source

Evict + Lock 2: nd . , , , , .

+2

I disconnected it from the first thread, after joining another thread.

+1
source

All Articles