`

小结:用Spring AOP配置事务要注意的几项

阅读更多
Spring AOP形式管理事务,Spring的官方文档写得不全,容易漏配,特总结如下:
1,数据源要加上数据源事务代理

<!-- 默认的数据源配置 -->
<bean id="talent.defaultDataSourceTarget"
	class="org.springframework.jdbc.datasource.DriverManagerDataSource">
	<!-- org.apache.commons.dbcp.BasicDataSource -->
	<!-- org.springframework.jdbc.datasource.DriverManagerDataSource -->
	<property name="driverClassName"
		value="${jdbc.default.driverClassName}"/>
	<property name="url" value="${jdbc.default.url}"/>
	<property name="username" value="${jdbc.default.username}"/>
	<property name="password" value="${jdbc.default.password}"/>
</bean>
<!-- 数据源代理 -->
<bean id="talent.defaultDataSource" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">   
	<constructor-arg>
		<ref bean="talent.defaultDataSourceTarget" />
	</constructor-arg>   
</bean>

2,事务特性配置时,要注明rollback-for类型,并不是所有的异常都回滚的
<!-- 配置事务特性 -->
<tx:advice id="serviceAdvice"
	transaction-manager="talent.defaultTransactionManager">
	<tx:attributes>
		<tx:method name="add*" propagation="REQUIRED" rollback-for="Throwable"/>
		<tx:method name="save*" propagation="REQUIRED" rollback-for="Throwable"/>
		<tx:method name="insert*" propagation="REQUIRED" rollback-for="Throwable"/>
		<tx:method name="del*" propagation="REQUIRED" rollback-for="Throwable"/>
		<tx:method name="update*" propagation="REQUIRED" rollback-for="Throwable"/>
		<tx:method name="*" read-only="true"/>
	</tx:attributes>
</tx:advice>

3,配置哪些类的方法需要进行事务管理时,表达式要写对
<!-- 配置哪些类的方法需要进行事务管理 -->
<aop:config proxy-target-class="true">
    <aop:pointcut id="servicePointcut" expression="execution(* com.jstrd.talent.manager.*.*(..))"/>
    <aop:advisor pointcut-ref="servicePointcut" advice-ref="serviceAdvice"/>
</aop:config>

此处只对com.jstrd.talent.manager包下的类进行管理,并不会对其子包也进行管理的
4,要通过ctx.getBean("beanName")的形式来获取管理类,而不是new一个管理类出来
分享到:
评论
4 楼 Angi 2011-02-16  
UserDaoImpl:
public class UserDaoImpl extends SqlMapClientDaoSupport implements IUserDAO {

public void saveUser(User user) {
getSqlMapClientTemplate().insert("insertUser", user);
}

}
UserService:
public class UserService implements IUserService {

private IUserDAO userDao;

public void doTransaction() {
User user = new User();
user.setName("ppp");
user.setSex(1);
userDao.saveUser(user);
                //下面这个插入是会报错的
User user1 = new User();
user1.setName("ggg");
user1.setSex(1);
userDao.saveUser(user1);
}

public IUserDAO getUserDao() {
return userDao;
}

public void setUserDao(IUserDAO userDao) {
this.userDao = userDao;
}

}
3 楼 Angi 2011-02-16  
还有一点,我使用如下的声明式事务是没问题的。
<bean id="userServiceProxy"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">

<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="target">
<ref local="userService" />
</property>
<property name="transactionAttributes">
<props>
<!-- 这里的方法签名可以精确到方法, 先懒惰一下全配置上 -->
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
2 楼 Angi 2011-02-16  
我的Spring的配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">

<bean id="dataSourceProxy"
class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
<constructor-arg>
<ref bean="dataSource" />
</constructor-arg>
</bean>
<!-- DataSource -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<!--<property name="defaultAutoCommit" value="false"/>-->
<property name="url">
<value>jdbc:mysql://localhost/test</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>mysql</value>
</property>
</bean>

<!-- Spring iBatis Template -->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="SqlMapConfig.xml" />
<property name="dataSource" ref="dataSourceProxy" />
</bean>

<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSourceProxy" />
</bean>

<!-- 需要引入aop的命名空间 -->
<aop:config proxy-target-class="true">
<!-- 切入点指明了在所有方法产生事务拦截操作 -->
<aop:pointcut id="daoMethods"
expression="execution(* com.angi.ibatis.service.*.*(..))" />
<!-- 定义了将采用何种拦截操作,这里引用到 txAdvice -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="daoMethods" />
</aop:config>

<!-- 需要引入tx的命名空间 -->
<!-- 这是事务通知操作,使用的事务管理器引用自 transactionManager -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 指定哪些方法需要加入事务,这里懒惰一下全部加入,可以使用通配符来只加入需要的方法 -->
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>

<bean id="userDAO" class="com.angi.ibatis.dao.UserDaoImpl">
<property name="sqlMapClient">
<ref bean="sqlMapClient" />
</property>
</bean>

<bean id="userService" class="com.angi.ibatis.service.UserService">
<property name="userDao">
<ref bean="userDAO" />
</property>
</bean>
</beans>
1 楼 Angi 2011-02-16  
您好!我遇到跟您一样的问题(http://www.iteye.com/problems/6314)
参考你的方法用TransactionAwareDataSourceProxy包装,可是问题还是没有解决,请指教!

相关推荐

Global site tag (gtag.js) - Google Analytics