`
sunbin
  • 浏览: 342439 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

jdbc事务处理

    博客分类:
  • jdbc
阅读更多

 

 

1:事务的特征:ACID

   atomic :原子性:一个事务是一个不可分割的单位

   consitency  一致性:事务开始结束之后,数据库完整性约束没有被破坏

约束:PK FK not null check unique

isolaction 隔离性:一个事务的执行,不被其它事务干扰

durable 持久性:数据应该持久保存

 

2事务的边界Autocommit

Autocommit :事务的自动的提交:

   JDBC默认是自动提交事务,Autocommit=true

   默认情况下事务的边界:

   对于insert update delete 是在statement执行完

   对于select resultSet 关闭

autocommit 手动提交Autocommit=false

 只能手动设置事务的边界,也就是手动提交事务

conn.commit();

   Autocommit=false,如果不手动提交事务,那么connection 在显示关闭的时候提交了事务

   如果没有显示关闭的connection,事务是不会提交,

   connection autoCommit 不要默认true的情况,

   设置为falsecatch异常一定要要rollback

 

3:事务的隔离级别

   问题:

     脏读:允许读取到别的事务未提交的数据

     不可重复读:同一事务在2此读取一条数据,读取后结果不一致

     幻象读:在一个事务中读取别的事务插入进来的数据

  

oracle 默认支持read Comitted serializable    

    

transaction read uncomitted

     可以读取到别的事务未提交的数据

 transaction  comitted

     只能读取到别的事务已经提交数据

 transaction repeatable read

      在一个事务中多次读取同一条记录结果是重复的

 transaction  serializable 

       在一个事务中,读取数据,如果别的事务插入的数据,

  每此读取到相同的结果

 

jdbc 3.0规范中(请自行到www.jcp.org查阅)提到jdbc支持五中事务隔离级别

原文

1. TRANSACTION_NONE — indicates that the driver does not support transactions,which means that it is not a JDBC compliant driver.
    2. TRANSACTION_READ_UNCOMMITTED — allows transactions to see uncommittedchanges to the data. This means that dirty reads, nonrepeatable reads, and phantom reads are possible.
    3. TRANSACTION_READ_COMMITTED — means that any changes made inside atransaction are not visible outside the transaction until the transaction iscommitted. This prevents dirty reads, but nonrepeatable reads and phantom reads are still possible.
    4. TRANSACTION_REPEATABLE_READ — disallows dirty reads and nonrepeatablereads. Phantom read are still possible.

5. TRANSACTION_SERIALIZABLE — specifies that dirty reads, nonrepeatable reads,and phantom reads are prevented.

 

 

 

 

 

问题

隔离级别

脏读

不可重复读

幻读

未提交读

Y

Y

Y

提交读

N

Y

Y

可重复读

N

N

Y

序列化读

N

N

N

 

4SavePoint

Savepoint :给事务提供了更细粒度的控制

            通过metadata 可以取到dirver是否支持metadata

 

 

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics