项目中遇到一个问题,就是在程序的执行过程中需要不断地更新某个信息,但是在springmvc中好像是默认不可以的,那么就需要手动提交
// 从spring容器对象中获取DataSourceTransactionManager public DataSourceTransactionManager getDTM(DefaultTransactionDefinition definition){ //获取Spring容器的对象 WebApplicationContext contextLoader = ContextLoader.getCurrentWebApplicationContext(); //设置事务的传播行为,此处是设置为开启一个新事物 definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW); //设置事务的隔离级别,此处是读已经提交 definition.setIsolationLevel(TransactionDefinition.ISOLATION_READ_COMMITTED); //从spring容器对象中获取DataSourceTransactionManager,这个根据配置文件中配置的id名(transactionManager) DataSourceTransactionManager transactionManager = (DataSourceTransactionManager) contextLoader.getBean("transactionManager"); return transactionManager; } //获取事务状态对象 public TransactionStatus getTS(DataSourceTransactionManager transactionManager,DefaultTransactionDefinition definition){ TransactionStatus transactionStatus = (TransactionStatus) transactionManager.getTransaction(definition); return transactionStatus; } // 尝试手动提交事务 //设置属性的默认属性 DefaultTransactionDefinition definition1 = new DefaultTransactionDefinition(); // 获取DataSourceTransactionManager DataSourceTransactionManager transactionManager1 = getDTM(definition1); //获取事务状态对象 TransactionStatus transactionStatus1 = getTS(transactionManager,definition1); iExtRulecheckLogDao.update(extRulecheckLog1); // 提交事务 transactionManager.commit(transactionStatus);