博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
数据库事务性
阅读量:7004 次
发布时间:2019-06-27

本文共 4010 字,大约阅读时间需要 13 分钟。

TPL:事务控制语句

start transaction:开启一次事务

rollback:回滚
commit:提交事务

JDBC中与事务有关的方法:

Connection.setAutoCommit(boolean b)
Connection.rollback()
Connection.rollback(Savepoint sp)
Connection.commit();

*****四、事务的特性(隔离级别)
A:原子性。说明事务是一个不可分割的单位。
C:一致性.事务必须使数据库从一个一致性状态变换到另外一个一致性状态.(比如转账)
*I:隔离性。一个事务不能被其他事务打扰。
D:持久性。事务一旦提交,就应该被永久保存起来。

如果不考虑事务的隔离级别,会出现以下“不正确”的情况:

脏读:指一个事务读到了另一个事务中未提交的数据。
不可重复读:针对一条记录的,同一条记录前后不一样
虚读(幻读):针对一张表,前后读到的记录条数不一样。

MySQL中控制事务隔离级别的语句:

select @@tx_isolation; //查看当前的事务隔离级别
set transaction isolation level 你的级别(四种之一);//设置隔离级别

 

隔离级别的分类:
READ UNCOMMITTED(未授权读):脏读、不可重复读、虚读都有可能发生。
READ COMMITTED(授权读):能避免脏读,不可重复读、虚读都有可能发生。
REPEATABLE READ(可重复读):能避免脏读、不可重复度,虚读都有可能发生。
SERIALIZABLE(序列化):能避免脏读、不可重复度、虚读。

 

oracle隔离级别 只有READ COMMITTED和 SERIALIZABLE

因为当有人对数据库的数据进行任何写操作(DML操作)时,Oracle数据库系统首先将原始的数据复制到回滚段中,之后才做相应的操作,在事务处理结束之前其他的用户可以读这些数据,但是读到的都是回滚段上的数据。

1 create table bank2 {3   id int primary key,4   name varchar(10),5   money int6 }
View Code

搭建数据库连接

c3p0-config.xml下

1 
2
3
4
com.mysql.jdbc.Driver
5
jdbc:mysql://127.0.0.1:3306/bank
6
root
7
mima
8
9
View Code

JdbcUtil.java   导入相关的类

1 public class JdbcUtil {2     private static ComboPooledDataSource dataSource =new ComboPooledDataSource();3     public static ComboPooledDataSource getDataSource()4     {5         return dataSource;6     }7 }
View Code

测试代码 。 注意 事务是针对一次连接Connection的,如果

QueryRunner runner =new  QueryRunner(ds);    的话可能对于连接 会有不同的Connecton就达不到效果了
1     public static void main(String[] args) throws Exception  { 2         DataSource ds=JdbcUtil.getDataSource(); 3         Connection con=ds.getConnection(); 4         QueryRunner runner =new  QueryRunner();         5         try { 6             con.setAutoCommit(false); 7         } catch (SQLException e1) { 8             e1.printStackTrace(); 9         }10         try {11             String sql1="update money_table set money=money+1000 where name = ?";12             runner.update(con,sql1, new Object[]{"aa"});13             int i=1/0;14             String sql2="update money_table set money=money-1000 where name = ?";15             runner.update(con,sql2, new Object[]{"bb"});16             con.commit();17         18         } catch (Exception e) {19             try {20                 con.rollback();21             } catch (SQLException e1) {22                 e1.printStackTrace();23             }24             e.printStackTrace();25         }26         finally27         {28             29         }30 31     }

数据正常。

 

1     public static void main(String[] args) throws Exception  { 2         DataSource ds=JdbcUtil.getDataSource(); 3         Connection con=ds.getConnection(); 4         QueryRunner runner =new  QueryRunner();         5         try { 6             con.setAutoCommit(false); 7         } catch (SQLException e1) { 8             e1.printStackTrace(); 9         }10         Savepoint sp=null;11         try {12             String sql1="update money_table set money=money+1000 where name = ?";13             runner.update(con,sql1, new Object[]{"aa"});    14             String sql2="update money_table set money=money-1000 where name = ?";15             runner.update(con,sql2, new Object[]{"bb"});16             sp=con.setSavepoint();17             int i=1/0;18             String sql3="update money_table set money=money+1000 where name = ?";19             runner.update(con,sql3, new Object[]{"cc"});20             String sql4="update money_table set money=money-1000 where name = ?";21             runner.update(con,sql4, new Object[]{"dd"});22         23         24         } catch (Exception e) {25             try {26                 con.rollback(sp);27             } catch (SQLException e1) {28                 e1.printStackTrace();29             }30             e.printStackTrace();31         }32         finally33         {34             con.commit();35         }36 37     }

 

转载地址:http://lnutl.baihongyu.com/

你可能感兴趣的文章