开启声明式事务
1.在启动类上增加注解来
@EnableTransactionManagement
2.在需要使用事务的方法上增加注解
@Transactional
注解的参数说明
value/transactionManager 传递事务管理器 接受string类型数据 事务管理器一般在在我们使用数据源并自定义配置的注入的时候,需要注入 数据源、数据源池对象、和事务管理器,事务管理器就是从这里注入(自动配置的时候就是spring帮忙注入的了)
propation 事务传播的机制 接受Propagation类型数据 默认传播机制是 /** * Enumeration that represents transaction propagation behaviors for use * with the {@link Transactional} annotation, corresponding to the * {@link TransactionDefinition} interface. * 该枚举类作为向Transactional 注解传递的参数,主要决定事务的传播机制 * @author Colin Sampaleanu * @author Juergen Hoeller * @since 1.2 */ public enum Propagation { /** *支持现有的事务,如果没有事务就创建新的事务 * Support a current transaction, create a new one if none exists. * Analogous to EJB transaction attribute of the same name. * <p>This is the default setting of a transaction annotation. 是默认的事务传递机制 */ REQUIRED(TransactionDefinition.PROPAGATION_REQUIRED), /** *支持当前已经存在的事务,如果没有事务就以非事务的方式来执行 * Support a current transaction, execute non-transactionally if none exists. * 和EJB的同名事务属性相似 * Analogous to EJB transaction attribute of the same name. *注意supports的事务传播机制和完全没有事务的执行是不同的,因为它定义了需要同步的事务范围 *因此,相同的资源将共享整个需要同步的事务范围。注意,这实际上应该取决于事务管理器的真实的同步配置。 * <p>Note: For transaction managers with transaction synchronization, * PROPAGATION_SUPPORTS is slightly different from no transaction at all, * as it defines a transaction scope that synchronization will apply for. * As a consequence, the same resources (JDBC Connection, Hibernate Session, etc) * will be shared for the entire specified scope. Note that this depends on * the actual synchronization configuration of the transaction manager. * @see #setTransactionSynchronization */ SUPPORTS(TransactionDefinition.PROPAGATION_SUPPORTS), /** *支持当前已经存在的事务,如果没有事务就抛出一个异常。 * Support a current transaction, throw an exception if none exists. * Analogous to EJB transaction attribute of the same name. */ MANDATORY(TransactionDefinition.PROPAGATION_MANDATORY), /** * 创建一个新的事务同时暂停当前存在的事务 * Create a new transaction, and suspend the current transaction if one exists. * Analogous to the EJB transaction attribute of the same name. * 注意:实际上事务暂停并不是对于所有的事务管理器开箱即用,这一点对于 * 特别适用,它需要事务管理器设置这一点对他可 * 用。 * <p><b>NOTE:</b> Actual transaction suspension will not work out-of-the-box * on all transaction managers. This in particular applies to * {@link }, * which requires the {@code } to be * made available it to it (which is server-specific in standard Java EE). * @see #setTransactionManager */ REQUIRES_NEW(TransactionDefinition.PROPAGATION_REQUIRES_NEW), /** * 以非事务的方式执行,如果已经存在事务就暂停当前事务 * Execute non-transactionally, suspend the current transaction if one exists. * Analogous to EJB transaction attribute of the same name. *事务暂停并不是对于所有事务管理器开箱即用的,JtaTransactionManager需要做相应的配置来生效 * <p><b>NOTE:</b> Actual transaction suspension will not work out-of-the-box * on all transaction managers. This in particular applies to * {@link }, * which requires the {@code } to be * made available it to it (which is server-specific in standard Java EE). * @see #setTransactionManager */ NOT_SUPPORTED(TransactionDefinition.PROPAGATION_NOT_SUPPORTED), /** * 以非事务的方式执行,如果当前存在事务就抛出异常 * Execute non-transactionally, throw an exception if a transaction exists. * Analogous to EJB transaction attribute of the same name. */ NEVER(TransactionDefinition.PROPAGATION_NEVER), /** * 如果已经存在事务就以嵌套事务的方式执行,如果不存在事务就创建一个新的事务 * 嵌套事务仅在特定的事务管理器上被支持,在以JDBC3.0 driver运行的事务管理器上的 * DataSourceTransactionManager 嵌套事务是开箱即用的,有些JTA可能也支持嵌套事务。 * Execute within a nested transaction if a current transaction exists, * behave like PROPAGATION_REQUIRED else. There is no analogous feature in EJB. * <p>Note: Actual creation of a nested transaction will only work on specific * transaction managers. Out of the box, this only applies to the JDBC * DataSourceTransactionManager when working on a JDBC 3.0 driver. * Some JTA providers might support nested transactions as well. * @see */ NESTED(TransactionDefinition.PROPAGATION_NESTED); private final int value; Propagation(int value) { = value; } public int value() { return ; } }
事务的隔离级别 默认隔离级别 因为只适用于新事务,所以事务的隔离级别仅使用于 Propagation.REQUIRES_NEW和两种传播机制 如果你希望在参与多个不同的隔离级别的事务的时候,被拒绝,那就将validateExistingTransactions配置为True Isolation isolation() default ; 说明该枚举类被应用于Transactional作为隔离级别被传入 public enum Isolation { 将会使用底层数据库的默认隔离级别 所有的隔离级别都类似于数据库中的隔离级别 DEFAULT(TransactionDefinition.ISOLATION_DEFAULT), 读未提交:事务可以读取到其他事务未提交的数据 可能存在脏读、不可重复读和幻读 脏读:可能出现一个事务读取了另外一个事务修改后回滚的数据,此时读取到的就是脏数据 READ_UNCOMMITTED(TransactionDefinition.ISOLATION_READ_UNCOMMITTED), 读已提交:事务只能读取到其他事务已经提交的数据 脏读被防止,可能存在不可充读和幻读 不可重复读:一个事务读取信息后,另外一个事务修改并提交,第一个事务再次读取,对于第一个事务来说两次读取内容不一致 READ_COMMITTED(TransactionDefinition.ISOLATION_READ_COMMITTED), 可重复读:事务的读取和更新加锁,读取过程中不允许更新 脏读和不可重复读被防止,可能存在幻读, 幻读:一个事务读取信息后,第二个事务进行删除或者新增,第一个事务重复读取数据的时候,数据的行数不一致了。 REPEATABLE_READ(TransactionDefinition.ISOLATION_REPEATABLE_READ), 串行话,事务串行执行,不会出现脏读、不可重复读和幻读 SERIALIZABLE(TransactionDefinition.ISOLATION_SERIALIZABLE);
一个标志位来表示事务是有效的只读,允许在运行时做相应的优化 这是对实际的事务子系统的一个提醒,如果事务子系统不能识别这个只读标志位,将会默默忽略它而不是抛出异常 boolean readOnly() default false;
指定了抛出什么异常时事务会回滚,默认是,该配置优先于rollbackForClassName Class<? extends Throwable>[] rollbackFor() default {};
指定了抛出什么异常时事务会回滚,默认是 String[] rollbackForClassName() default {};
指定了抛出什么异常是不需要回滚的,该配置是首选,优先于noRollbackForClassName Class<? extends Throwable>[] noRollbackFor() default {};
指定了抛出什么异常是不需要回滚的. String[] noRollbackForClassName() default {};