`
hu2004hx
  • 浏览: 20767 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Spring 源码 TransactionDefinition 定义(助于理解Spring事务传播,事务的隔离级别)

阅读更多
/**
 * Interface that defines Spring-compliant transaction properties.
 * Based on the propagation behavior definitions analogous to EJB CMT attributes.
 *
 * <p>Note that isolation level and timeout settings will not get applied unless
 * an actual new transaction gets started. As only {@link #PROPAGATION_REQUIRED},
 * {@link #PROPAGATION_REQUIRES_NEW} and {@link #PROPAGATION_NESTED} can cause
 * that, it usually doesn't make sense to specify those settings in other cases.
 * Furthermore, be aware that not all transaction managers will support those
 * advanced features and thus might throw corresponding exceptions when given
 * non-default values.
 *
 * <p>The {@link #isReadOnly() read-only flag} applies to any transaction context,
 * whether backed by an actual resource transaction or operating non-transactionally
 * at the resource level. In the latter case, the flag will only apply to managed
 * resources within the application, such as a Hibernate <code>Session</code>.
 *
 * @author Juergen Hoeller
 * @since 08.05.2003
 * @see PlatformTransactionManager#getTransaction(TransactionDefinition)
 * @see org.springframework.transaction.support.DefaultTransactionDefinition
 * @see org.springframework.transaction.interceptor.TransactionAttribute
 */
public interface TransactionDefinition {

	/**
	 * Support a current transaction; create a new one if none exists.
	 * Analogous to the EJB transaction attribute of the same name.
	 * <p>This is typically the default setting of a transaction definition,
	 * and typically defines a transaction synchronization scope.
	 */
	int PROPAGATION_REQUIRED = 0;

	/**
	 * Support a current transaction; execute non-transactionally if none exists.
	 * Analogous to the EJB transaction attribute of the same name.
	 * <p><b>NOTE:</b> For transaction managers with transaction synchronization,
	 * <code>PROPAGATION_SUPPORTS</code> is slightly different from no transaction
	 * at all, as it defines a transaction scope that synchronization might apply to.
	 * As a consequence, the same resources (a JDBC <code>Connection</code>, a
	 * Hibernate <code>Session</code>, etc) will be shared for the entire specified
	 * scope. Note that the exact behavior depends on the actual synchronization
	 * configuration of the transaction manager!
	 * <p>In general, use <code>PROPAGATION_SUPPORTS</code> with care! In particular, do
	 * not rely on <code>PROPAGATION_REQUIRED</code> or <code>PROPAGATION_REQUIRES_NEW</code>
	 * <i>within</i> a <code>PROPAGATION_SUPPORTS</code> scope (which may lead to
	 * synchronization conflicts at runtime). If such nesting is unavoidable, make sure
	 * to configure your transaction manager appropriately (typically switching to
	 * "synchronization on actual transaction").
	 * @see org.springframework.transaction.support.AbstractPlatformTransactionManager#setTransactionSynchronization
	 * @see org.springframework.transaction.support.AbstractPlatformTransactionManager#SYNCHRONIZATION_ON_ACTUAL_TRANSACTION
	 */
	int PROPAGATION_SUPPORTS = 1;

	/**
	 * Support a current transaction; throw an exception if no current transaction
	 * exists. Analogous to the EJB transaction attribute of the same name.
	 * <p>Note that transaction synchronization within a <code>PROPAGATION_MANDATORY</code>
	 * scope will always be driven by the surrounding transaction.
	 */
	int PROPAGATION_MANDATORY = 2;

	/**
	 * Create a new transaction, suspending 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 org.springframework.transaction.jta.JtaTransactionManager},
	 * which requires the <code>javax.transaction.TransactionManager</code>
	 * to be made available it to it (which is server-specific in standard J2EE).
	 * <p>A <code>PROPAGATION_REQUIRES_NEW</code> scope always defines its own
	 * transaction synchronizations. Existing synchronizations will be suspended
	 * and resumed appropriately.
	 * @see org.springframework.transaction.jta.JtaTransactionManager#setTransactionManager
	 */
	int PROPAGATION_REQUIRES_NEW = 3;

	/**
	 * Do not support a current transaction; rather always execute non-transactionally.
	 * 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 org.springframework.transaction.jta.JtaTransactionManager},
	 * which requires the <code>javax.transaction.TransactionManager</code>
	 * to be made available it to it (which is server-specific in standard J2EE).
	 * <p>Note that transaction synchronization is <i>not</i> available within a
	 * <code>PROPAGATION_NOT_SUPPORTED</code> scope. Existing synchronizations
	 * will be suspended and resumed appropriately.
	 * @see org.springframework.transaction.jta.JtaTransactionManager#setTransactionManager
	 */
	int PROPAGATION_NOT_SUPPORTED = 4;

	/**
	 * Do not support a current transaction; throw an exception if a current transaction
	 * exists. Analogous to the EJB transaction attribute of the same name.
	 * <p>Note that transaction synchronization is <i>not</i> available within a
	 * <code>PROPAGATION_NEVER</code> scope.
	 */
	int PROPAGATION_NEVER = 5;

	/**
	 * Execute within a nested transaction if a current transaction exists,
	 * behave like {@link #PROPAGATION_REQUIRED} else. There is no analogous
	 * feature in EJB.
	 * <p><b>NOTE:</b> Actual creation of a nested transaction will only work on
	 * specific transaction managers. Out of the box, this only applies to the JDBC
	 * {@link org.springframework.jdbc.datasource.DataSourceTransactionManager}
	 * when working on a JDBC 3.0 driver. Some JTA providers might support
	 * nested transactions as well.
	 * @see org.springframework.jdbc.datasource.DataSourceTransactionManager
	 */
	int PROPAGATION_NESTED = 6;//支持当前事务,新增Savepoint点,与当前事务同步提交或回滚。它只对DataSourceTransactionManager有效。


	/**
	 * Use the default isolation level of the underlying datastore.
	 * All other levels correspond to the JDBC isolation levels.
	 * @see java.sql.Connection
	 */
	int ISOLATION_DEFAULT = -1;

	/**
	 * Indicates that dirty reads, non-repeatable reads and phantom reads
	 * can occur.
	 * <p>This level allows a row changed by one transaction to be read by another
	 * transaction before any changes in that row have been committed (a "dirty read").
	 * If any of the changes are rolled back, the second transaction will have
	 * retrieved an invalid row.
	 * @see java.sql.Connection#TRANSACTION_READ_UNCOMMITTED
	 */
	int ISOLATION_READ_UNCOMMITTED = Connection.TRANSACTION_READ_UNCOMMITTED;

	/**
	 * Indicates that dirty reads are prevented; non-repeatable reads and
	 * phantom reads can occur.
	 * <p>This level only prohibits a transaction from reading a row
	 * with uncommitted changes in it.
	 * @see java.sql.Connection#TRANSACTION_READ_COMMITTED
	 */
	int ISOLATION_READ_COMMITTED = Connection.TRANSACTION_READ_COMMITTED;

	/**
	 * Indicates that dirty reads and non-repeatable reads are prevented;
	 * phantom reads can occur.
	 * <p>This level prohibits a transaction from reading a row with uncommitted changes
	 * in it, and it also prohibits the situation where one transaction reads a row,
	 * a second transaction alters the row, and the first transaction re-reads the row,
	 * getting different values the second time (a "non-repeatable read").
	 * @see java.sql.Connection#TRANSACTION_REPEATABLE_READ
	 */
	int ISOLATION_REPEATABLE_READ = Connection.TRANSACTION_REPEATABLE_READ;

	/**
	 * Indicates that dirty reads, non-repeatable reads and phantom reads
	 * are prevented.
	 * <p>This level includes the prohibitions in {@link #ISOLATION_REPEATABLE_READ}
	 * and further prohibits the situation where one transaction reads all rows that
	 * satisfy a <code>WHERE</code> condition, a second transaction inserts a row
	 * that satisfies that <code>WHERE</code> condition, and the first transaction
	 * re-reads for the same condition, retrieving the additional "phantom" row
	 * in the second read.
	 * @see java.sql.Connection#TRANSACTION_SERIALIZABLE
	 */
	int ISOLATION_SERIALIZABLE = Connection.TRANSACTION_SERIALIZABLE;


	/**
	 * Use the default timeout of the underlying transaction system,
	 * or none if timeouts are not supported. 
	 */
	int TIMEOUT_DEFAULT = -1;


	/**
	 * Return the propagation behavior.
	 * <p>Must return one of the <code>PROPAGATION_XXX</code> constants
	 * defined on {@link TransactionDefinition this interface}.
	 * @return the propagation behavior
	 * @see #PROPAGATION_REQUIRED
	 * @see org.springframework.transaction.support.TransactionSynchronizationManager#isActualTransactionActive()
	 */
	int getPropagationBehavior();

	/**
	 * Return the isolation level.
	 * <p>Must return one of the <code>ISOLATION_XXX</code> constants
	 * defined on {@link TransactionDefinition this interface}.
	 * <p>Only makes sense in combination with {@link #PROPAGATION_REQUIRED}
	 * or {@link #PROPAGATION_REQUIRES_NEW}.
	 * <p>Note that a transaction manager that does not support custom isolation levels
	 * will throw an exception when given any other level than {@link #ISOLATION_DEFAULT}.
	 * @return the isolation level
	 */
	int getIsolationLevel();

	/**
	 * Return the transaction timeout.
	 * <p>Must return a number of seconds, or {@link #TIMEOUT_DEFAULT}.
	 * <p>Only makes sense in combination with {@link #PROPAGATION_REQUIRED}
	 * or {@link #PROPAGATION_REQUIRES_NEW}.
	 * <p>Note that a transaction manager that does not support timeouts will throw
	 * an exception when given any other timeout than {@link #TIMEOUT_DEFAULT}.
	 * @return the transaction timeout
	 */
	int getTimeout();

	/**
	 * Return whether to optimize as a read-only transaction.
	 * <p>The read-only flag applies to any transaction context, whether
	 * backed by an actual resource transaction
	 * ({@link #PROPAGATION_REQUIRED}/{@link #PROPAGATION_REQUIRES_NEW}) or
	 * operating non-transactionally at the resource level
	 * ({@link #PROPAGATION_SUPPORTS}). In the latter case, the flag will
	 * only apply to managed resources within the application, such as a
	 * Hibernate <code>Session</code>.
<<	 * <p>This just serves as a hint for the actual transaction subsystem;
	 * it will <i>not necessarily</i> cause failure of write access attempts.
	 * A transaction manager which cannot interpret the read-only hint will
	 * <i>not</i> throw an exception when asked for a read-only transaction.
	 * @return <code>true</code> if the transaction is to be optimized as read-only 
	 * @see org.springframework.transaction.support.TransactionSynchronization#beforeCommit(boolean)
	 * @see org.springframework.transaction.support.TransactionSynchronizationManager#isCurrentTransactionReadOnly()
	 */
	boolean isReadOnly();

	/**
	 * Return the name of this transaction. Can be <code>null</code>.
	 * <p>This will be used as the transaction name to be shown in a
	 * transaction monitor, if applicable (for example, WebLogic's).
	 * <p>In case of Spring's declarative transactions, the exposed name will be
	 * the <code>fully-qualified class name + "." + method name</code> (by default).
	 * @return the name of this transaction
	 * @see org.springframework.transaction.interceptor.TransactionAspectSupport
	 * @see org.springframework.transaction.support.TransactionSynchronizationManager#getCurrentTransactionName()
	 */
	String getName();

}
分享到:
评论

相关推荐

    spring学习事务源码

    本文将深入探讨Spring事务管理的源码,理解其背后的实现机制。 首先,Spring事务管理有两种主要模式:编程式事务管理和声明式事务管理。编程式事务管理通过调用`PlatformTransactionManager`接口提供的方法进行显式...

    Spring源码解析.zip

    本压缩包“Spring源码解析”提供了对Spring框架核心组件——IOC(Inversion of Control,控制反转)、AOP(Aspect Oriented Programming,面向切面编程)以及Transaction(事务管理)的源码分析,帮助开发者更全面地...

    spring-tx事物源码

    2. **TransactionDefinition**: 定义了事务的属性,如隔离级别、传播行为、超时和是否只读。例如,`PROPAGATION_REQUIRED`是默认的传播行为,表示如果当前存在事务,则加入该事务;如果不存在,则创建一个新的事务。...

    Spring源码分析.rar

    通过对Spring源码的深入分析,我们可以了解到Spring是如何实现这些功能的,这将有助于我们在开发过程中更好地利用Spring,解决实际问题,提升系统性能。同时,源码阅读也能锻炼我们的设计思维和编程能力,让我们能站...

    深入分析 Spring 源码(第四阶段)

    具体到事务管理的源码分析,Spring通过定义TransactionDefinition接口来描述事务的定义信息,包含事务的传播行为(PropagationBehavior)和事务的隔离级别(IsolationLevel)。传播行为决定了事务在遇到其他事务时的...

    全面分析 Spring 的编程式事务管理及声明式事务管理

    1. **TransactionDefinition**: 这个接口定义了事务的属性,如隔离级别、事务超时时间、是否只读等。通过TransactionDefinition,我们可以定制事务的行为。 2. **TransactionStatus**: 提供了事务的状态信息,如...

    Spring事务处理-ThreadLocal的使用

    本篇文章将聚焦于Spring事务处理中ThreadLocal的使用,以及如何通过源码理解和应用这个工具。 首先,了解Spring事务管理的基本概念。在多线程环境中,事务管理是至关重要的,它负责确保一组数据库操作要么全部成功...

    spring-jdbc源码

    5. TransactionDefinition和PlatformTransactionManager:定义了事务的属性,如隔离级别、超时时间等,而PlatformTransactionManager则是事务管理的接口,DataSourceTransactionManager实现了这个接口。 6. ...

    spring框架的学习--事务

    - **基于XML的声明式事务管理**:在Spring的配置文件中,使用`&lt;tx:advice&gt;`、`&lt;aop:config&gt;`和`&lt;bean&gt;`标签定义事务规则,如事务的传播行为、隔离级别、超时设置等。 - **基于注解的声明式事务管理**:通过在...

    Spring2.0 事务处理

    2. **声明式事务管理**:这是Spring最常用且推荐的方式,它通过在方法级别或类级别使用特定的注解(如`@Transactional`)来定义事务边界。这大大简化了代码,并将事务管理逻辑从业务代码中分离出来。Spring 2.0引入...

    spring JDBC事务管理

    关键类如`TransactionDefinition`定义了事务属性,如隔离级别、超时时间等;`TransactionStatus`接口则表示当前事务的状态。在事务管理器内部,Spring会根据这些信息创建并维护事务。 **工具使用**:Spring提供了...

    spring src 源码

    源码分析有助于深入理解其工作原理,提升编程技能,以及解决实际问题时能够更有效地调试和优化。下面将对Spring 2.5版本的源码进行详细的解析。 1. **核心模块**:Spring的核心模块主要包括`core-container`和`...

    spring源码包-源码包源码包

    深入学习 Spring 源码有助于开发者更好地理解框架的工作机制,优化应用性能,解决疑难问题,甚至参与框架的扩展和定制。通过阅读源码,我们能领略到 Spring 设计模式的巧妙运用,例如工厂模式、代理模式、装饰器模式...

    spring 的3种事务配置方式

    在Spring中,事务的传播行为包括`PROPAGATION_REQUIRED`(默认,必须在一个事务中运行)、`PROPAGATION_SUPPORTS`(如果存在事务,则加入,否则非事务运行)、`PROPAGATION_MANDATORY`(必须在一个存在的事务中运行...

    第八节-spring-事物源码解析1

    - **TransactionDefinition**:此接口用于定义事务的属性,如隔离级别、传播行为、超时和只读标志: - **隔离级别**:定义了事务之间的可见性,例如`READ_UNCOMMITTED`、`READ_COMMITTED`、`REPEATABLE_READ`和`...

    spring 事务

    关于标签中的“源码”,探究Spring事务管理的源码可以深入理解其实现原理。例如,分析TransactionProxyFactoryBean和AOP代理是如何实现声明式事务的;查看PlatformTransactionManager接口及其实现类,如...

    最全的Spring考题与答案

    - TransactionDefinition:定义事务属性,如隔离级别、传播行为等。 - TransactionStatus:表示当前事务状态,支持提交、回滚等操作。 - PlatformTransactionManager:事务管理基础接口,...

    spring jdbc 事务

    2. 在业务代码中,通过TransactionDefinition定义事务属性(如隔离级别、超时时间等)。 3. 使用TransactionTemplate或者直接调用PlatformTransactionManager的方法来管理事务的生命周期。 **声明式事务管理**: 1....

    Spring+源码+深度解析

    源码解析涉及TransactionDefinition、PlatformTransactionManager、TransactionStatus等接口,以及事务传播行为的定义。 5. **Spring MVC** Spring MVC是Spring提供的Web层解决方案,通过DispatcherServlet、...

    spring3.2.0源码

    Spring框架是Java开发中不可或缺的一部分,它以其模块化、易用性和灵活性著称。Spring 3.2.0是该框架的一个重要版本,包含了多个...同时,源码阅读也能帮助我们理解如何利用Spring框架来构建更高效、可维护的Java应用。

Global site tag (gtag.js) - Google Analytics