spring transaction manager

spring 编程控制事务

public <T> T execute(TransactionCallback<T> action) throws TransactionException {
if (this.transactionManager instanceof CallbackPreferringPlatformTransactionManager) {
return ((CallbackPreferringPlatformTransactionManager) this.transactionManager).execute(this, action);
}
else {
TransactionStatus status = this.transactionManager.getTransaction(this);
T result;
try {
result = action.doInTransaction(status);
}
catch (RuntimeException ex) {
// Transactional code threw application exception -> rollback
rollbackOnException(status, ex);
throw ex;
}
catch (Error err) {
// Transactional code threw error -> rollback
rollbackOnException(status, err);
throw err;
}
catch (Exception ex) {
// Transactional code threw unexpected exception -> rollback
rollbackOnException(status, ex);
throw new UndeclaredThrowableException(ex, "TransactionCallback threw undeclared checked exception");

}
this.transactionManager.commit(status);
return result;
}
}


来看一下PlatformTransactionManager interface是怎么声明的

public interface PlatformTransactionManager {

/**
* Return a currently active transaction or create a new one, according to
* the specified propagation behavior.
* <p>Note that parameters like isolation level or timeout will only be applied
* to new transactions, and thus be ignored when participating in active ones.
* <p>Furthermore, not all transaction definition settings will be supported
* by every transaction manager: A proper transaction manager implementation
* should throw an exception when unsupported settings are encountered.
* <p>An exception to the above rule is the read-only flag, which should be
* ignored if no explicit read-only mode is supported. Essentially, the
* read-only flag is just a hint for potential optimization.
* @param definition TransactionDefinition instance (can be {@code null} for defaults),
* describing propagation behavior, isolation level, timeout etc.
* @return transaction status object representing the new or current transaction
* @throws TransactionException in case of lookup, creation, or system errors
* @throws IllegalTransactionStateException if the given transaction definition
* cannot be executed (for example, if a currently active transaction is in
* conflict with the specified propagation behavior)
* @see TransactionDefinition#getPropagationBehavior
* @see TransactionDefinition#getIsolationLevel
* @see TransactionDefinition#getTimeout
* @see TransactionDefinition#isReadOnly
*/
TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException;

/**
...
*/
void commit(TransactionStatus status) throws TransactionException;

/**
...
*/
void rollback(TransactionStatus status) throws TransactionException;

}

要使用编程事务管理,我们要做的就是实现TransactionCallback接口,spring 中的很多接口都是遵循ISP原则的,用匿名类来实现这样接口是很方便的,例如spring-jdbc 中的很多接口
Rowmapper,ParameterMapper,PreparedStatementCallback......

public interface TransactionCallback<T> {

/**
...
*/
T doInTransaction(TransactionStatus status);

}

伪代码如下
new TransactionCallback() {
public Object doInTransaction(TransactionStatus status) {
try{
operation1();
operation2();
operation3();
}catch(Exception e){
status.setRollbackOnly();
}
return null;
}
};

当然要使用spring的事务管理还需要实例化TransactioinTemplate,在application.xml文件中配置,不赘述。

猜你喜欢

转载自huangcongweng.iteye.com/blog/1847095