java - Overriding transactional Annotation Spring + hibernate -


i have dao:

@transactional("transactionmanager") public class dao{      public void save(string a){...} } 

i have class:

public class test{ ... @transactional(rollbackfor = exception.class, propagation = propagation.requires_new)     public void save(){         dao.save("a");         dao.save("b");        }     } 

i want "save" method rollback when throws exception, doesn't seem work when exception occurs not rollback, proper approach this? other methods in dao transactional. there way can override transactional settings of override?

edit: have updated be, , still not working when throwing exception:

public class test{ ...      public void save(){         service.test(a,b);        }     }      public class service{     @transactional(rollbackfor = exception.class, propagation = propagation.requires_new)         public void testsave(object a, object b){             dao.updateentry(a);             dao.updateentry(b);          }     } 

remove transactional annotation dao layer , place transactional annotation in service layer. take @ code:-

@transactional @service public class service {      @autowired     private dao1 dao1;      @autowired     private dao2 dao2;      public dao1 getdao1() {         return dao1;     }      public void setdao1(dao1 dao1) {         this.dao1 = dao1;     }      public dao2 getdao2() {         return dao2;     }      public void setdao2(dao2 dao2) {         this.dao2 = dao2;     }      public void insertdata(){         dao1.insert1();         dao2.insert2();     } 

in above code, if dao2.insert2() fails dao1.insert1() rollback.

in case when have multiple methods in service class different transaction properties :
can define @transactional annotation on public methods below rule:-

when using proxies, should apply @transactional annotation methods public visibility. if annotate protected, private or package-visible methods @transactional annotation, no error raised, annotated method not exhibit configured transactional settings.

link1: transactional annotation on whole class + excluding single method

transaction support configuration setup:-

1) spring-config.xml

<beans xmlns="http://www.springframework.org/schema/beans"     xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:context="http://www.springframework.org/schema/context"     xmlns:tx="http://www.springframework.org/schema/tx"     xsi:schemalocation="http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans.xsd     http://www.springframework.org/schema/context     http://www.springframework.org/schema/context/spring-context.xsd      http://www.springframework.org/schema/tx         http://www.springframework.org/schema/tx/spring-tx.xsd">        <context:component-scan base-package="com.concept" />      <tx:annotation-driven transaction-manager="txmanager"/>        <bean id="txmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager">         <property name="datasource" ref="datasource"/>       </bean>       <bean id="datasource"         class="org.springframework.jdbc.datasource.drivermanagerdatasource">         <property name="driverclassname" value="oracle.jdbc.driver.oracledriver" />         <property name="url" value="" />         <property name="username" value="" />         <property name="password" value="" />     </bean>  </beans> 

Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -