笔记:
在用spring aop事务管理的时候,出现了回滚不成功而且控制台还报错的问题,
而配置文件什么的都是正确的,
找了两天之后,发现了自己之前在action里面用的是ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");来加载配置文件。
而在service里面用的是实现一个接口(这个接口声明了public static final ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");)来直接使用里面的参数,所以没有回滚。
后来改了都是实现接口来获取context,成功回滚了(不知道为什么,试图两个类都用
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");来加载配置文件时启动服务器时貌似一直在循环,最后提示无法在45秒内开启))
而控制台报错还是没解决,不过发现我用的是故意插入错误的数据到数据库才会报错,而空指针之类的就不会,不懂其中原理,也找不到答案。
现在我把下一步要做什么业务逻辑放在了action里面,而且由action来获取异常,让action跳转(之前是直接把request和response交给service去处理)
<!-- 配置Hibernate 的事务管理'-->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置事务特性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="*" propagation="REQUIRED" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 事务管理 -->
<aop:config>
<aop:pointcut id="allManagerMethod" expression="execution (* com.atm.service.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"/>
</aop:config>