TransactionManager - Types
|
|
|
|---|---|
|
|
|
@Transactional commonly works with thread-bound transactions managed by PlatformTransactionManager, exposing a transaction to all data access operations within the current execution thread. Note: This does not propagate to newly started threads within the method.
A reactive transaction managed by ReactiveTransactionManager uses the Reactor context instead of thread-local attributes. As a consequence, all participating data access operations need to execute within the same Reactor context in the same reactive pipeline
TransactionManager - Code Examples
PlatformTransactionManager
PlatformTransactionManager - Configuration
@Configuration @EnableTransactionManagement // <-------------------------------- (1) public class MySpringConfig { @Bean public DataSource dataSource() { return new MysqlDataSource(); // <----------------------- (2) } @Bean public PlatformTransactionManager transactionManager() { return new DataSourceTransactionManager(dataSource()); // (3) } }
- @EnableTransactionManagement enables Spring’s annotation-driven transaction management capability (e.g.
@Transactional)- create a database-specific or connection-pool specific
DataSourcehere (MySQL is being used for this example)- create
TransactionManager, which needs aDataSourceto be able to manage transactions (for in-depth understanding see otherPlatformTransactionManagerimplementations that Spring offers)PlatformTransactionManager - Usage (Programmatically)
@Service public class UserService { @Autowired private TransactionTemplate template; public Long registerUser(User user) { DefaultTransactionDefinition def = new DefaultTransactionDefinition(); // explicitly setting the transaction name is something that can only be done programmatically def.setName("SomeTxName"); def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); TransactionStatus status = transactionManager.getTransaction(def); try { // execute your business logic here } catch (MyException ex) { transactionManager.rollback(status); throw ex; } transactionManager.commit(status); } }PlatformTransactionManager - Usage (Declaratively)
TODO
ReactiveTransactionManager
PlatformTransactionManager - Configuration
TODOReactiveTransactionManager - Usage (Programmatically)
TODOReactiveTransactionManager - Usage (Declaratively)
TODO