TransactionUtils
类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.interceptor.DefaultTransactionAttribute;
@Component
public class TransactionUtils {
@Autowired
private DataSourceTransactionManager dataSourceTransactionManager;
/**
* begin事务
*
* @return
*/
public TransactionStatus begin() {
TransactionStatus transaction = dataSourceTransactionManager.
getTransaction(new DefaultTransactionAttribute());
return transaction;
}
/**
* 提交事务
*/
public void commit(TransactionStatus transaction) {
dataSourceTransactionManager.commit(transaction);
}
/**
* 回滚事务
*
* @param transaction
*/
public void rollback(TransactionStatus transaction) {
dataSourceTransactionManager.rollback(transaction);
}
}