Hi everybody,
I have a test with two sessions, session1 and session2.
I have the simple following code (I minimize it so it will be much clear to read).
//Session1 session1 = HibernateUtil.openSession(); session1.beginTransaction(); Repository rpy = new Repository(key); session1.saveOrUpdate(rpy); session1.flush();
//Session2 session2 = HibernateUtil.openSession(); session2.beginTransaction(); Repository rpy = new Repository(key); session2.saveOrUpdate(rpy);
//commit and close session1 session1.getTransaction().commit(); session1.close();
session2.flush(); //should fail on uniqueConstrains ...
Repository rpy = new Repository(key); session2.saveOrUpdate(rpy); session2.flush(); //should NOT fail on uniqueConstrains (row already in the database)...
//commit and close session2 session2.getTransaction().commit(); session2.close();
session2 invoke saveOrUpdate(object) before session1 commit and close it transaction. So actually session2, make a select to see if it should insert, or update it's data. In that case since session1 still not committed, session2 will make insert after it make flush() and get org.hibernate.exception.ConstraintViolationException. Till now, everything is ok ... I want to check that in some test in our system.
But, after I commit with session1 and make the same operation with session2 (i.e saveOrUpdate(obj) and then flush()) I am expecting to get update operation on the object since it's already in the DB.
I am working with hibernate.
It works fine with Oracle DB, but not as expected with MySQL (i.e with Oracle I get only once ConstraintViolationException and it updating in the second time, but I am getting twice ConstraintViolationException when I am working infront of MySQL DB)
Do, someone, now the reason why and how can I solve it???
I tried to remove second-level-cache in hibernate configuration file, I tried to change the CacheMode of the session, unfortunately, nothing helped.
Can you please, advice?
Best regards,
Ofer Nagar
|