spring开启事务的方式
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
@Service("transactionService")
public class TransactionService {
private static final Logger logger = LoggerFactory.getLogger(TransactionService.class);
/**
* 事务管理
*/
@Autowired
private PlatformTransactionManager baiTTransactionManager;
/**
* 事务开始
*
* @return
*/
public TransactionStatus init() {
long start = System.currentTimeMillis();
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
def.setIsolationLevel(DefaultTransactionDefinition.ISOLATION_READ_COMMITTED);
TransactionStatus ts = baiTTransactionManager.getTransaction(def);
long end = System.currentTimeMillis();
logger.info("---start transaction cost:{}", (end - start));
return ts;
}
/**
* 事务提交
*
* @param status
*/
public void commit(TransactionStatus status) {
if (status != null) {
baiTiaoTransactionManager.commit(status);
}
}
/**
* 回滚事务
*
* @param status
*/
public void rollback(TransactionStatus status) {
if (status != null) {
baiTiaoTransactionManager.rollback(status);
}
}
}
说明:
实例化对象
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
设置事务的传播行为为PROPAGATION_REQUIRED(已经存在事务 就用已有的 否则创建新事物)
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
设置事务的隔离级别为读已提交
def.setIsolationLevel(DefaultTransactionDefinition.ISOLATION_READ_COMMITTED);
开启事务
TransactionStatus ts = baiTTransactionManager.getTransaction(def);