1、事务的四种属性:
- 原子性(Atomicity)
- 一致性(Consistency)
- 隔离性(Isolation)
- 持久性(Durability)
2、数据库的三种并发问题:
- 脏读
- 不可重复读
- 幻读
3、数据库的四种隔离级别
- READ_UNCOMMITTED(读未提交)
- READ_COMMITTED(读已提交)
- REPEATABLE_READ(可重复读)
- SERIALIZABLE(串行化)
public class TransactionTest {
@Test
public void testTransaction(){
Connection connection = null;
try {
connection = JDBCUtils.getConnection();
//取消数据的自动提交功能
connection.setAutoCommit(false);
String sql1="";
JDBCUtils.generalUpdate(connection,sql1);
String sql2="";
JDBCUtils.generalUpdate(connection,sql2);
//提交数据
connection.commit();
} catch (Exception e) {
e.printStackTrace();
//回滚数据
try {
connection.rollback();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
} finally {
//打开自动提交
try {
connection.setAutoCommit(true);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
JDBCUtils.closeResource(connection,null);
}
}
}