异步任务中使用spring事务

// 设置属性的默认属性
DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
// 设置事务的传播行为,此处是设置为开启一个新事务
definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
// 设置事务的隔离级别,此处是读提交
definition.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED);
// 从spring容器对象中获取DataSourceTransactionManager,这个根据配置文件中配置的id名(transactionManager)
DataSourceTransactionManager transactionManager = SpringContextUtils.getBean("transactionManager",
		DataSourceTransactionManager.class);
// 获取事务状态
TransactionStatus transactionStatus = (TransactionStatus) transactionManager.getTransaction(definition);
//TransactionStatus transaction = TransactionAspectSupport.currentTransactionStatus();
// 创建回滚点
Object point = transactionStatus.createSavepoint();

try {
	// 业务处理
	......
	// 提交事务
	transactionManager.commit(transactionStatus);
} catch(Exception e) {
	// 回滚
	transactionStatus.rollbackToSavepoint(point);
}
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页