Last active
June 1, 2024 13:38
-
-
Save agoston/9953322 to your computer and use it in GitHub Desktop.
JdbcTemplate manual transaction example, using TransactionTemplate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//[... initialize ...] | |
final SimpleDataSourceFactory simpleDataSourceFactory = new SimpleDataSourceFactory("com.mysql.jdbc.Driver"); | |
final DataSource dataSource = simpleDataSourceFactory.createDataSource(jdbcUrl, user, pass); | |
jdbcTemplate = new JdbcTemplate(dataSource); | |
final DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(dataSource); | |
transactionTemplate = new TransactionTemplate(transactionManager); | |
transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); | |
//[... and then ...] | |
transactionTemplate.execute(new TransactionCallback<Object>() { | |
@Override | |
public Object doInTransaction(TransactionStatus status) { | |
jdbcTemplate.execute(...) // will be executed in transaction | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Finally...
an easy to understand & straightforward way to initialise jdbc template without spring.