实现spring事务的四种方式


用一个银行账号转钱的案例来说明spring事务的实现。

在转钱过程中,一个账号钱增加,另一个减少,那么当有异常产生时,就会出现钱转丢了的现象

一个减少了,而另一个没有增加,这个时候就需要把这两个行为绑定到一起,要么同时发生,要么都不发生

这就用到了事务,事务就是指在逻辑上的一组操作,这组操作要么全部成功,要么全部失败


实现spring事务的四种方式分别为:

(1)编程式事务管理:需要手动编写代码,在实际开发中很少使用

(2)声明式事务管理:

(2.1)基于TransactionProxyFactoryBean的方式,需要为每个进行事务管理的类做相应配置

(2.2)基于AspectJ的XML方式,不需要改动类,在XML文件中配置好即可

(2.3)基于注解的方式,配置简单,需要在业务层类中添加注解


(2.2)和(2.3)在开发中使用比较多,前者配置一目了然,可以在XML文件中得到所有信息,后者配置简单方便


需要做的一些准备工作:


1.在数据库中新建一张account数据表

SQL脚本:

CREATE TABLE `account` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) NOT NULL,
  `money` double DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
INSERT INTO `account` VALUES ('1', 'aaa', '1000');
INSERT INTO `account` VALUES ('2', 'bbb', '1000');
INSERT INTO `account` VALUES ('3', 'ccc', '1000');


2.需要引入的Jar包


实现spring事务的四种方式_第1张图片



3.为数据库连接准备的配置文件- jdbc.properties

使用c3p0数据库连接池


jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc\:mysql\://127.0.0.1\:3306/test
jdbc.username=root
jdbc.password=



4.创建两个接口

AccountDao:数据库操作

public interface AccountDao {
public void outMoney(String out,Double money);
public void inMoney(String in,Double money);
}


AccountService:逻辑处理操作

public interface AccountService {
/**

* @param out 转出账号
* @param in  转入账号
* @param money 转账金额
*/
public void transfer(String out,String in,Double money);

}



5.创建以上两个接口的实现类

import org.springframework.jdbc.core.support.JdbcDaoSupport;

public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao{
	
	public void outMoney(String out, Double money) {
		String sql="update account set money=money-? where name=?";
		this.getJdbcTemplate().update(sql,money,out);
	}

	public void inMoney(String in, Double money) {
		String sql="update account set money=money+? where name=?";
		this.getJdbcTemplate().update(sql,money,in);
	}
}


public class AccountServiceImpl implements AccountService{

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

	public void transfer(String out,String in,Double money) {
		
		accountDao.outMoney(out, money);
		accountDao.inMoney(in, money);	
	}		
}



6.测试类(使用spring加JUnit4整合测试)

import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")//引入xml文件
public class TestDemo1 {
	
	@Resource(name="accountService")//得到bean id为accountService的对象
	private AccountService accountService;
	
	@Test
	public void demo1(){
		accountService.transfer("aaa", "bbb", 200d);
	}	
}



7.xml文件 - applicationContext.xml:



	
	
	

	
	
		
		
		
		
	
	
	
		
	
	
	
		
	
	




代码实现方式:


(1)编程式事务管理


ServiceImpl类:

import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
public class AccountServiceImpl implements AccountService{

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

	/**
	 * 注入事务管理的模板
	 * 在xml文件中添加Property
	 * 
	 */
	private TransactionTemplate transactionTemplate;
	
	public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
		this.transactionTemplate = transactionTemplate;
	}

	public void transfer(final String out,final String in,final Double money) {
		/**
		 * 在这里面进行事务操作
		 * 因为需要在匿名内部类中使用参数,所以给参数加上final关键字
		 */
		transactionTemplate.execute(new TransactionCallbackWithoutResult(){

			@Override
			protected void doInTransactionWithoutResult(
					TransactionStatus transactionStatus) {
				accountDao.outMoney(out, money);
			
				accountDao.inMoney(in, money);		
			}	
		});
	
	}		
}




xml文件中需要配置事务管理器,无论使用哪一种方式,都需要在xml文件中配置事务管理器,将事务管理器注入到模板中,而该模板又会自动注入到accountService中,业务逻辑处理都放在了execute方法中。





	

	


	
 


在DaoImpl类中,继承了JdbcDaoSupport类,可以省去jdbc复杂的代码

在XML文件配置中,注入dataSource用来获取数据库的连接




(2.1)基于TransactionProxyFactoryBean的方式

采用spring-aop的方式,需要用到代理模式


XML文件:



	

	


	
	
	
	
	
	
		
		
			PROPAGATION_REQUIRED
		
	



在测试类中需自动注入的bean-id就不再是accountService了,而是代理类accountServiceProxy

此时代理类和被代理类实现了共同的接口,AccountService,有关代理模式请看:代理模式

@Resource(name="accountServiceProxy")
private AccountService accountService;




(2.2)基于AspectJ的XML方式


XML文件:



	

	


	
		
		
	

	


	
	




(2.3)基于注解的方式,配置简单,需要在业务层类中添加注解




	

	



在serviceImpl类中需要添加注解,参数和上面同理

@Transactional(propagation=Propagation.REQUIRED)


你可能感兴趣的:(Spring)