时间:2020-11-13 08:55:41 | 栏目:JAVA代码 | 点击:次
我就废话不多说啦,还是直接看代码吧!
/** * 设置数据库是否自动提交事务 * @param flag * @throws SQLException */ public void setAutoCommit(boolean flag) throws SQLException { con.setAutoCommit(flag); } /** * 提交 * @throws SQLException */ public void commit() throws SQLException { con.commit(); } /** * 回滚 * @throws SQLException */ public void rollback() throws SQLException { con.rollback(); }
定义一个全局变量Connection 第一个方法设置为false就是手动提交,这种方法适用于我们删除东西后重新再添加东西,类似权限管理系统这种可以用得上
补充知识:springboot 手动开启事务,分段提交
我就废话不多说了,直接看代码吧!
List<OrdLogSyn> ordLogSynList = ordLogSynMapper.batchQuery("AP", "20190926", "0", "1000"); for (int i = 0; i < 2; i++) { DefaultTransactionDefinition def = new DefaultTransactionDefinition(); def.setPropagationBehavior(DefaultTransactionDefinition.PROPAGATION_REQUIRES_NEW); TransactionStatus status = platformTransactionManager.getTransaction(def); List<OrdLogSyn> subList = null; if (i==0){ subList = ordLogSynList.subList(0, 500); }else if (i==1){ subList = ordLogSynList.subList(501, 1000); } for (OrdLogSyn ordLogSyn : subList) { int q = ordLogSynMapper.updateChkFlag(ordLogSyn.getConfirmSeqId(), ordLogSyn.getAcctDate(), "I"); System.out.println("q = " + q); } platformTransactionManager.commit(status); }