zoukankan      html  css  js  c++  java
  • JDBC 事务(一) 隔离级别

    public class TxTest {

        public static void main(String[] args) throws SQLException {
            test();
        }

        static void test() throws SQLException {
            Connection conn = null;
            Statement st = null;
            ResultSet rs = null;
            try {
                conn = JdbcUtils.getConnection();
                conn.setAutoCommit(false);
                conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);

    /*

    先解释一下:

    事务指定一个隔离级别,该隔离级别定义一个事务必须与由其他事务进行的资源或数据更改相隔离的程度。隔离级别从允许的并发副作用(例如,脏读或虚拟读取)的角度进行描述。 
    a:脏读取:一个事务读取了另外一个并行事务未提交的数据
    b:不可重复读取:一个事务再次读取之前读过的数据时得到的数据不一致,被另外一个事务修改。

    把甲的安全级别设成是是允许自己可重复读的。那么甲事务在执行中读了一次ID是10的数据的资金是1000,在执行过程中第二次它再去读这个ID10的资金有可能就不是1000了,因为乙在甲执行第二次查询之前第一次查询之后将这条数据的资金修改并提交了。

    c:虚读:一个事务重新执行一个查询,返回的记录包含了其他事务提交的新记录
    设定事务的隔离级别:con.setTransactionIsolation(Connection.isolationLevel);

    四种隔离级别:
    con.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);//最底级别:只保证不会读到非法数据,上述3个问题有可能发生
    con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); //默认级别:可以防止脏读
    con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);//可以防止脏读和不可重复读取
    con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE); //最高级别:防止上述3种情况,事务串行执行,慎用

    表:

    允许自己,但是不一定会:V     不允许自己,绝对不会:x

    隔离级别

    脏读

    不可重复读

    幻读

    读未提交(Read uncommitted)

    V

    V

    V

    读已提交(Read committed)

    x

    V

    V

    可重复读(Repeatable read)

    x

    x

    V

    可串行化(Serializable )

    x

    x

    x

    */           
                st = conn.createStatement();
                String sql = "update user set money=money-10 where id=1";
                st.executeUpdate(sql);

                sql = "select money from user where id=2";
                rs = st.executeQuery(sql);
                float money = 0.0f;
                if (rs.next()) {
                    money = rs.getFloat("money");
                }
                if (money > 400)
                    throw new RuntimeException("已经超过最大值!");
                sql = "update user set money=money+10 where id=2";
                st.executeUpdate(sql);
                conn.commit();
            } catch (SQLException e) {
                if (conn != null)
                    conn.rollback();
                throw e;
            } finally {
                JdbcUtils.free(rs, st, conn);
            }
        }
    }

    跨越多个数据源的事务,使用JTA容器实现事务

    分成两阶段提交。

    javax.transaction.UserTransaction tx = (UserTransaction)ctx.lookup(“jndiName");

    tx.begin();

    //connection1 connection2 (可能来自不同的数据库)…

    tx.commit();//tx.rollback();

  • 相关阅读:
    进程与线程
    the art of seo(chapter seven)
    the art of seo(chapter six)
    the art of seo(chapter five)
    the art of seo(chapter four)
    the art of seo(chapter three)
    the art of seo(chapter two)
    the art of seo(chapter one)
    Sentinel Cluster流程分析
    Sentinel Core流程分析
  • 原文地址:https://www.cnblogs.com/flying607/p/3461215.html
Copyright © 2011-2022 走看看