Spring事务管理核心:
1.TransactionDefinition:定义一次事务传播行为,隔离级别,是否只读,本次事务名称
/** * 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() * 最终会调用connection.setReadOnly方法,然后不能执行更新操作 */ 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();
2.TransactionStatus: 事务状态
/** * Return whether the present transaction is new (else participating * in an existing transaction, or potentially not running in an * actual transaction in the first place). * 是不是一个新的事务,即是不是一个独立的事务 */ boolean isNewTransaction(); /** * Return whether this transaction internally carries a savepoint, * that is, has been created as nested transaction based on a savepoint. * <p>This method is mainly here for diagnostic purposes, alongside * {@link #isNewTransaction()}. For programmatic handling of custom * savepoints, use SavepointManager's operations. * @see #isNewTransaction() * @see #createSavepoint * @see #rollbackToSavepoint(Object) * @see #releaseSavepoint(Object) * 是否有保存点 */ boolean hasSavepoint(); /** * Set the transaction rollback-only. This instructs the transaction manager * that the only possible outcome of the transaction may be a rollback, as * alternative to throwing an exception which would in turn trigger a rollback. * <p>This is mainly intended for transactions managed by * {@link org.springframework.transaction.support.TransactionTemplate} or * {@link org.springframework.transaction.interceptor.TransactionInterceptor}, * where the actual commit/rollback decision is made by the container. * @see org.springframework.transaction.support.TransactionCallback#doInTransaction * @see org.springframework.transaction.interceptor.TransactionAttribute#rollbackOn * 将该事务设置成回滚,事务结束时会回滚 */ void setRollbackOnly(); /** * Return whether the transaction has been marked as rollback-only * (either by the application or by the transaction infrastructure). * 事务是不是设置了只允许回滚的属性 */ boolean isRollbackOnly(); /** * Flush the underlying session to the datastore, if applicable: * for example, all affected Hibernate/JPA sessions. */ void flush(); /** * Return whether this transaction is completed, that is, * whether it has already been committed or rolled back. * @see PlatformTransactionManager#commit * @see PlatformTransactionManager#rollback * 事务是否完成? */ boolean isCompleted();
默认的实现DefaultTransactionStatus
ublic class DefaultTransactionStatus extends AbstractTransactionStatus { private final Object transaction; //事务对象 private final boolean newTransaction; //是不是一个新事务即独立的事务 private final boolean newSynchronization;//? private final boolean readOnly;//是不是只读事务 private final boolean debug; //是不是日志级别为debug private final Object suspendedResources; //挂起的事务资源 /**如果已经存在A事务,方法B的事务传播级别为REQUIRED_NEW,则会将A事务的信息放在B事务/*的suspendedResources中,同时将B事务的newTransaction设置为true,保证这是一个新的事务,然后 /* B事务会获得到一个新的连接 */
3. PlatformTransactionManager
public interface PlatformTransactionManager { /** * Return a currently active transaction or create a new one, according to * the specified propagation behavior. * <p>Note that parameters like isolation level or timeout will only be applied * to new transactions, and thus be ignored when participating in active ones. * <p>Furthermore, not all transaction definition settings will be supported * by every transaction manager: A proper transaction manager implementation * should throw an exception when unsupported settings are encountered. * <p>An exception to the above rule is the read-only flag, which should be * ignored if no explicit read-only mode is supported. Essentially, the * read-only flag is just a hint for potential optimization. * @param definition TransactionDefinition instance (can be <code>null</code> for defaults), * describing propagation behavior, isolation level, timeout etc. * @return transaction status object representing the new or current transaction * @throws TransactionException in case of lookup, creation, or system errors * @throws IllegalTransactionStateException if the given transaction definition * cannot be executed (for example, if a currently active transaction is in * conflict with the specified propagation behavior) * @see TransactionDefinition#getPropagationBehavior * @see TransactionDefinition#getIsolationLevel * @see TransactionDefinition#getTimeout * @see TransactionDefinition#isReadOnly */ TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException; /** * Commit the given transaction, with regard to its status. If the transaction * has been marked rollback-only programmatically, perform a rollback. * <p>If the transaction wasn't a new one, omit the commit for proper * participation in the surrounding transaction. If a previous transaction * has been suspended to be able to create a new one, resume the previous * transaction after committing the new one. * <p>Note that when the commit call completes, no matter if normally or * throwing an exception, the transaction must be fully completed and * cleaned up. No rollback call should be expected in such a case. * <p>If this method throws an exception other than a TransactionException, * then some before-commit error caused the commit attempt to fail. For * example, an O/R Mapping tool might have tried to flush changes to the * database right before commit, with the resulting DataAccessException * causing the transaction to fail. The original exception will be * propagated to the caller of this commit method in such a case. * @param status object returned by the <code>getTransaction</code> method * @throws UnexpectedRollbackException in case of an unexpected rollback * that the transaction coordinator initiated * @throws HeuristicCompletionException in case of a transaction failure * caused by a heuristic decision on the side of the transaction coordinator * @throws TransactionSystemException in case of commit or system errors * (typically caused by fundamental resource failures) * @throws IllegalTransactionStateException if the given transaction * is already completed (that is, committed or rolled back) * @see TransactionStatus#setRollbackOnly */ void commit(TransactionStatus status) throws TransactionException; /** * Perform a rollback of the given transaction. * <p>If the transaction wasn't a new one, just set it rollback-only for proper * participation in the surrounding transaction. If a previous transaction * has been suspended to be able to create a new one, resume the previous * transaction after rolling back the new one. * <p><b>Do not call rollback on a transaction if commit threw an exception.</b> * The transaction will already have been completed and cleaned up when commit * returns, even in case of a commit exception. Consequently, a rollback call * after commit failure will lead to an IllegalTransactionStateException. * @param status object returned by the <code>getTransaction</code> method * @throws TransactionSystemException in case of rollback or system errors * (typically caused by fundamental resource failures) * @throws IllegalTransactionStateException if the given transaction * is already completed (that is, committed or rolled back) */ void rollback(TransactionStatus status) throws TransactionException; }
AbstractPlatformTransactionManager
/** * Always activate transaction synchronization, even for "empty" transactions * that result from PROPAGATION_SUPPORTS with no existing backend transaction. * @see org.springframework.transaction.TransactionDefinition#PROPAGATION_SUPPORTS * @see org.springframework.transaction.TransactionDefinition#PROPAGATION_NOT_SUPPORTED * @see org.springframework.transaction.TransactionDefinition#PROPAGATION_NEVER */ public static final int SYNCHRONIZATION_ALWAYS = 0; /** * Activate transaction synchronization only for actual transactions, * that is, not for empty ones that result from PROPAGATION_SUPPORTS with * no existing backend transaction. * @see org.springframework.transaction.TransactionDefinition#PROPAGATION_REQUIRED * @see org.springframework.transaction.TransactionDefinition#PROPAGATION_MANDATORY * @see org.springframework.transaction.TransactionDefinition#PROPAGATION_REQUIRES_NEW */ public static final int SYNCHRONIZATION_ON_ACTUAL_TRANSACTION = 1; /** * Never active transaction synchronization, not even for actual transactions. */ public static final int SYNCHRONIZATION_NEVER = 2; /** Constants instance for AbstractPlatformTransactionManager */ private static final Constants constants = new Constants(AbstractPlatformTransactionManager.class); protected transient Log logger = LogFactory.getLog(getClass()); private int transactionSynchronization = SYNCHRONIZATION_ALWAYS; private int defaultTimeout = TransactionDefinition.TIMEOUT_DEFAULT; private boolean nestedTransactionAllowed = false; private boolean validateExistingTransaction = false; private boolean globalRollbackOnParticipationFailure = true; private boolean failEarlyOnGlobalRollbackOnly = false; private boolean rollbackOnCommitFailure = false;
4. TransactionSynchronizationManager:资源管理,在同一线程的任一地方均可获取
private static final Log logger = LogFactory.getLog(TransactionSynchronizationManager.class); private static final ThreadLocal<Map<Object, Object>> resources = new NamedThreadLocal<Map<Object, Object>>("Transactional resources"); private static final ThreadLocal<Set<TransactionSynchronization>> synchronizations = new NamedThreadLocal<Set<TransactionSynchronization>>("Transaction synchronizations"); private static final ThreadLocal<String> currentTransactionName = new NamedThreadLocal<String>("Current transaction name"); private static final ThreadLocal<Boolean> currentTransactionReadOnly = new NamedThreadLocal<Boolean>("Current transaction read-only status"); private static final ThreadLocal<Integer> currentTransactionIsolationLevel = new NamedThreadLocal<Integer>("Current transaction isolation level"); private static final ThreadLocal<Boolean> actualTransactionActive = new NamedThreadLocal<Boolean>("Actual transaction active"); //------------------------------------------------------------------------- // Management of transaction-associated resource handles //-------------------------------------------------------------------------
相关推荐
这个名为"springTransaction.rar"的压缩包文件包含了一个关于Spring事务管理的小型示例,旨在演示如何使用Spring的事务传播机制来处理数据库操作,特别是转账功能的实现。 首先,让我们了解一下什么是事务。在...
在实际项目中,`springtransaction`工程可能是包含了一个完整的示例,用于演示如何在MyEclipse环境中配置和使用Spring的事务管理功能。开发者可以通过导入此工程,学习和实践Spring事务管理的配置与使用,从而更好地...
"spring-transaction.jar"正是提供了Spring事务管理的类库,它包含了一系列用于处理事务的接口、类和配置元素,使得开发者能够方便地实现事务控制。 一、Spring 事务管理概述 Spring事务管理分为编程式事务管理和...
本篇主要聚焦于"Spring 常用 Transaction Annotation",即声明式事务管理,这是一种更简洁、易于维护的事务控制方式。 首先,Spring的声明式事务管理基于AOP(面向切面编程),它允许我们在不修改业务代码的情况下...
12. Spring Transaction Management 13. Spring Batch 14. Spring NoSQL and Big Data 15. Spring Java Enterprise Services and Remoting Technologies 16. Spring Messaging 17. Spring Integration 18. Spring ...
使用 Spring Transaction 支持的简单 Spring MVC 应用程序,带有 @Transactional 注释和 JPA。 配置为与 Weblogic 事务管理器集成。 此示例应用程序仅用作配置了 Spring Transaction 支持的 Spring MVC 应用程序的...
org.springframework.transaction-3.2.2.RELEASE最新版本
### Spring中的Transaction事务传播行为种类详解 #### 一、引言 在开发基于Spring框架的应用程序时,事务管理是确保数据一致性的重要手段之一。Spring框架提供了丰富的事务管理功能,其中包括了事务传播行为...
6. **Spring Transaction**:事务管理模块提供了声明式和编程式的事务管理,确保了在分布式环境下的数据一致性。 7. **Spring Aspects**:此模块提供了AOP的扩展,支持自定义切面和通知类型,增强了Spring的面向切...
在Java企业级应用开发中,Spring框架以其强大的功能和灵活性被广泛应用,特别是在事务管理方面。Spring提供了全面的事务管理解决方案,使得开发者可以方便地控制事务的边界,保证数据的一致性和完整性。本篇将深入...
7. **Spring Transaction**: 提供了一致的事务管理接口,支持编程式和声明式事务管理。这使得事务管理可以跨不同的数据访问技术进行。 8. **Spring MVC**: 是Spring提供的用于构建Web应用的模型-视图-控制器(Model...
SpringTransaction是Spring框架的事务管理模块,它提供了一种声明式和编程式的事务管理方式。声明式事务管理允许我们在配置文件中定义事务边界,而无需在代码中显式控制事务开始和结束。编程式事务管理则通过...
6. **Spring Transaction**:提供了一种声明式和编程式事务管理机制,可以在多个数据库操作之间确保数据的一致性。 7. **Spring Web**:针对Web开发的模块,包含Spring MVC(Model-View-Controller)和Spring ...
3. **Spring Transaction** (spring-tx-4.1.6.RELEASE.jar): 事务管理是Spring的核心功能之一,它允许开发者声明性地管理事务,提供编程式和声明式的事务处理,支持多种事务API如JTA、JDBC、Hibernate等。...
- **功能简介**:包含了 Spring DAO、Spring Transaction 进行数据访问的所有类。 - **应用场景**:适用于需要进行数据访问操作的项目。 - **依赖关系**:依赖于 `spring-core.jar`、`spring-beans.jar`、`spring-...
- Spring Data JPA可以无缝地与Spring Boot、Spring MVC、Spring Transaction管理等组件集成,为开发者提供了完整的解决方案,降低了系统的复杂性。 然而,尽管Spring Data JPA带来了诸多便利,但在实际使用中也会...
7. **Spring Transaction**:提供了统一的事务管理接口,无论是本地事务还是分布式事务,都能进行统一的管理和控制。`@Transactional`注解使得事务管理变得简单。 8. **Spring Aspects**:提供了AOP的实现,包括...
Spring Transaction Management 事务管理是保证数据一致性的重要手段,Spring 提供了全面的事务管理功能,可以方便地应用于不同的业务场景中。 #### 12. Spring Batch 对于需要处理大量数据的任务,Spring Batch...
在思维导图"Spring Transaction.twd"中,可能包含了Spring事务管理的各个概念和它们之间的关系,如事务的ACID属性(原子性、一致性、隔离性和持久性),事务管理器,以及声明式和编程式事务管理的实现方式。...
6. **Integration with Spring Transaction Management**:Spring Data JPA与Spring的事务管理无缝集成,可以方便地进行事务控制。 在实际使用中,我们需要配置Spring Data JPA,这通常涉及到以下步骤: 1. 添加...