Common Scenarios Where Spring @Transactional Does Not Take Effect
This article explains eight typical situations—such as using non‑transactional storage engines, missing Spring bean registration, non‑public methods, self‑invocation, absent transaction manager, incorrect propagation settings, swallowed exceptions, and wrong exception types—that cause the @Transactional annotation in Spring to appear ineffective, and provides practical solutions for each.
When using Spring's @Transactional annotation, developers often encounter cases where the transaction seems to be ignored, leading to data not being persisted after an insert or update. This article lists eight common scenarios that create this illusion and offers remedies.
1. Database engine does not support transactions
MySQL's MyISAM engine lacks transaction support; use InnoDB instead. The default engine from MySQL 5.5.5 onward is InnoDB, which supports transactions.
2. Class not managed by Spring
If the class is not annotated as a Spring bean, the @Transactional annotation has no effect.
// @Service
public class OrderServiceImpl implements OrderService {
@Transactional
public void updateOrder(Order order) {
// update order
}
}3. Method is not public
Spring proxies only apply transactional behavior to public methods. Non‑public methods will not trigger the transaction unless AspectJ mode is used.
When using proxies, you should apply the @Transactional annotation only to methods with public visibility.
4. Self‑invocation
Calling a transactional method from within the same class bypasses the proxy, so the transaction does not start.
// Example 1
@Service
public class OrderServiceImpl implements OrderService {
public void update(Order order) {
updateOrder(order);
}
@Transactional
public void updateOrder(Order order) {
// update order
}
}
// Example 2
@Service
public class OrderServiceImpl implements OrderService {
@Transactional
public void update(Order order) {
updateOrder(order);
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void updateOrder(Order order) {
// update order
}
}Both examples fail because the internal call does not go through the Spring proxy.
5. Data source without a transaction manager
If the configured data source lacks a PlatformTransactionManager, transactions cannot be managed.
@Bean
public PlatformTransactionManager transactionManager(DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}6. Incorrect propagation configuration
Using Propagation.NOT_SUPPORTED disables transaction support for the method, making any @Transactional annotation ineffective.
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public void updateOrder(Order order) {
// update order
}7. Swallowed exceptions
If an exception is caught and not re‑thrown, Spring cannot trigger a rollback.
@Transactional
public void updateOrder(Order order) {
try {
// update order
} catch (Exception e) {
// exception swallowed, no rollback
}
}8. Wrong exception type for rollback
Spring rolls back only on unchecked exceptions by default. To roll back on checked exceptions, specify rollbackFor in the annotation.
@Transactional(rollbackFor = Exception.class)
public void updateOrder(Order order) throws Exception {
// update order
throw new Exception("Update error");
}Summary
The most frequent causes of transaction failure are self‑invocation, swallowed exceptions, and using the wrong exception type. Understanding these scenarios helps ensure that @Transactional works as intended.
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.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
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.
