java - Spring Transaction - automatic rollback of previous db updates when one db update failes -


i writing simple application (spring + hibernate + postgresql db). trying construct sample object , persist in db.

i run simple java class main method have loaded applicationcontext , have got reference service class below

testservice srv = (testservice)factory.getbean("testservice");   

application context - context :

<bean id="transactionmanager"     class="org.springframework.orm.hibernate3.hibernatetransactionmanager">     <property name="sessionfactory" ref="sessionfactoryvsm" /> </bean>  <bean id="testservice" class="com.test.service.testserviceimpl">     <property name="testdao" ref="testdao"/> </bean>  <bean id="testdao" class="com.test.dao.testdaoimpl>     <property name="sessionfactory" ref="sessionfactoryvsm"/> </bean> 

in testservice have injected testdao. in test service method have constructed employee objects emp1 , emp2 , calling dao twice update.

testdaoimpl code:

public void saveorupdate(basedomainmodel baseobject) {      session session = null;     try {         session = gethibernatetemplate().getsessionfactory().opensession();         session.saveorupdate(baseobject);         session.flush();     } catch (exception e) {         logger.error("generic dao:saveorupdate::" + e);         e.printstacktrace();     } {         if (session != null) {             session.close();         }     }  } 

when emp2 update fails emp1 should fail. how do that. please advice

thanks in advance

updated :

thanks nanda. tried declarative transaction. not working. emp1 gets persisted , not rolled eveb second dao call fails. have added transaction advice method.

to test if transaction advice applied or not changed propagation "not_supported". still emp1 gets persisted. expectation should have got transaction not supported type of exception. please advice .

updated

@seanizer - update. have tried adding
@transactional(propagation=propagation.not_supported) public void saveemp(employee emp) service method. didn't work. iterating collection hold if need call 1 dao. if in case have call 2 different dao persist obj1 , obj2- may not help. check if transaction getting applied @transactional(propagation=propagation.not_supported). still obj1 got persisted. doubt if xml configuration/ annotation given correct. please check

<bean id="txmanager"         class="org.springframework.orm.hibernate3.hibernatetransactionmanager">         <property name="sessionfactory" ref="sessionfactoryvsm" />     </bean>  <tx:advice id="txadvice" transaction-manager="txmanager">     <tx:attributes>          <tx:method name="saveemp" propagation="required" rollback-for="exception"/>         <tx:method name="*"/>     </tx:attributes>  </tx:advice>     <aop:config>     <aop:pointcut id="testserviceoperation" expression="execution(*com.test.service.*(..))"/>     <aop:advisor advice-ref="txadvice" pointcut-ref="testserviceoperation"/>   </aop:config>  

i using org.springframework.orm.hibernate3.hibernatetransactionmanager transactionmanager. correct ?


updated

i have created exception class myruntimeexp extending runtimeexception , throwing same dao method service method. still rollback not happening. doubt if have correctly given configurations in applncontext.xml. can me how check if transaction advice / annotation being applied method or not? there way of running in debugging mode , check

issue :

i using

session = gethibernatetemplate().getsessionfactory().opensession(); 

but should current session , working fine.

session = gethibernatetemplate().getsessionfactory().getcurrentsession(); 

if use declarative transaction management, can lose of boilerplate:

testdaoimpl:

private sessionfactory sessionfactory;  public void setsessionfactory(sessionfactory f){     this.sessionfactory = f; }  public void saveorupdate(basedomainmodel baseobject) {     session session = sessionfactory.getcurrentsession();     session.saveorupdate(baseobject); } 

and can control transaction handling service layer using @transactional (or xml configuration)

testserviceimpl:

private testdao testdao;  public void settestdao(testdao  d){     this.testdao = d; }  @transactional // 1 transaction multiple operations public void someservicemethod(collection<basedomainmodel> data){      for(basedomainmodel baseobject : data)          testdao.saveorupdate(baseobject); } 

reference:


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -