model
public class Account {
private int id;
private String name;
private double money;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
@Override
public String
toString() {
return "UD{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
}
dao层
//接口
public interface IAccountDao {
/**
* 根据名字查找账户信息
*
* @param name
* @return
*/
public Account findAccount(String name) throws SQLException;
/**
* 根据名字查找账号信息
*
* @param account
*/
public void updateAccount(Account account) throws SQLException;
}
//实现类
public class AccountDao implements IAccountDao {
private Connection con;
@Override
public Account findAccount(String name) {
QueryRunner qr = new QueryRunner();
String sql = "SELECT * FROM account WHERE name = ?";
Account account = null;
try {
account = qr.query(ManagerThreadLocal.getConnection(), sql, new BeanHandler<Account>(Account.class), name);
} catch (SQLException e) {
e.printStackTrace();
}
return account;
}
@Override
public void updateAccount(Account account) throws SQLException {
QueryRunner qr = new QueryRunner();
String sql = "UPDATE account SET MONEY = ? WHERE ID = ?";
qr.update(ManagerThreadLocal.getConnection(), sql, account.getMoney(), account.getId());
}
}
service层
//接口
public interface IAccountService {
//转账
public void transfer(String from, String to, double amount);
}
//实现类
public class AccountServiceImpl implements IAccountService {
@Override
public void transfer(String from, String to, double amount) {
try {
IAccountDao iAccountDao = new AccountDao();
//开启事务
ManagerThreadLocal.beginTransaction();
//1.获取from的账号信息
Account fromAccount = iAccountDao.findAccount(from);
//2.获取to的账号信息
Account toAccount = iAccountDao.findAccount(to);
//3.修改model的money信息
fromAccount.setMoney(fromAccount.getMoney() - amount);
toAccount.setMoney(toAccount.getMoney() + amount);
//4.操作数据库
iAccountDao.updateAccount(fromAccount);
//int i = 10 / 0;
iAccountDao.updateAccount(toAccount);
//提交事务
ManagerThreadLocal.commitTransaction();
} catch (SQLException e) {
//回滚事务
ManagerThreadLocal.rollbackTransaction();
} finally {
ManagerThreadLocal.close();//放回连接池
}
}
}
THREADLOCAL
public class ManagerThreadLocal {
private static ThreadLocal<Connection> tl = new ThreadLocal<>();
public static Connection getConnection() {
Connection con = tl.get();
//第一次为空
if (con == null) {
try {
con = DBC3P0Utils.getConnection();
tl.set(con);
System.out.println(1);
return con;
} catch (SQLException e) {
e.printStackTrace();
}
} else {
con = tl.get();
System.out.println(2);
return con;
}
return null;
}
/**
* 开启事务
*/
public static void beginTransaction() {
try {
getConnection().setAutoCommit(false);
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 回滚事务
*/
public static void rollbackTransaction() {
try {
getConnection().rollback();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 提交事务
*/
public static void commitTransaction() {
try {
getConnection().commit();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 关闭连接
*/
public static void close() {
try {
getConnection().close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
单元测试
public class T01 {
@Test
public void test() {
IAccountService iAccountService = new AccountServiceImpl();
iAccountService.transfer("aaa","bbb",100);
}
@Test
public void test2() {
Connection con1 = ManagerThreadLocal.getConnection();
Connection con2 = ManagerThreadLocal.getConnection();
System.out.println(con1);
System.out.println(con2);
}
}