示例采用三层框架
web层:
package cn.sasa.web; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import cn.sasa.service.TransferService; public class TransferServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String outAccount = request.getParameter("outAccount"); String inAccount = request.getParameter("inAccount"); double money = Double.parseDouble(request.getParameter("money")); TransferService tran = new TransferService(); boolean flag = tran.doTran(outAccount, inAccount, money); if(flag) { response.getWriter().write("ok"); }else { response.getWriter().write("no"); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
service层:
package cn.sasa.service; import java.sql.Connection; import java.sql.SQLException; import cn.sasa.dao.TransferDao; import cn.sasa.util.C3P0Utils; public class TransferService { public boolean doTran(String outAccount, String inAccount, double money) { Connection conn = C3P0Utils.getConnection(); boolean flag = true; try { conn.setAutoCommit(false); TransferDao tran = new TransferDao(); int rs1 = tran.doOutAccount(conn, outAccount, money); int rs2 = tran.doInAccount(conn, inAccount, money); if(rs1<=0 || rs2<=0) { conn.rollback(); flag=false; } } catch (Exception e) { flag = false; try { conn.rollback(); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } e.printStackTrace(); }finally { try { conn.commit(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return flag; } }
dao层:
package cn.sasa.dao; import java.sql.Connection; import java.sql.SQLException; import org.apache.commons.dbutils.QueryRunner; public class TransferDao { //资金转出 public int doOutAccount(Connection conn, String outAccount, double money) throws SQLException { QueryRunner runner = new QueryRunner(); String sql = "update account set money=money-? where name=?"; int rs = runner.update(conn, sql, money,outAccount); return rs; } //资金转入 public int doInAccount(Connection conn, String inAccount, double money) throws SQLException { QueryRunner runner = new QueryRunner(); String sql = "update account set money=money+? where name=?"; int rs = runner.update(conn, sql, money,inAccount); return rs; } }
客户端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.servletContext.contextPath}/TransferServlet" method="post"> 转出账户:<input type="text" name="outAccount" value=""/><br/> 转入账户:<input type="text" name="inAccount" value=""/><br/> 转账金额:<input type="text" name="money" value=""/><br> <input type="submit" value="确定"> </form> </body> </html>
事务的注意事项:
手动开启事务都用 conn.setAutoCommit(false);
要用同一个conn控制事务。