We've got a problem with stale data in session. We islolated example code - in short it's doing this:
Opens 2 sessions at start. In 1st session display actual count of persisted objects, starts transaction and save new object into database. Then in 2nd session(!) displays actual count again.
Problem is that 2nd session doesn't see changes made by 1st session and shows same count.
I happens only on MySQL server. Same code runs correct on Informix (it outputs count 0 and then 1).
We tried disable caches in hibernate and similar experiments which came on our minds. Nothig helped.
So any good advice makes us happy. Thanks.
Hibernate version: 2.1.7
Mapping documents: Not important, code bellow fails always. We can use some "Worker.hbm.xml" for example.
Code:
Code:
try {
SessionFactory sf = cfg.buildSessionFactory();
Session s1 = sf.openSession();
Session s2 = sf.openSession();
List lst = s1.find("from Obec");
System.out.println("1st count = " + lst.size());
Transaction tx = s2.beginTransaction();
Worker w = new Worker();
o.setName("John");
s2.save(o);
tx.commit();
s1.clear();
List lst2 = s1.find("from Obec");
System.out.println("2st count = " + lst2.size());
s1.close();
s2.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
Name and version of the database: MySQL 4.0.20
The generated output - copied from log messages:
DEBUG SQL:229 - select worker0_.id as id, worker0_.name as name from worker worker0_
1st count = 0
DEBUG SQL:229 - insert into worker(name, id) values (?, ?)
DEBUG SQL:229 - select worker0_.id as id, worker0_.name as name, from worker worker0_
2nd count = 0