Spring(六)spring之事务管理

通过给账户赠送积分的例子引入事务的话题:

未使用事务:

账户加减积分Dao:

public interface AccountDao {
	void upScore(int score,int id);
	void downScore(int score,int id);
}
账户加减积分实现类:

public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {

	@Override
	public void upScore(int score, int id) {
		getJdbcTemplate().update("update t_user set score = score + ? where id = ?", score,id);
	}

	@Override
	public void downScore(int score, int id) {
		getJdbcTemplate().update("update t_user set score = score - ? where id = ?", score,id);
	}
}


账户赠送积分Service:

public interface AccountService {
	void sendScore(int senderId,int accepterId,int score);
}
账户赠送积分实现类:

public class AccountServiceImpl implements AccountService {

	private AccountDao accountDao;
	
	@Override
	public void sendScore(int senderId, int accepterId, int score) {
		accountDao.downScore(score, senderId);
		accountDao.upScore(score, accepterId);
	}

	public AccountDao getAccountDao() {
		return accountDao;
	}

	public void setAccountDao(AccountDao accountDao) {
		this.accountDao = accountDao;
	}
}

spring的配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">

	<context:property-placeholder location="classpath:db.properties" />

	<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
		<property name="driverClass" value="${jdbc.driverClass}"></property>
		<property name="user" value="${jdbc.user}"></property>
		<property name="password" value="${jdbc.password}"></property>
	</bean>

	<bean name="accountDao" class="com.milan.transaction.AccountDaoImpl">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<bean name="accountService" class="com.milan.transaction.AccountServiceImpl"> 
		<property name="accountDao" ref="accountDao"></property>
	</bean>
	
</beans>	


测试类:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext-transaction.xml")
public class TestDemo {

	@Resource(name="accountService")
	private AccountService service;
	
	@Test
	public void test(){
		service.sendScore(3, 4, 100);
	}
	
}

说明:测试id为3的账户给id为4的账户充值100积分,测试通过。(图略)

若sendScore方法过程报错,则会造成只减积分,没加积分的情况,故此添加spring事务处理


==========================================================

注意:以上代码没有添加事务
==========================================================

spring事务实现方式(一)


spring配置文件中添加:事务核心管理器,事务模板对象

	<!-- 事务核心管理器,依赖于连接池 -->
	<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<!-- 事务模板对象 -->
	<bean name="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
		<property name="transactionManager" ref="transactionManager"></property>
	</bean>

将账户Service实现类的sendScore方法添加事务处理:
public class AccountServiceImpl implements AccountService {

	private AccountDao accountDao;
	
	private TransactionTemplate template;
	
	@Override
	public void sendScore(int senderId, int accepterId, int score) {
		template.execute(new TransactionCallbackWithoutResult() {
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus arg0) {
				accountDao.downScore(score, senderId);
				int i = 1/0;
				accountDao.upScore(score, accepterId);
			}
		});
	}

	public AccountDao getAccountDao() {
		return accountDao;
	}

	public void setAccountDao(AccountDao accountDao) {
		this.accountDao = accountDao;
	}

	public TransactionTemplate getTemplate() {
		return template;
	}

	public void setTemplate(TransactionTemplate template) {
		this.template = template;
	}
}


spring事务实现方式(二)


spring配置文件:

	<!-- 事务核心管理器,依赖于连接池 -->
	<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<!-- 事务模板对象 -->
	<bean name="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
		<property name="transactionManager" ref="transactionManager"></property>
	</bean>

	<!-- 配置事务通知 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<!--
			 	isolation:隔离级别
			 	propagation:传播行为
			 	read-only:是否只读
			 -->
			<tx:method name="sendScoreByXml" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
			
			<!-- 可以使用通配符*进行批量配置 -->
			<tx:method name="add*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
			<tx:method name="delete*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
			<tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" />
			<tx:method name="find*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" />
		
		</tx:attributes>
	</tx:advice>
	
	<!-- 配置织入 -->
	<aop:config>
		<aop:pointcut expression="execution(* com.milan.transaction.*ServiceImpl.*(..))" id="txPc"/>
		<aop:advisor advice-ref="txAdvice" pointcut-ref="txPc" />
	</aop:config>


spring事务实现方式(三)

使用注解管理:

spring配置文件:

	<!-- 开启使用注解管理事务 -->
	<!-- <tx:annotation-driven/> -->


Service实现类中添加@Transactional注解

	@Override
	@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=false)
	public void sendScoreByHere(int senderId, int accepterId, int score) {
		accountDao.downScore(score, senderId);
		//int i = 1/0;
		accountDao.upScore(score, accepterId);
	}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值