Suppose code like this:
Code:
Session session = sessionFactory.openSession ();
try {
Transaction tx = session.beginTransaction ();
Person p = session.load (Person.class, id);
session.delete (p);
session.evict (p);
...
p = new Person (id); // same id as above
session.save (p);
tx.commit ();
} catch (Exception e) {
tx.rollback ();
}
finally {
session.close ();
}
If I omit the evict() call, I get a NonUniqueObjectException. If I include the evict() call I get org.hibernate.AssertionFailure: possible nonthreadsafe access to session. Only one thread has access to the session and mapped objects managed by the session. I'm specifying FlushMode.COMMIT to flush when a transaction commits.
Questions:
1) Some posters have used flush() instead of evict() as a solution, and indeed, it solves it for me too. How does flushing effect the transaction? If data is flushed, can it still be rolled back later in the same transaction?
2) Is Hibernate being overly agressive about the assertion? Is it safe to ignore it in this case?
Hibernate version: 3.0.2
Name and version of the database you are using: mysql