java - Hibernate session overlap -


I am using Hibernate to read / write data.

The situation is that I have 2 processes on a record. The process A opens a session, writes the database to a transaction in a transaction, then closes the session. After that, the process B opens a session immediately and tries to read the data written by Process A (in one transaction).

The process B is the problem I have read successfully, sometimes a process A. and sometimes the process B can not read the new record.

To solve the problem, in Process B, I try to stop the transaction that reads the data, waits for another, tries to open a new transaction, time, Data is successfully fetched !!! I do not really solve it

Can you explain the situation?

Update 1: Adding code

Code to process A:

session session = session Factory.open session (); Transaction tx = session.beginTransaction (); Person p = new person (); P.setID (1234); P.setName ("Truong Nguyen"); Session.save (p); Tx.commit (); Session.close (); // Here is the code to inform process B ... A data has been added // ...

code on the process B: Note: this code is only processed Is executed after code above i fully executed.

  session session = session Factory.open session (); Person p = new person (); P.setID (1234); Transaction tx = session.beginTransaction (); List results = session. Generation classification ("com.sample.Person") .add (example.create (p)) .list (); Log.debug ("Result Size:" + results.size ()); Tx.rollback ();  

Result is sometimes 0, sometimes 1, sometimes 1 happens, sometimes 1 happens. But if I change the code on Process B, it always gives 1.

  session session = sessionFactory .openSession (); Person p = new person (); P.setID (1234); Transaction tx = session.beginTransaction (); List results = session. Generation classification ("com.sample.Person") .add (example.create (p)) .list (); Tx.rollback (); If (results.size () == 0) {try {thread.sleep (1000); } Hold (exception e) {} tx = session.beginTransaction (); Result = Session. SafeCertia ("com.sample.Person") .add (e.g., create (P)) .list (); Tx.rollback (); } Log.debug ("Result Size:" + results.size ());  


Comments