Unveiling Spring Transaction Mechanics: From Annotations to Core Interceptors
This article dives deep into Spring's transaction infrastructure, explaining how @EnableTransactionManagement activates transaction support, detailing the roles of key configuration classes, core interceptors, transaction attribute resolution, propagation behaviors, and the commit‑and‑rollback processes that power reliable data operations in Spring and Spring Boot applications.
1. Enabling Transaction Support
Spring enables transaction management through the @EnableTransactionManagement annotation. This annotation imports TransactionManagementConfigurationSelector, which selects either AutoProxyRegistrar and ProxyTransactionManagementConfiguration for PROXY mode or the AspectJ configuration for ASPECTJ mode.
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(TransactionManagementConfigurationSelector.class)
public @interface EnableTransactionManagement {
boolean proxyTargetClass() default false;
AdviceMode mode() default AdviceMode.PROXY;
int order() default Ordered.LOWEST_PRECEDENCE;
}The selector decides which configuration class to import based on the chosen advice mode.
public class TransactionManagementConfigurationSelector extends AdviceModeImportSelector<EnableTransactionManagement> {
@Override
protected String[] selectImports(AdviceMode adviceMode) {
switch (adviceMode) {
case PROXY:
return new String[]{AutoProxyRegistrar.class.getName(), ProxyTransactionManagementConfiguration.class.getName()};
case ASPECTJ:
return new String[]{TransactionManagementConfigUtils.TRANSACTION_ASPECT_CONFIGURATION_CLASS_NAME};
default:
return null;
}
}
}2. Core Interceptor – TransactionInterceptor
The TransactionInterceptor implements MethodInterceptor and delegates to invokeWithinTransaction to apply transaction semantics around method execution.
public class TransactionInterceptor extends TransactionAspectSupport implements MethodInterceptor, Serializable {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
Class<?> targetClass = (invocation.getThis() != null ? AopUtils.getTargetClass(invocation.getThis()) : null);
return invokeWithinTransaction(invocation.getMethod(), targetClass, invocation::proceed);
}
}The core logic resides in invokeWithinTransaction, which obtains the transaction attribute, determines the transaction manager, creates or joins a transaction, executes the target method, and handles commit or rollback.
protected Object invokeWithinTransaction(Method method, @Nullable Class<?> targetClass, final InvocationCallback invocation) throws Throwable {
TransactionAttributeSource tas = getTransactionAttributeSource();
TransactionAttribute txAttr = (tas != null ? tas.getTransactionAttribute(method, targetClass) : null);
PlatformTransactionManager tm = determineTransactionManager(txAttr);
String joinpointIdentification = methodIdentification(method, targetClass, txAttr);
if (txAttr == null || !(tm instanceof CallbackPreferringPlatformTransactionManager)) {
TransactionInfo txInfo = createTransactionIfNecessary(tm, txAttr, joinpointIdentification);
Object retVal = null;
try {
retVal = invocation.proceedWithInvocation();
} catch (Throwable ex) {
completeTransactionAfterThrowing(txInfo, ex);
throw ex;
} finally {
cleanupTransactionInfo(txInfo);
}
commitTransactionAfterReturning(txInfo);
return retVal;
} else {
// JTA transaction handling omitted for brevity
}
}3. Transaction Attribute Resolution
Spring resolves @Transactional attributes via AnnotationTransactionAttributeSource. It parses annotation properties into a RuleBasedTransactionAttribute instance.
public TransactionAttribute parseTransactionAnnotation(AnnotationAttributes attributes) {
RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute();
rbta.setPropagationBehavior(attributes.getEnum("propagation").value());
rbta.setIsolationLevel(attributes.getEnum("isolation").value());
rbta.setTimeout(attributes.getNumber("timeout").intValue());
rbta.setReadOnly(attributes.getBoolean("readOnly"));
rbta.setQualifier(attributes.getString("value"));
// rollback rules handling omitted for brevity
return rbta;
}The attribute source caches resolved attributes for performance.
public TransactionAttribute getTransactionAttribute(Method method, @Nullable Class<?> targetClass) {
if (method.getDeclaringClass() == Object.class) return null;
Object cacheKey = getCacheKey(method, targetClass);
TransactionAttribute cached = this.attributeCache.get(cacheKey);
if (cached != null) {
return (cached == NULL_TRANSACTION_ATTRIBUTE ? null : cached);
}
TransactionAttribute txAttr = computeTransactionAttribute(method, targetClass);
if (txAttr == null) {
this.attributeCache.put(cacheKey, NULL_TRANSACTION_ATTRIBUTE);
} else {
this.attributeCache.put(cacheKey, txAttr);
}
return txAttr;
}4. Propagation Behaviors
Spring supports seven propagation types (REQUIRED, NOT_SUPPORTED, SUPPORTS, MANDATORY, NEVER, REQUIRES_NEW, NESTED). The transaction manager decides whether to join an existing transaction, suspend it, or start a new one based on the selected mode.
REQUIRED – join if present, otherwise start a new transaction (default).
NOT_SUPPORTED – suspend any existing transaction and execute non‑transactionally.
SUPPORTS – join if present, otherwise execute without a transaction.
MANDATORY – must join an existing transaction, otherwise throw.
NEVER – must not run within a transaction, otherwise throw.
REQUIRES_NEW – suspend current transaction and start a fresh one.
NESTED – create a savepoint within the existing transaction.
5. Transaction Lifecycle – Commit
After successful method execution, Spring calls commitTransactionAfterReturning, which delegates to the transaction manager.
protected void commitTransactionAfterReturning(@Nullable TransactionInfo txInfo) {
if (txInfo != null && txInfo.getTransactionStatus() != null) {
txInfo.getTransactionManager().commit(txInfo.getTransactionStatus());
}
}The manager checks the transaction status, handles savepoints for nested transactions, and finally invokes doCommit on the underlying JDBC connection.
protected void doCommit(DefaultTransactionStatus status) {
DataSourceTransactionObject txObject = (DataSourceTransactionObject) status.getTransaction();
Connection con = txObject.getConnectionHolder().getConnection();
con.commit();
}6. Transaction Lifecycle – Rollback
If an exception occurs, completeTransactionAfterThrowing decides whether to roll back based on the rollbackOn rule (by default RuntimeException or Error).
public boolean rollbackOn(Throwable ex) {
return (ex instanceof RuntimeException || ex instanceof Error);
}When a rollback is required, processRollback either rolls back to a savepoint (nested case) or calls doRollback on the JDBC connection.
protected void doRollback(DefaultTransactionStatus status) {
DataSourceTransactionObject txObject = (DataSourceTransactionObject) status.getTransaction();
Connection con = txObject.getConnectionHolder().getConnection();
con.rollback();
}7. Spring Boot Auto‑Configuration
Spring Boot automatically registers @EnableTransactionManagement via TransactionAutoConfiguration in spring-boot-autoconfigure. The auto‑configuration creates a TransactionManagerCustomizers bean and configures a TransactionTemplate for programmatic transaction handling.
@Configuration
@ConditionalOnClass(PlatformTransactionManager.class)
@AutoConfigureAfter({JtaAutoConfiguration.class, HibernateJpaAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, Neo4jDataAutoConfiguration.class})
@EnableConfigurationProperties(TransactionProperties.class)
public class TransactionAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public TransactionManagerCustomizers platformTransactionManagerCustomizers(ObjectProvider<List<PlatformTransactionManagerCustomizer<?>>> customizers) {
return new TransactionManagerCustomizers(customizers.getIfAvailable());
}
@Configuration
@ConditionalOnSingleCandidate(PlatformTransactionManager.class)
static class TransactionTemplateConfiguration {
private final PlatformTransactionManager transactionManager;
public TransactionTemplateConfiguration(PlatformTransactionManager transactionManager) {
this.transactionManager = transactionManager;
}
@Bean
@ConditionalOnMissingBean
public TransactionTemplate transactionTemplate() {
return new TransactionTemplate(this.transactionManager);
}
}
// Additional inner configurations for JDK dynamic vs CGLIB proxy omitted for brevity
}8. Key Takeaways
Spring’s transaction system is built on annotation‑driven configuration, a flexible interceptor chain, and a robust attribute resolution mechanism. Understanding how @EnableTransactionManagement, the various configuration selectors, and the TransactionInterceptor cooperate helps developers troubleshoot common pitfalls such as incorrect propagation settings, missing savepoints, or unexpected rollbacks.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Su San Talks Tech
Su San, former staff at several leading tech companies, is a top creator on Juejin and a premium creator on CSDN, and runs the free coding practice site www.susan.net.cn.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
