1.spring配置文件中配置
<aop:config> <aop:pointcut id="appService" expression="execution(* com.hhr.service.*Service*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="appService" /> </aop:config> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="select*" read-only="true" /> <tx:method name="find*" read-only="true" /> <tx:method name="get*" read-only="true" /> <tx:method name="*" /> </tx:attributes> </tx:advice> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <tx:annotation-driven transaction-manager="transactionManager" />
2.调用
@Transactional(rollbackFor=Exception.class) public void test(){ update(obj) throw new Exception(); }
其中 1)test方法中不能加try catch语句
2)数据库表为innodb