实现从A账户向B账户转钱,如果遇到异常,则B账户钱不会多的同时A账户也不会少钱
AccountDao .java
package com.bky.dtdaili.dao;
public interface AccountDao {
void add();
void delete();
void updateA();
void updateB();
void find();
}
AccountDaoImpl .java
package com.bky.dtdaili.dao.impl;
import com.bky.dtdaili.dao.AccountDao;
import com.bky.dtdaili.utils.ConnUtils;
import org.springframework.stereotype.Repository;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
@Repository
public class AccountDaoImpl implements AccountDao {
@Override
public void add() {
}
@Override
public void delete() {
}
@Override
public void updateA() {
Connection connection = ConnUtils.getConnection();
try {
PreparedStatement preparedStatement = connection.prepareStatement("update t_account set a_account=a_account- ? where id=1");
preparedStatement.setDouble(1,100);
//执行sql
int i = preparedStatement.executeUpdate();
if(i>0){
System.out.println("修改成功");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
@Override
public void updateB() {
Connection connection = ConnUtils.getConnection();
try {
PreparedStatement preparedStatement = connection.prepareStatement("update t_account set b_account=b_account + ? where id=1");
preparedStatement.setDouble(1,100);
//执行sql
int i = preparedStatement.executeUpdate();
if(i>0){
System.out.println("修改成功");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
@Override
public void find() {
}
}
AccountService .java
package com.bky.dtdaili.service;
public interface AccountService {
void add();
void delete();
void updateA();
void updateB();
void find();
}
AccountServiceImpl .java
package com.bky.dtdaili.service.impl;
import com.bky.dtdaili.dao.AccountDao;
import com.bky.dtdaili.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao dao;
@Override
public void add() {
}
@Override
public void delete() {
}
@Override
public void updateA() {
//完成对A账户扣钱的操作
dao.updateA();
}
@Override
public void updateB() {
//完成对B账户扣钱的操作
dao.updateB();
}
@Override
public void find() {
}
}
test01.java
package com.bky.dtdaili.test;
import com.bky.dtdaili.service.AccountService;
import com.bky.dtdaili.utils.ConnUtils;
import com.bky.dtdaili.utils.TranstionUtils;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.sql.Connection;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:xml/beans.xml")
public class Test {
@Autowired
private AccountService accountService;
@Autowired
private TranstionUtils transtionUtils;
@org.junit.Test
public void test01(){
/**
* 此时 就可以采用aop 把重复的代码提取出来 放进增强代码里面
*/
//开启事务
transtionUtils.beginTranstion();
//扣钱
accountService.updateA();
//模拟异常
int a=1/0;
//价钱
accountService.updateB();
//提交事务
transtionUtils.commitTranstion();
transtionUtils.closeConn();
}
}
ConnUtils .java
package com.bky.dtdaili.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* 完成单例模式 是的连接只有一个
* 需要让Connection数据库对象在当前线程处于一个单例的模式
*/
public class ConnUtils {
private static Connection connection;
static {
try {
//注册驱动
Class.forName("com.mysql.jdbc.Driver");
connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/test01","root","root");
} catch (SQLException | ClassNotFoundException throwables) {
throwables.printStackTrace();
}
}
public static Connection getConnection(){
return connection;
}
}
TranstionUtils .java
package com.bky.dtdaili.utils;
import org.springframework.stereotype.Component;
import java.sql.SQLException;
@Component
public class TranstionUtils {
/*
开启事务
*/
public void beginTranstion(){
//true 会默认每一次sql执行完毕的时候都提交一次
//此处 我们想要自己提交
try {
ConnUtils.getConnection().setAutoCommit(false);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
/*
提交事务
*/
public void commitTranstion(){
try {
ConnUtils.getConnection().commit();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
/*
回滚事务
*/
public void rollTranstion(){
try {
ConnUtils.getConnection().rollback();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
/*
关闭资源
*/
public void closeConn(){
try {
ConnUtils.getConnection().close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
beans.xml
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.bky"></context:component-scan>
</beans>