I have come across some behavoir with Hibernate transactions that I do not understand. I would expect a transaction rollback to invalidate a session.save, but it do not seem to. If I do the code below:
Session sess = factory.getSession();
Transaction tx = null;
try {
tx = s.beginTransaction();
MappedObject obj = new MappedObject();
sess.save( obj );
throw new Exception();
tx.commit();
}
catch (Exception e) {
tx.rollback();
throw e;
}
finally {
sess.close();
}
I end up with obj in the database. Now if obj was already in the database, and I am just updating a value, then the rollback works as expected.
Am I missing something? I assume a transaction rollback puts the database in the same state it was before the transaction started.
thanks,
-tao starbow
|