一、概述
模拟银行转钱的案例,
在service中执行代码逻辑中是
- 根据名称查询转出账户
- 根据名称查询转入账户
- 转出账户减钱
- 转入账户加钱
- 更新转出账户
- 更新转入账户
当上述步骤中任何一步发生错误时,应该发生事务回滚的操作。
下面采用动态代理+纯注解方式实现事务的管理
案例文件架构
二、案例的代码实现
导入的依赖
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.48</version>
</dependency>
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.26.0-GA</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
</dependencies>
实体类
public class Account implements Serializable {
private Integer id;
private String name;
private Float money;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Float getMoney() {
return money;
}
public void setMoney(Float money) {
this.money = money;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
第一步:首先配置类,代替了xml
SpringConfiguration
/**
* 用来替代bean.xml
*/
@Configuration //表明这是一个配置类
@ComponentScan("com.itheima") //告诉spring要扫描包
@Import(Jdbcconfig.class) //引入连接数据库的配置类
@PropertySource("classpath:jdbcConfig.properties") //jdbc的相关属性设置
public class SpringConfiguration {}
Jdbcconfig
public class Jdbcconfig {
//将properties文件中的信息读取出来,配置文件中等号左边是什么,注解中就使用什么
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
//自动提交事务时
@Bean(name="runner")
@Scope("prototype")
public QueryRunner createQueryRunner(){
return new QueryRunner();
}
//地址池
@Bean(name="dataSource")
public DataSource createDataSource(){
try {
ComboPooledDataSource ds = new ComboPooledDataSource();
ds.setDriverClass(driver);
ds.setJdbcUrl(url);
ds.setUser(username);
ds.setPassword(password);
return ds;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
jdbcConfig.properties在resources中
需要自己设置用户名和密码
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/******
jdbc.username=root
jdbc.password=****
第二步:工具类
从地址池获取连接绑定线程,还有事务管理工具类包含开始事务,提交事务,回滚事务,释放连接
ConnectionUtils
@Component("connectionUtils")
public class ConnectionUtils {
private ThreadLocal<Connection> tl = new ThreadLocal<Connection>();
@Autowired
private DataSource dataSource;
/**
* 获取当前线程上的连接
*/
public Connection getTreadConnection(){
try{
//1.先从ThreadLocal上获取
Connection conn = tl.get();
//2.判断当前线程上是否有连接
if(conn == null){
//3.从数据园中获取一个连接,并且存入ThreadLocal中
conn = dataSource.getConnection();
tl.set(conn);
}
//4.返回当前线程上的连接
return conn;
}catch (Exception e){
throw new RuntimeException(e);
}
}
/**
* 把链接和线程解绑
*/
public void removeConnection(){
tl.remove();
}
}
TransactionManager
@Component("txManager")
public class TransactionManager {
@Autowired
private ConnectionUtils connectionUtils;
/**
* 开始事务
*/
public void beginTransaction(){
try {
connectionUtils.getTreadConnection().setAutoCommit(false);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 提交事务
*/
public void commit(){
try {
connectionUtils.getTreadConnection().commit();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 回滚事务
*/
public void rollback(){
try {
connectionUtils.getTreadConnection().rollback();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 关闭连接
*/
public void release(){
try {
connectionUtils.getTreadConnection().close();
connectionUtils.removeConnection();
} catch (Exception e) {
e.printStackTrace();
}
}
}
第三步: 持久层(dao层)
AccountImpl接口
/**
* 账户持久层接口
*/
public interface IAccountDao {
/**
* 查询所有
* @return
*/
List<Account> findAllAccount();
/**
*查询一个
*/
Account findAccountById(Integer id);
/**
* 保存
*/
void saveAccount(Account account);
/**
* 更新
*/
void updateAccount(Account account);
/**
* 删除
*/
void deleteAccount(Integer id);
/**
* 根据姓名查找
*/
Account findAccountByName(String name);
}
AccountImpl 实现类
/**
* 账户持久层实现类
*/
@Repository("accountDao")
public class AccountImpl implements IAccountDao {
@Autowired
private QueryRunner runner;
@Autowired
private ConnectionUtils connectionUtils;
public List<Account> findAllAccount() {
try {
return runner.query(connectionUtils.getTreadConnection(),"select * from account", new BeanListHandler<Account>(Account.class));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public Account findAccountById(Integer id) {
try {
return runner.query(connectionUtils.getTreadConnection(),"select * from account where id =?", new BeanHandler<Account>(Account.class),id);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void saveAccount(Account account) {
try {
runner.update(connectionUtils.getTreadConnection(),"insert into account(name,money) values(?,?)",account.getName(),account.getMoney());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void updateAccount(Account account) {
try {
runner.update(connectionUtils.getTreadConnection(),"update account set name=?,money=? where id=?",account.getName(),account.getMoney(),account.getId());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void deleteAccount(Integer id) {
try {
runner.update(connectionUtils.getTreadConnection(),"delete from account where id= ?",id);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public Account findAccountByName(String name) {
try {
List<Account> accounts = runner.query(connectionUtils.getTreadConnection(),"select * from account where name = ?", new BeanListHandler<Account>(Account.class), name);
if(accounts == null || accounts.size() == 0){
return null;
}
if(accounts.size()>1){
throw new RuntimeException("结果不唯一,数据有问题");
}
return accounts.get(0);
}catch (Exception e){
throw new RuntimeException(e);
}
}
}
第四步:servcie层
AccountServiceImpl接口
public interface IAccountService{
/**
* 查询所有
* @return
*/
List<Account> findAllAccount();
/**
*查询一个
*/
Account findAccountById(Integer id);
Account findAccountByName(String name);
/**
* 保存
*/
void saveAccount(Account account);
/**
* 更新
*/
void updateAccount(Account account);
/**
* 删除
*/
void deleteAccount(Integer id);
/**
* 转账,转出,转入,金额
*/
void transfer(String sourceName,String targetName,Float money);
}
AccountServiceImpl实现类
@Service("accountService")
public class AccountServiceImpl implements IAccountService {
@Autowired
private IAccountDao accountDao;
public List<Account> findAllAccount() {
return accountDao.findAllAccount();
}
public Account findAccountById(Integer id) {
return accountDao.findAccountById(id);
}
public Account findAccountByName(String name) {
return accountDao.findAccountByName(name);
}
public void saveAccount(Account account) {
accountDao.saveAccount(account);
}
public void updateAccount(Account account) {
accountDao.updateAccount(account);
}
public void deleteAccount(Integer id) {
accountDao.deleteAccount(id);
}
public void transfer(String sourceName, String targetName, Float money) {
System.out.println("transfer....");
//2.1根据名称查询转出账户
Account source = accountDao.findAccountByName(sourceName);
//2.2根据名称查询转入账户
Account target = accountDao.findAccountByName(targetName);
//2.3转出账户减钱
source.setMoney(source.getMoney()-money);
//2.4转入账户加钱
target.setMoney(target.getMoney()+money);
//2.5更新转出账户
accountDao.updateAccount(source);
int i = 1/0; //模拟异常
//2.6更新转入账户
accountDao.updateAccount(target);
}
}
第五步:编写代理类 (非常重要)
Beanfactory
/**
* 用于创建Service的代理对象的工厂
*/
@Component("beanFactory")
public class Beanfactory {
@Autowired
private IAccountService accountService;
@Autowired
private TransactionManager txManager;
/**
* 获取Service代理对象
* @return
*/
@Bean("proxyAccountService")
public IAccountService getAccountService() {
return (IAccountService)Proxy.newProxyInstance(accountService.getClass().getClassLoader(),
accountService.getClass().getInterfaces(),
new InvocationHandler() {
/**
* 添加事务的支持
*
* @param proxy
* @param method
* @param args
* @return
* @throws Throwable
*/
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if("test".equals(method.getName())){
return method.invoke(accountService,args);
}
Object rtValue = null;
try {
//1.开启事务
txManager.beginTransaction();
//2.执行操作
rtValue = method.invoke(accountService, args);
//3.提交事务
txManager.commit();
//4.返回结果
return rtValue;
} catch (Exception e) {
//5.回滚操作
txManager.rollback();
throw new RuntimeException(e);
} finally {
//6.释放连接
txManager.release();
}
}
});
}
}
最后编写测试类
AccountServiceTest
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfiguration.class)
public class AccountServiceTest {
@Autowired
@Qualifier("proxyAccountService") //使用Service的代理对象的工厂
private IAccountService as;
@Test
public void testfindAll() {
List<Account> accounts = as.findAllAccount();
for (Account account:accounts){
System.out.println(account);
}
}
@Test
public void testFindOne() {
Account account = as.findAccountById(1);
System.out.println(account);
}
@Test
public void testSave() {
Account account = new Account();
account.setMoney(1200f);
account.setName("test");
as.saveAccount(account);
}
@Test
public void testUpdate() {
Account account = as.findAccountById(5);
account.setMoney(5632f);
as.updateAccount(account);
}
@Test
public void testDelete() {
as.deleteAccount(4);
}
@Test
public void findAccountByName(){
Account account = as.findAccountByName("aaa");
System.out.println(account);
}
@Test
public void testTransfer(){
as.transfer("bbb","aaa",20f);
}
}
结果:
因为在service的转账逻辑中设置一个异常 int i=1/0;所以转账不能成功
去掉设置int i=1/0; 后 成功