jdbc事務處理

jdbc工具類
package com.offcn.util;
import java.sql.Connection;
import java.sql.SQLException;
import com.mchange.v2.c3p0.ComboPooledDataSource;

public class JdbcUtil {
    private static ComboPooledDataSource dataSource = new ComboPooledDataSource();
    //線程中存儲鏈接的形式至關於map(key,value)
    private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>();
    
    //獲取一個鏈接
    private static Connection getconn() {
        Connection conn = tl.get();//從線程中獲取鏈接
        if (conn==null) {
            try {
                //當第一次問線程中獲取鏈接時,線程中時沒有鏈接的,須要去鏈接池獲取一條
                dataSource.getConnection();
                tl.set(conn);//將鏈接綁定到線程中去
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    
    //開啓事務
    public static void startTransaction() {
        Connection conn = getconn();
        try {
            conn.setAutoCommit(false);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    
    //提交事務
    public static void commitTransaction() {
        Connection conn = getconn();
        try {
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
        
    //回滾事務
    public static void rollBackTransaction() {
        Connection conn = getconn();
        try {
            conn.rollback();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
------------------------------------------------------------------------------------------------------------------------------
控制層
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //解決請求傳遞中文參數亂碼的的問題  瀏覽器--》服務器
        request.setCharacterEncoding("utf-8");
        //服務器--》瀏覽器亂碼問題的解決
        response.setContentType("text/html;charset=utf-8");
        String from = request.getParameter("from");
        String to = request.getParameter("to");
        double money =Double.parseDouble( request.getParameter("money"));
        MoneyService ms = new MoneyService();
        Boolean flag = ms.trans(from,to,money);
        if(flag) {
            response.getWriter().println("轉帳成功。。。。。。");
        }else {
            response.getWriter().println("轉帳失敗。。。。。。");
        }
    }
------------------------------------------------------------------------------------------------------------------------------
業務層
public class MoneyService {
    public Boolean trans(String from, String to, double money) {
            MoneyDao md = new MoneyDao();        
        try {
            JdbcUtil.startTransaction();
                int a = md.from(from,money);
                int b = md.to(to,money);
                if(a*b>0) {
                    JdbcUtil.commitTransation();
                    return true;
                }    
        } catch (Exception e) {                
            e.printStackTrace();
            JdbcUtil.rollBackTransaction();
        }finally {
            try {
                JdbcUtil.getConn().close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }            
        return false;
    }
}
------------------------------------------------------------------------------------------------------------------------------
持久層
public class MoneyDao {
    
    public int from(String from, double money) throws Exception {

        Connection conn = JdbcUtil.getConn();
        PreparedStatement pstmt = conn.prepareStatement("update account set money=money-? where name=?");
        pstmt.setDouble(1, money);
        pstmt.setString(2, from);
        int i = pstmt.executeUpdate();
        pstmt.close();
        return i;
    }

    public int to(String to, double money) throws Exception {

        Connection conn = JdbcUtil.getConn();
        PreparedStatement pstmt = conn.prepareStatement("update account set money=money+? where name=?");
        pstmt.setDouble(1, money);
        pstmt.setString(2, to);
        int i = pstmt.executeUpdate();
        pstmt.close();
        return i;
    }
}
相關文章
相關標籤/搜索