Spring supports a software transaction that gives us subtle control over TX management. According to the Spring Documentation, you can use TX programmatic control:
1. Using the Spring TransactionTemplate:
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
protected void doInTransactionWithoutResult(TransactionStatus status) {
try {
updateOperation1();
updateOperation2();
} catch (SomeBusinessExeption ex) {
status.setRollbackOnly();
}
} });
2. Using the PlatformTransactionManager directly (implement the PlatformTransactionManager implementation in the DAO):
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setName("SomeTxName");
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
//txManager is a reference to PlatformTransactionManager
TransactionStatus status = txManager.getTransaction(def);
try {
updateOperation1();
updateOperation2();
}
catch (MyException ex) {
txManager.rollback(status);
throw ex;
}
txManager.commit(status);
to simplify, let's say we are dealing with a JDBC database operation.
I am interested in what kind of database operations occurred in updateOperation1(),updateOperation2()the second fragment, is it either implemented using JDBCTemplateor JDBCDaoSupport, if not, the operation is not actually performed in any transaction, is it?
, JDBCTemplate JDBCDaoSupport, / . , , , PlatformTransactionManager .
Spring , , PlatformTransactionManager , ConnectionHolder, TransactionSynchronizationManager. , JDBCTemplate JDBCDaoSupport, TransactionSynchronizationManager.
TransactionSynchronizationManager , ( Threadlocal, , )
, , , PlatformTransactionManager JDBCTemplate JDBCDaoSupport, , , Spring updateOperation1(),updateOperation2(), .
? , Spring ?