一、简述事物处理
1、事物处理的基本概念
1)提交:所有操作步骤都被完整执行后,称该事物被提交
2)回滚:某步操作执行失败,所有操作都没被提交,则事物必须被回滚
2、事物处理的特性(ACID)
1)原子性
2)一致性
3)隔离性
4)持久性
二、事物处理的3种方式
1、关系型数据库的事物处理
1)Begin Transaction(启动事务处理)
2)Commit或RollBack(提交或回滚)
3)End Transaction(提交事务处理)
2、传统的JDBC事务处理
package com.gc.action; import org.junit.Test; import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; /** * Created by sky on 16-7-18. */ public class JdbcHelloWorld { DataSource dataSource; //获取数据源 public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; } @Test public void test() { Connection conn = null; Statement stmt = null; try {//获取数据库连接 dataSource.getConnection(); //开始启动事务 //Sets this connection's auto-commit mode to the given state. conn.setAutoCommit(false); //Creates a Statement object for sending SQL statements to the database. conn.createStatement(); //执行相应的操作 stmt.executeUpdate("INSERT INTO hello VALUES (1, 'gf', 'HelloWorld')"); //执行成功则提交事务 conn.commit(); } catch (SQLException e1) { if (conn != null) { try { //执行不成功则回滚 conn.rollback(); } catch (SQLException e2) { System.out.println("数据库连接有异常" + e2); } } } finally { //加入stmt不为空,则关闭stmt if (stmt != null) { try { stmt.close(); } catch (SQLException e3) { System.out.println("执行操作有异常" + e3); } } if (conn != null) { try { conn.close(); } catch (SQLException e4) { System.out.println("数据连接有异常" + e4); } } } } }
3、分布式事务处理
原子性、多个事务管理器合作
三、Spring的事务处理
1、Spring事务处理的概述
Spring中事务处理实际上是基于动态AOP机制实现
package org.springframework.transaction; import org.springframework.transaction.TransactionDefinition; import org.springframework.transaction.TransactionException; import org.springframework.transaction.TransactionStatus; public interface PlatformTransactionManager {
//目前的事务
TransactionStatus getTransaction(TransactionDefinition var1) throws TransactionException;
void commit(TransactionStatus var1) throws TransactionException;
void rollback(TransactionStatus var1) throws TransactionException;
}
TransactionDefinition代表着事务处理的一些属性定义
package org.springframework.transaction; public interface TransactionDefinition { int PROPAGATION_REQUIRED = 0; int PROPAGATION_SUPPORTS = 1; int PROPAGATION_MANDATORY = 2; int PROPAGATION_REQUIRES_NEW = 3; int PROPAGATION_NOT_SUPPORTED = 4; int PROPAGATION_NEVER = 5; int PROPAGATION_NESTED = 6; int ISOLATION_DEFAULT = -1; int ISOLATION_READ_UNCOMMITTED = 1; int ISOLATION_READ_COMMITTED = 2; int ISOLATION_REPEATABLE_READ = 4; int ISOLATION_SERIALIZABLE = 8; int TIMEOUT_DEFAULT = -1; //获得事务的传播行为 int getPropagationBehavior(); //获得事务的隔离层次 int getIsolationLevel(); //获得事务是否超时 int getTimeout(); //判断是否为只读事务 boolean isReadOnly(); //返回一个事务的名字 String getName(); }
TransactionStatus代表了目前的事务
package org.springframework.transaction; import java.io.Flushable; import org.springframework.transaction.SavepointManager; public interface TransactionStatus extends SavepointManager, Flushable { //判断是否是一个事务 boolean isNewTransaction(); boolean hasSavepoint(); //设定为只读事务 void setRollbackOnly(); //判断是否为只读事务 boolean isRollbackOnly(); void flush(); //判断一个事务是否完成 boolean isCompleted(); }