DBUtils 제어 트랜잭션 -------- 이체 작업

6923 단어 JavaWeb
dao:
package com.itheima.dao;

import com.itheima.domain.Account;

public interface AccountDao {
	/**
	 *   
	 * @param fromname     
	 * @param toname      
	 * @param money      
	 */
	@Deprecated
	public void updateAccount(String fromname,String toname,double money)throws Exception;
	
	/**
	 *           
	 * @param accout
	 */
	public void updateAccout(Account accout) throws Exception;
	
	/**
	 *            
	 * @param name
	 * @return
	 * @throws Exception
	 */
	public Account findAccountByName(String name)throws Exception;
}

dao.impl:
package com.itheima.dao.impl;

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

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import com.itheima.dao.AccountDao;
import com.itheima.domain.Account;
import com.itheima.util.C3P0Util;
import com.itheima.util.ManagerThreadLocal;

public class AccountDaoImpl implements AccountDao {


	public void updateAccount(String fromname, String toname, double money) throws Exception {
		//    QueryRunner  
		QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
		qr.update("update account set money=money-? where name=?",money,fromname);
		qr.update("update account set money=money+? where name=?",money,toname);
	}

	public void updateAccout(Account account) throws Exception {
		QueryRunner qr = new QueryRunner();
		qr.update(ManagerThreadLocal.getConnection(),"update account set money=? where name=?",account.getMoney(),account.getName());
	}

	public Account findAccountByName(String name) throws Exception {
		QueryRunner qr = new QueryRunner();
		return qr.query(ManagerThreadLocal.getConnection(),"select * from account where name=?", new BeanHandler(Account.class),name);
	}

}

service:
package com.itheima.service;

public interface AccountService {
	/**
	 *   
	 * @param fromname     
	 * @param toname      
	 * @param money      
	 */
	public void transfer(String fromname,String toname,double money);
}

service.impl:
package com.itheima.service.impl;

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

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

public class AccountServiceImpl implements AccountService {

	public void transfer(String fromname, String toname, double money) {
	//	ad.updateAccount(fromname, toname, money);
		AccountDao ad = new AccountDaoImpl();
		
		try {
			ManagerThreadLocal.startTransacation();//begin
			//             
			Account fromAccount = ad.findAccountByName(fromname);
			Account toAccount = ad.findAccountByName(toname);
			
			//         
			fromAccount.setMoney(fromAccount.getMoney()-money);
			toAccount.setMoney(toAccount.getMoney()+money);
			
			//      
			ad.updateAccout(fromAccount);
//			int i = 10/0;
			ad.updateAccout(toAccount);
			
			ManagerThreadLocal.commit();//    
		} catch (Exception e) {
			try {
				ManagerThreadLocal.rollback();//    
			} catch (Exception e1) {
				e1.printStackTrace();
			} 
		}finally{
			try {
				ManagerThreadLocal.close();
			} catch (Exception e) {
				e.printStackTrace();
			}//  
		}
	}

}

domain:
package com.itheima.domain;

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 "Account [id=" + id + ", name=" + name + ", money=" + money
				+ "]";
	}
	
	
}

util:
C3P0Util:
package com.itheima.util;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class C3P0Util {
	//       
	private static DataSource dataSource = new ComboPooledDataSource();
	
	
	public static DataSource getDataSource() {
		return dataSource;
	}

	//             
	public static Connection getConnection(){
		try {
			return dataSource.getConnection();
		} catch (SQLException e) {
			throw new RuntimeException("     ");
		}
	}
	
	public static void release(Connection conn,Statement stmt,ResultSet rs){
		//    
				if(rs!=null){
					try {
						rs.close();
					} catch (Exception e) {
						e.printStackTrace();
					}
					rs = null;
				}
				if(stmt!=null){
					try {
						stmt.close();
					} catch (Exception e) {
						e.printStackTrace();
					}
					stmt = null;
				}
				if(conn!=null){
					try {
						conn.close();//  
					} catch (Exception e) {
						e.printStackTrace();
					}
					conn = null;
				}
	}
	
}

ManagerThreadLocal:
package com.itheima.util;

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

public class ManagerThreadLocal {
	private static ThreadLocal tl = new ThreadLocal();
	
	//      
	public static Connection getConnection(){
		Connection conn = tl.get();//            
		if(conn==null){
			conn = C3P0Util.getConnection();//       
			tl.set(conn);// conn            
		}
		return conn;
	}
	
	//    
	public static void startTransacation(){
		try {
			Connection conn = getConnection();
			conn.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();//       
			tl.remove();//         conn  
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}

xml:


  
	com.mysql.jdbc.Driver
	jdbc:mysql://localhost:3306/day13
	root
	abc
    10
    30
    100
    10

  

좋은 웹페이지 즐겨찾기