1、Read uncommitted(读未提交):可能出现脏读、不可重复读和幻读。
create table account( id int(36) primary key comment '主键', card_id varchar(16) unique comment '卡号', name varchar(8) not null comment '姓名', balance float(10,2) default 0 comment '余额' )engine=innodb; insert into account (id,card_id,name,balance) values (1,'6226090219290000','Tom',1000);
public class Boss { public static void main(String[] args) { Connection connection = null; Statement statement = null; try { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://127.0.0.1:3306/test"; connection = DriverManager.getConnection(url, "root", "root"); connection.setAutoCommit(false); statement = connection.createStatement(); String sql = "update account set balance=balance+5000 where card_id='6226090219290000'"; statement.executeUpdate(sql); Thread.sleep(30000);//30秒后发现工资发错了 connection.rollback(); sql = "update account set balance=balance+2000 where card_id='6226090219290000'"; statement.executeUpdate(sql); connection.commit(); } catch (Exception e) { e.printStackTrace(); } finally { //释放资源 } } }
public class Employye { public static void main(String[] args) { Connection connection = null; Statement statement = null; ResultSet resultSet = null; try { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://127.0.0.1:3306/test"; connection = DriverManager.getConnection(url, "root", "root"); statement = connection.createStatement(); String sql = "select balance from account where card_id='6226090219290000'"; resultSet = statement.executeQuery(sql); if(resultSet.next()) { System.out.println(resultSet.getDouble("balance")); } } catch (Exception e) { e.printStackTrace(); } finally { //释放资源 } } }
运行结果:6000------> 30秒阻塞后,运行结果为3000