Spring手动提交事务

时间:2021-08-12 11:25:33

  

// name的值根据spring配置文件的事物管理器的id而定
@Resource(name="transactionManager")
private DataSourceTransactionManager transactionManager; public boolean save(Entity e) {
DefaultTransactionDefinition transDefinition = new DefaultTransactionDefinition();
   transDefinition.setPropagationBehavior(DefaultTransactionDefinition.PROPAGATION_REQUIRES_NEW);
TransactionStatus transStatus = transactionManager.getTransaction(transDefinition);
try {
  /**
    写自己的操作数据库的代码
  /
  transactionManager.commit(transStatus);
} catch (Exception e) {
transactionManager.rollback(transStatus);
} }