上一篇我们发现还是有问题,不同把C3P0得到Connection对象的代码出现在业务层,这个只能出现在DAO层,这篇就来解决这个问题。
1.ThreadLocal
先看看ThreadLocal,叫线程局部变量,这个类有set和get方法,和当前线程绑定。
package com.anthony.threadlocal;
public class TestThreadLocal {
public static void main(String[] args) {
ThreadLocal t = new ThreadLocal();
t.set("P");
//第二个线程
MyThread mt = new MyThread(t);
mt.start();
System.out.println(t.get());
}
}
package com.anthony.threadlocal;
public class MyThread extends Thread {
private ThreadLocal t1;
public MyThread(ThreadLocal t1) {
this.t1 = t1;
}
public void run() {
System.out.println(t1.get() + "aaaaaa");
}
}
第一个线程是Main方法产生的,第二个线程MyThread,我这里想在第二个线程里面,想取出第一个线程中的set的值“P”,运行下
P
nullaaaaaa
发现第二个线程是取不到第一个线程中set进去的P这个值,如果这里把P换成我们Connection对象,那么我们就可以在DAO层实现getConnection。
2.第三版转账(推荐)
2.1 在utils包(没有就新建一个)下新建一个MangerThreadLocal类
package com.anthony.utils;
import java.sql.Connection;
import java.sql.SQLException;
import com.anthony.datasource.C3P0Utils;
public class MangerThreadLocal {
private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>();
// 得到连接对象
public static Connection getConnection() {
Connection conn = tl.get();
if(conn == null) {
conn = C3P0Utils.getConnection(); //从池中取出一个连接对象
tl.set(conn);
}
return conn;
}
// 开始事务
public static void startTransaction() {
try {
getConnection().setAutoCommit(false);
} catch (SQLException e) {
e.printStackTrace();
}
}
// 提交事务
public static void commit() {
try {
getConnection().commit();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 回滚事务
public static void rollback() {
try {
getConnection().rollback();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void close() {
try {
getConnection().close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
注意上面tl.set(conn)不要漏写。
2.2 AccountDaoImpl.java代码
package com.anthony.dao.impl;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import com.anthony.dao.AccountDao;
import com.anthony.datasource.C3P0Utils;
import com.anthony.entity.Account;
import com.anthony.utils.MangerThreadLocal;
public class AccountDaoImpl implements AccountDao {
public void updateAccount(String fromName, String toName, double money) throws Exception {
//创建一个QueryRunner对象
QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
//转出
qr.update("update account set money=money-? where name=?", money, fromName);
qr.update("update account set money=money+? where name=?", money, toName);
}
@Override
public void updateAccount(Account account) throws Exception {
QueryRunner qr = new QueryRunner();
qr.update(MangerThreadLocal.getConnection(), "update account set money=? where name=?", account.getMoney(), account.getName());
}
public Account findAccountByName(String name) throws Exception {
QueryRunner qr = new QueryRunner();
Account account = qr.query(MangerThreadLocal.getConnection(), "select * from account where name=?", new BeanHandler<Account>(Account.class), name);
return account;
}
}
注意这里conn的得到方式。
2.3 AccountServiceImpl.java代码
package com.anthony.service.impl;
import java.sql.Connection;
import java.sql.SQLException;
import com.anthony.dao.AccountDao;
import com.anthony.dao.impl.AccountDaoImpl;
import com.anthony.datasource.C3P0Utils;
import com.anthony.entity.Account;
import com.anthony.service.AccountService;
import com.anthony.utils.MangerThreadLocal;
public class AccountServiceImpl implements AccountService {
AccountDao ad = new AccountDaoImpl();
public void transfer(String fromName, String toName, double money) {
//ad.updateAccount(fromName, toName, money);
try {
MangerThreadLocal.startTransaction(); //开始事务
Account fromAccount = ad.findAccountByName(fromName);
Account toAccount = ad.findAccountByName(toName);
//设置各自账户金额
fromAccount.setMoney(fromAccount.getMoney()-money);
toAccount.setMoney(toAccount.getMoney()+money);
//完成转账操作
ad.updateAccount(fromAccount);
ad.updateAccount(toAccount);
MangerThreadLocal.commit(); //提交事务
} catch (Exception e) {
e.printStackTrace();
MangerThreadLocal.rollback(); //回滚事务
} finally {
MangerThreadLocal.close(); //释放连接资源
}
}
}
运行下,转账没问题。