第8章 基于MVC模式的事务控制

2019-05-28  本文已影响0人  yangsg

1. 事务的概念和特性

事务一般指,一个业务逻辑包含多个DAO操作。这些DAO操作是一个整体不可分割,并且必须同时成功或同时失败。
事务具有下述4个特性

2. 基于MVC模式事务控制

MVC模式的事务控制是在Service层完成的,正常情况下Service方法中通过调用多个DAO方法来完成业务逻辑。
需要保证的事务需要的支持

DAO方法中必须将异常上报,这样Service层才可以得知DAO方法的执行情况,根据DAO是否发生异常来判断Service执行是否成功

Service层在执行DAO方法前,关闭自动提交,在try的结尾手工提交,在catch中手工回滚

3. 实例-转账

数据库表

数据库表
数据

DAO方法

public class SystemDAO {
    
    public void addMoney(Connection conn, String name, double money) throws Exception {
        String sql = "update accounts set money = money + ? where aname = ?";
        PreparedStatement pst = null;
        try {
            pst = conn.prepareStatement(sql);
            pst.setDouble(1, money);
            pst.setString(2, name);
            pst.executeUpdate();
        } catch (Exception e) {
            throw e;
        } finally {
            DBUtil.close(pst);
        }
        return; 
    }
    
    public void descMoney(Connection conn, String name, double money) throws Exception {
        String sql = "update accounts set money = money - ? where aname = ?";
        PreparedStatement pst = null;
        try {
            pst = conn.prepareStatement(sql);
            pst.setDouble(1, money);
            pst.setString(2, name);
            pst.executeUpdate();
        } catch (Exception e) {
            throw e;
        } finally {
            DBUtil.close(pst);
        }
        return; 
    }
}

Service方法
数据库连接工具类DBUtil略,请参照之前的例子

public class SystemService {
    
    private SystemDAO dao = new SystemDAO();
    
    public void zhuanzhang(String from, String to, double money) {
        Connection conn = DBUtil.getConnection();
        
        try {
            conn.setAutoCommit(false); //关闭自动提交
            dao.descMoney(conn, from, money);
            dao.addMoney(conn, to, money);
            conn.commit(); //手动提交
        } catch (Exception e) {
            try {
                conn.rollback();//手工回滚
            } catch (SQLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } 
        } finally {
            DBUtil.close(conn);
        }
        
    }
}

Servlet

@WebServlet("/zhuanzhang")
public class ZhuanzhangServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        
        String m = request.getParameter("money");
        double d = Double.parseDouble(m);
        SystemService ss = new SystemService();
        ss.zhuanzhang("赵四", "刘能", d);

        response.sendRedirect(request.getContextPath()+"/index.jsp");
    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

JSP页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form action="${pageContext.request.contextPath}/zhuanzhang" method="post">
        转账金额:<input type="text" name="money"><br>
        <input type="submit" value="转账">
    </form>
</body>
</html>
上一篇 下一篇

猜你喜欢

热点阅读