mysql事務處理

c3p0工具類
package com.offcn.util;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
/*
 * c3p0 基於c3p0工具類
 */
public class C3P0Util {
    private static ComboPooledDataSource dataSource = new ComboPooledDataSource();
    public static Connection getconn(){
        Connection conn = null;
        try {
            conn = dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
    public static DataSource getDataSource(){
        return dataSource;
    }
}
------------------------------------------------------------------------------------------------------------------------------
控制層
    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"));

        TransformService ts = new TransformService();
        try {
            ts.trans(from,to,money);
            response.getWriter().println("轉帳成功");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
業務層
    public Boolean trans(String from, String to, Double money) throws SQLException {
        //若from表示張三,to表示李四,則張三帳戶減小1000元,李四帳戶增長1000元
        Connection conn = C3P0Util.getconn();
        TransformDao td = new TransformDao();
        try {
            conn.setAutoCommit(false);
            int i = td.from(from,money,conn);
            int j = td.to(to,money,conn);
            int p = 10/0;
            //提交事務
            conn.commit();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            //回滾事務
            conn.rollback();
        }finally {
            try {
            //關閉鏈接
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return true;
    }
持久層
public class MoneyDao {

    public int from(String from, double money) throws Exception {
        
        Connection conn = c3p0Util.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();
        conn.close();
        return i;
        
    }

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

        Connection conn = c3p0Util.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();
        conn.close();
        return i;
    }

}
相關文章
相關標籤/搜索