rollback - best approach for roll back in hibernate -


i have web application built using struts2 + hibernate. there several entity bean class's represents database tables project. have situation 1 record dependent on record below code:

  dao.getincomeinstance.save(user user,income income);   dao.getbudgetinstance.update(budget budget,income income); 

each method has session.gettransaction().commit(); means committing transaction. irrespective of error in budget income record getting saved assuming not right approach. have approach in mind creating new method accepts parameter of transaction object budget method dao.getbudgetinstance.update(transaction trans,budget budget,income income); if there issues/error can roll , don't end creating multiple methods same name( around 10+ methods same name update accepts different parameters).

i new hibernate. can 1 suggest me of approach can create rollback point , operations , can rollback when ever want (some thing below. these assumptions pinpoint idea):

  rollback roll=null;   try{   roll=session.transaction.createrollback();   dao.getincomeinstance.save(user user,income income);   dao.getbudgetinstance.update(budget budget,income income);   }   catch(runtimeexception exe)   {    // rollback roll reference   } 

any suggestion better approach looking for.

transactions should not demarcated in dao methods, in business service method calls daos, single transaction encompasses related changes database, or none of them committed.

in modern enterprise code, transactions demarcated declaratively whatever dependency injection container happen use (spring, cdi, ejb, ...). spring, instance, 1 annotates business service make transactional:

@transactional public void whatever(user user, budget budget, income income) {     incomedao.save(user, income);     budgetdao.update(budget, income); } 

further reading: spring reference manual


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -