web19 事务

2017-10-03  本文已影响0人  路人爱早茶

1.事务

2.mysql的事务
默认的事务:一条sql语句就是一个事务 默认就开启事务并提交事务
手动
1)显示的开启一个事务:start transaction事务:

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

二、JDBC事务操作
默认是自动事务:
执行sql语句:executeUpdate()  ---- 每执行一次executeUpdate方法 代表   事务自动提交
通过jdbc的API手动事务:
开启事务:conn.setAutoComnmit(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参数的方法

四、使用ThreadLocal绑定连接资源
-----DBUtils的修改,将service,dao层的connection使用ThreadLocal联系起来确保同一个,同时使service得conn看不见---
-------web层
public class TransferServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String in = request.getParameter("in");
        String out = request.getParameter("out");
        Double money = Double.parseDouble(request.getParameter("money"));
        Service ser = new Service();
        boolean istranfer=ser.istransfer(in,out,money);
        response.setContentType("text/html;charset=utf-8");
        if (istranfer) {
            response.getWriter().write("转账成功");
        }else {
            response.getWriter().write("转账失败");
            
        }
        
    }
--------------service层
public class Service {

    
    public boolean istransfer(String in, String out, Double money) {

        
//      Connection conn=null;
        
        boolean istransfer=true;
        Dao dao = new Dao();
        try {
//          ------service出现dao层的conn不合适所以将获得conn写进方法类中--------
//          conn=C3p0Utiles.getCon();
//          conn.setAutoCommit(false);
//          dao.in( conn,in,money);
//          int a=1/0;
//          dao.out( conn,out,money);
//          --------将当前线程绑定当前conn------------
            
            MyCurrentConn.startCommit();
            dao.in(in, money);
            dao.out(out, money);
            
        
        } catch (Exception e) {
            istransfer=false;
            
        try {
//          conn.rollback();
            MyCurrentConn.rollBackCommit();
        } catch (SQLException e1) {
            
            e1.printStackTrace();
        }   
            e.printStackTrace();
        }finally {
            try {
//              conn.commit();
                MyCurrentConn.endCommit();
            } catch (SQLException e) {
                
                e.printStackTrace();
            }
        }
        
        
        return istransfer;
    }

}
-----------------dao层
public class Dao {
private QueryRunner qr = new QueryRunner();

    /*public void in(Connection conn, String in, Double money) throws SQLException {
        String sql="update count set money=money+? where name=?";
        qr.update(conn, sql,money,in);
        }

    public void out(Connection conn, String out, Double money) throws SQLException {
    
        String sql="update count set money=money-? where name=?";
        qr.update(conn, sql,money,out);         }*/
    
public void in( String in, Double money) throws SQLException {
    String sql="update count set money=money+? where name=?";
    qr.update(MyCurrentConn.getCurrentConn(), sql,money,in);
}

public void out( String out, Double money) throws SQLException {

    String sql="update count set money=money-? where name=?";
    qr.update(MyCurrentConn.getCurrentConn(), sql,money,out);
}
----------------最关键的工具-ThreadLocal仅能携带一个,想带多个将参数改成list
public class MyCurrentConn {
    private static ComboPooledDataSource datasourse = new ComboPooledDataSource("text");
    private static ThreadLocal<Connection> tl = new ThreadLocal<>();
    public static ComboPooledDataSource getCombpdatasource() {
        return datasourse;
    }
    public static Connection getCurrentConn() {
        Connection conn = tl.get();
        if (conn == null) {
            conn = getCon();
            tl.set(conn);
        }
        return conn;
    }

    public static void startCommit() throws SQLException {
        getCurrentConn().setAutoCommit(false);
    }

    public static void rollBackCommit() throws SQLException {
        getCurrentConn().rollback();
        ;
    }

    public static void endCommit() throws SQLException {
        getCurrentConn().commit();
        // 释放tl绑定资源,关闭close;
        tl.remove();
        getCurrentConn().close();
        ;   }

    public static Connection getCon() {
        try {
            return datasourse.getConnection();
        } catch (SQLException e) {

            throw new RuntimeException(e);
        }
    }
}
Paste_Image.png Paste_Image.png

2.事务的特性和隔离级别(概念性问题---面试)

注意:mysql数据库默认的隔离级别
查看mysql数据库默认的隔离级别:select @@tx_isolation

Paste_Image.png

设置mysql的隔离级别:set session transaction isolation level 设置事务隔离级别


Paste_Image.png
总结:
    mysql的事务控制:
            开启事务:start transaction;
            提交:commit;
            回滚:rollback;
    JDBC事务控制:
            开启事务:conn.setAutoCommit(false);
            提交:conn.commit();
            回滚:conn.rollback();
        DBUtils的事务控制 也是通过jdbc
            
        ThreadLocal:实现的是通过线程绑定的方式传递参数

        概念:
            事务的特性ACID
            并发问题:脏读、不可重读、虚读\幻读
            解决并发:设置隔离级别
                read uncommitted
                read committed
                repeatable read (mysql默认)
                serialazable 

            隔离级别的性能:
                read uncommitted>read committed>repeatable read>serialazable 
            安全性:
                read uncommitted<read committed<repeatable read<serialazable

上一篇下一篇

猜你喜欢

热点阅读