I've seen articles saying that we should try to limit the scope of transaction, e.g. instead of doing this:
@Transactional
public void save(User user) {
queryData();
addData();
updateData();
}
We should exclude queryData
from the transaction by using Spring's TransactionTemplate
(or just move it out of the transactional method):
@Autowired
private TransactionTemplate transactionTemplate;
public void save(final User user) {
queryData();
transactionTemplate.execute((status) => {
addData();
updateData();
return Boolean.TRUE;
})
}
But my understanding is that since JDBC will always need a transaction for all operations, if I use the second way, there will be 2 transactions opened and closed, 1 for queryData
(opened by JDBC), and another for codes inside transactionTemplate.execute
opened by our class. If so, won't this be a waste of resources now that you've split 1 transaction into 2?