使用ThreadLocal控制事务实现用户转账

上一篇已经实现了用户转帐功能,补充一个内容,使用事务要保证实用的是同一个connection连接,其中dao层和dao的实现类还有service接口内容不变,改变的是service的实现类AccountServiceImpl.java

package com.qianfeng.service.impl;

import java.sql.Connection;
import java.sql.SQLException;

import com.qianfeng.dao.AccountDao;
import com.qianfeng.dao.impl.AccountDaoImpl;
import com.qianfeng.domain.Account;
import com.qianfeng.service.AccountService;
import com.qianfeng.util.C3P0Util;
import com.qianfeng.util.ManagerThreadLocal;

public class AccountServiceImpl implements AccountService {

    private AccountDao  accountDao = new AccountDaoImpl();

    public void transfer(String fromname, String toname, int money)  {
            //Connection conn = null;
          try {
          //使用threadlocal、实现就不用c3p0的方法了
            //conn = C3P0Util.getConnection();
            //accountDao = new AccountDaoImpl(conn);
            //conn.setAutoCommit(false);

            //需要手动控制事务则是service 先得到Connection对象,否则是Dao先得到 Connection对象
            //ManagerThreadLocal.startTransaction();
            //得到转账人的信息
            Account from = accountDao.findAccountByName(fromname);
            //得到收钱人的信息
            Account to = accountDao.findAccountByName(toname);

            from.setMoney(from.getMoney()-money);
            to.setMoney(to.getMoney()+money);

            //转账
            accountDao.updateAccount(from);
            //int num = 5/0;
            accountDao.updateAccount(to);

            ManagerThreadLocal.commitTrancsaction();
        } catch (Exception e) {
            try {
                ManagerThreadLocal.roolBackTransaction();
                e.printStackTrace();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }finally{
            try {
                ManagerThreadLocal.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

下面看一下threadlocal的使用
ManagerThreadLocal.java

package com.qianfeng.util;
import java.sql.Connection;
import java.sql.SQLException;
public class ManagerThreadLocal {
    private static ThreadLocal<Connection> local = new ThreadLocal<Connection>();
    public static Connection getConnection() throws SQLException
    {
        Connection conn = null;
        conn = local.get();//从ThreadLocal中获取Connection 对象
        if(conn==null){
            conn = C3P0Util.getConnection();
            local.set(conn);//把取到的Connection对象 放入ThreadLocal
        }
        return conn;
    }
    //开启事务
    public static void startTransaction() throws SQLException
    {
        Connection conn = getConnection();
        conn.setAutoCommit(false);
    }

    //提交事务
    public static void commitTrancsaction() throws SQLException
    {
        getConnection().commit();
    }
    //回滚事务
    public static void roolBackTransaction() throws SQLException
    {
        getConnection().rollback();
    }
    public static void close() throws SQLException
    {
        getConnection().close();//放回连接池
        local.remove();//从ThreadLocal中删除
    }
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值