Hello Guys,
Standard Transaction code is usually like this rite(as in the docs)?
Session sess = factory.openSession();
Transaction tx;
try {
tx = sess.beginTransaction();
//do some work
...
tx.commit();
}
catch (Exception e) {
if (tx!=null) tx.rollback();
throw e;
}
finally {
sess.close();
}
ok now, my question is : what if in the "//do some work" block, i create a new object that i want to be persisted when the transaction commits, but however, according to what i experienced, tx.commit only commits already persistent objects that have been modified in some way in the "//do some work" block. tx.commit, however, does not persist the new object that i have created. for that to happen, i will have to call session.save(). The problem is what if i have already called session.save on that new object and some where down the line of the transaction, an error occurs and whatever changes that i have made to the persistent object is rollback by tx.rollback() BUT my newly created object which is supposed to be part of the transaction isnt rollbacked cos it is already saved to the database!! what kind of transaction is this?! or am i missing out on something that i dont know??? can some guru please enlighten me ? a transaction has either be successful or not successful and all changes are made persistent or all rollbacked, but in this case, it seems that my transaction is a half baked one, allowing my newly created object to stay persistent in the database even when a tx is rollbacked?! what gives???
Best Regards
|