JavaWeb学习-DBUtils框架-6-ThreadLocal和转账版本3

上一篇我们发现还是有问题,不同把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(); //释放连接资源
		}
	}
 
}

运行下,转账没问题。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值