Hello,
i am having problems using a single transaction and having multuple commits and rollbacks with the rollbacks rolling back previous commits. I have even set the flush mode to commit but still the rollbacks rollback my previous commits. ok this is what i am trying to do:
get a list of Xobjects with hql
go thru each one of them in a loop
{
try{
create new Yobject
sess.save(Yobject)
tx.commit();
}catch(Exception ex)
{
tx.rollback();
}
try{
create new Zobject
sess.save(Zobject);
if(true)
throw new Exception("test")
tx.commit()
}catch(Exception ex)
{
tx.rollback();
}
}
this is just the summary of the code but as you can see, the second try catch block, i purposely throw an exception in order for the tx to rollback but i realize that the rollback also rollbacked my previous commit in a way that Yobject was not saved into the database. what i understand is that commit synchronizes the state with the database and i saw the sql to insert Yobject being displayed on my console. i have also tried to set flush mode to commit on session object but the Zobject rollback still undid the commit on Yobject. am i doing anything wrong here?
it is important for me to go through this loop and have a series of commits and rollbacks for a failover mechnism i am working on but it seems that the behaviour described above is not acceptable for my failover mechanism. should i be creating new transactions for each Yobject and Zobject so that when Zobject rollback is called, it will not affect the transaction of Yobject?
if that is the case, wouldnt that sound expensive to create 2 x transactions for each loop, well of course using the same session
Can anyone please shed some light ?
Thanks
|