




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、oracle 事务隔离级别,用jdbc体验做技术支持 2 个月了,也就是说有 2 个月没有碰代码了,手都很生了,最近遇到项目大的并发问题,数据也有些不太正确,就想到了项目中,由于模块过多,异步的情况也有发生,所以想到事务与锁的相关知识,先写一点事务相关的理解,然后写一点锁相关的东西,以便加深自己的理解。 Oracle 支持的 2 种事务隔离级别 Read committed , Serializable 用 JDBC 进行了测试和学习,根据自己的理解写点心得,这里全部是我个人的看法和理解,如果错误之处请大家告诉我,以便误导他人同时也会使我学习到更
2、多的东西。 所需数据准备如下: item item_value action_time id aaa LOOCKY 06-12-2006 15:23:54 1 tsindex users 06-12-2006 15:23:54 2 tstemp temp 06-12-2006 15:23:54 3 来自 oracle 官方网站的 Read committed , Serializable 的解释 Isolation Level Description Read committed This is the default transaction isolation level. Each que
3、ry executed by a transaction sees only data that was committed before the query (not the transaction) began. An Oracle query never reads dirty (uncommitted) data. Because Oracle does not prevent other transactions from modifying the data read by a query, that data can be changed by other transaction
4、s between two executions of the query. Thus, a transaction that runs a given query twice can experience both nonrepeatable read and phantoms. Serializable Serializable transactions see only those changes that were committed at the time the transaction began, plus those changes made by the transactio
5、n itself through INSERT , UPDATE , and DELETE statements. Serializable transactions do not experience nonrepeatable reads or phantoms. 2 者的区别也是来自官方网站 summarizes key differences between read committed and serializable transactions in Oracle. Table 13-2 Read Committed and Serializable Transactions Rea
6、d Committed Serializable Dirty write Not possible Not possible Dirty read Not possible Not possible Nonrepeatable read Possible Not possible Phantoms Possible Not possible 上面的 2 个表来自 http:/download- 都可以随时查询 Isolation Level Description Read committed This is the default transaction isolation level. E
7、ach query executed by a transaction sees only data that was committed before the query (not the transaction) began. An Oracle query never reads dirty (uncommitted) data. Because Oracle does not prevent other transactions from modifying the data read by a query, that data can be changed by other tran
8、sactions between two executions of the query. Thus, a transaction that runs a given query twice can experience both nonrepeatable read and phantoms. 默认的隔离级别设置。事务中的查询只能看到在此查询之前( 而非事务开始之前 )提交的数据。 由于 oracle 不会因为查询数据而阻止另外一个事务修改数据,因此数据可以在一个事务中的 2 次查询中,查到不同的结果。因此 可能出现 nonrepeatable read and phantoms 的情况 S
9、erializable Serializable transactions see only those changes that were committed at the time the transaction began, plus those changes made by the transaction itself through INSERT , UPDATE , and DELETE statements. Serializable transactions do not experience nonrepeatable reads or phantoms. 根绝我的理解解释
10、一下: serializable transactions 在事务执行: 2 次同一条数据查询的时候(就是两次执行查询,就是说执行完第一个 .executeQuery ,然后执行第二个 .executeQuery ),如果在第一个 .executeQuery 开始执行而另外一个事务已经开始修改数据,并且已经提交,那么两次读取的数据是另外一个事务修改前的数据。 如果在第一个 .executeQuery 之前,另外一个事务修改了数据,那么两次读取的数据是另外一个事务修改后的数据。 这恰恰反映了, repeatable read ,两次结果一致 这与 Read committed 完全不同, 要是
11、Read committed ,第一个 .executeQuery 未执行完第二事务,而在第二个 .executeQuery 前第二个事务执行完毕,那么第一个 .executeQuery 得到的是初始数据,而第二个 .executeQuery 得到的是修改后的数据 恰恰说明了 nonrepeatable read ,两次结果不一致的情况 以上 2 点都会保证不能脏读脏写,就是说不能得到另外一个事务修改没有提交的事务的修改后的数据。 用一个例子来解释一下 BaseTestCase package test.transaction; import java.sql.Co
12、nnection; import java.sql.DriverManager; import junit.framework.TestCase; public class BaseTestCase extends TestCase protected Connection conn = null; private String user = null; private String pwd = null; private String url = null; /* * override super setup
13、. */ protected void setUp() throws Exception super.setUp(); try Class.forName("oracle.jdbc.driver.OracleDriver"); catch (ClassNotFoundException e) e.printStackTrace(); url = "jdbc:oracle:thin:9:1521:aaaa" user = "loocky" pwd = "loocky" try conn = Dr
14、iverManager.getConnection(url, user, pwd); catch (Exception e) e.printStackTrace(); protected void tearDown() throws Exception super.tearDown(); try if(conn!=null) if(!conn.isClosed() conn.close(); catch(Exception e) e.printStackTrace(); finally if(!conn.isClosed() conn.close();
15、 TestTransaction0 package test.transaction; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; public class TestTransaction0 extends BaseTestCase protected void setUp() throws Exception super.setUp(); protected void tearDown()
16、 throws Exception super.tearDown(); public void test0() try System.out.println(this.getClass().getName(); conn.setAutoCommit(false); conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); String sql1="update sys_dbinfo set item='bbb' where id =1" ; PreparedStatement p
17、s1= conn.prepareStatement(sql1); ps1.executeUpdate(); ps1.close(); String sql2 ="select item from sys_dbinfo where id =1" PreparedStatement ps2= conn.prepareStatement(sql2); ResultSet rs2 = ps2.executeQuery(); rs2.next(); System.out.println(rs2.getString(1); rs2.close(); ps2.close(); mit()
18、; System.out.println(this.getClass().getName(); catch (Exception e) e.printStackTrace(); TestTransaction1 package test.transaction; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; public class TestTransaction1 extends BaseT
19、estCase protected void setUp() throws Exception super.setUp(); protected void tearDown() throws Exception super.tearDown(); public void test1() try System.out.println(this.getClass().getName(); conn.setAutoCommit(false); conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE); String sql2 = "sel
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 聘用项目总工合同
- 个体诊所聘用合同
- 预定租房合同协议
- 摄像摄影服务合同
- 工地门卫劳动合同
- 劳务派遣人员劳动合同书
- 13足球运球绕杆 教学设计 -九年级体育与健康
- 天津音乐学院《写作与沟通》2023-2024学年第二学期期末试卷
- 江苏科技大学《大数据处理框架》2023-2024学年第二学期期末试卷
- 对外经济贸易大学《儿童文学与中学语文教学》2023-2024学年第二学期期末试卷
- 一年级美术课后辅导教案-1
- 智慧社区建设中的智能化医疗与康养服务
- 2023-2024年人教版八年级上册数学期末模拟试卷(含答案)
- 数据采集管理制度范文
- 幼儿园小班开学家长会课件
- 中药抗骨质疏松作用
- 中建综合支吊架施工方案
- 建筑施工规范大全
- 幼儿园开学家长会PPT模板(含完整内容)
- 表冷器更换施工方案
- 沥青集料筛分反算计算表格(自动计算)
评论
0/150
提交评论