JDBC事务

JDBC事务

一、事务概述

1.什么是事务

一件事情有n个组成单元 要不这n个组成单元同时成功 要不n个单元就同时失败,就是将n个组成单元放到一个事务中,结果只有两个,要么成功,要么失败

2.mysql的事务

默认的事务:一条sql语句就是一个事务 默认就开启事务并提交事务,属于自动事务

手动事务:

1)开启一个事务:start transaction

2)事务提交:commit代表从开启事务到事务提交 中间的所有的sql都认为有效真正的更新数据库

3)事务的回滚:rollback 代表事务的回滚 从开启事务到事务回滚 中间的所有的sql操作都认为无效数据库没有被更新

二、JDBC事务操作

默认是自动事务:

执行sql语句:executeUpdate()  ---- 每执行一次executeUpdate方法 代表 事务自动提交

通过jdbc的API手动事务:

开启事务:conn.setAutoComnmit(false);默认值是false

提交事务:conn.commit();

回滚事务:conn.rollback();

注意:控制事务的connnection必须是同一个

执行sql的connection与开启事务的connnection必须是同一个才能对事务进行控制

三、DBUtils事务操作

1.QueryRunner

有参构造:QueryRunner runner = new QueryRunner(DataSource dataSource);

有参构造将数据源(连接池)作为参数传入QueryRunner,QueryRunner会从连 接池中获得一个数据库连接资源操作数据库,所以直接使用无Connection参数   的update方法即可操作数据库

无参构造:QueryRunner runner = new QueryRunner();

无参的构造没有将数据源(连接池)作为参数传入QueryRunner,那么我们在使  用QueryRunner对象操作数据库时要使用有Connection参数的方法

四、事务的特性和隔离级别

事务的特性ACID

1)原子性(Atomicity)原子性是指事务是一个不可分割的工作单位,事务中的操作 要么都发生,要么都不发生。 

2)一致性(Consistency)一个事务中,事务前后数据的完整性必须保持一致。

3)隔离性(Isolation)多个事务,事务的隔离性是指多个用户并发访问数据库时,一个用户的 事务不能被其它用户的事务所干扰,多个并发事务之间数据要相互隔离。

4)持久性(Durability)持久性是指一个事务一旦被提交,它对数据库中数据的改变就是永久性的,接下来即使数据库发生故障也不应该对其有任何影响。

事务的学习最常见的生活实例就是转账功能的实现

//Dao层代码
public
class AccountDao { //转出 public int outMoney(Connection conn,String out,double money) throws SQLException{ QueryRunner qr=new QueryRunner(); String sql="update account set money=money-? where aname=?"; return qr.update(conn,sql,money,out); } //转入 public int inMoney(Connection conn,String in,double money) throws SQLException{ QueryRunner qr=new QueryRunner(); String sql="update account set money=money+? where aname=?"; return qr.update(conn,sql,money,in); }
//Service层代码
public
class AccountService { private AccountDao accountDao=new AccountDao();//对Dao层对象进行封装 public int transfer(String out,String in,double money){ int row=0; int row2=0; //获取链接对象 Connection conn=MyDBUTils.getConn(); try { //开启事务 conn.setAutoCommit(false); row=accountDao.outMoney(conn,out, money); int y=1/0; row2=accountDao.inMoney(conn,in, money); } catch (SQLException e) { e.printStackTrace(); }finally{ //提交事务 try { conn.commit(); } catch (SQLException e) { e.printStackTrace(); //回滚事务 try { conn.rollback(); } catch (SQLException e1) { e1.printStackTrace(); } } } if(row>0&&row2>0){ return 1; } return 0; } }
//Servlet层代码
public
class TransferServlet extends HttpServlet { private AccountService accountService=new AccountService(); public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取参数 request.setCharacterEncoding("utf-8");//解决请求乱码 String out=request.getParameter("out"); String in=request.getParameter("in"); String moneystr=request.getParameter("money"); double money=Double.parseDouble(moneystr); int row=accountService.transfer(out, in, money); //解决响应乱码 response.setContentType("text/html;charset=utf-8"); if(row>0){ response.getWriter().write("转账成功!"); }else{ response.getWriter().write("转账失败!"); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
//jsp文件
<body> <form action="${pageContext.request.contextPath }/TransferServlet" method="post"> 转出账户:<input type="text" name="out"><br> 转入账户:<input type="text" name="in"><br> 金额:<input type="text" name="money"><br> <input type="submit" name="确认"> </form> </body>
原文地址:https://www.cnblogs.com/xinzong/p/14553604.html