JAVA设置手动提交事务,回滚事务,提交事务的操作
我就废话不多说啦,还是直接看代码吧!
/**
*设置数据库是否自动提交事务
*@paramflag
*@throwsSQLException
*/
publicvoidsetAutoCommit(booleanflag)throwsSQLException{
con.setAutoCommit(flag);
}
/**
*提交
*@throwsSQLException
*/
publicvoidcommit()throwsSQLException{
con.commit();
}
/**
*回滚
*@throwsSQLException
*/
publicvoidrollback()throwsSQLException{
con.rollback();
}
定义一个全局变量Connection第一个方法设置为false就是手动提交,这种方法适用于我们删除东西后重新再添加东西,类似权限管理系统这种可以用得上
补充知识:springboot手动开启事务,分段提交
我就废话不多说了,直接看代码吧!
ListordLogSynList=ordLogSynMapper.batchQuery("AP","20190926","0","1000"); for(inti=0;i<2;i++){ DefaultTransactionDefinitiondef=newDefaultTransactionDefinition(); def.setPropagationBehavior(DefaultTransactionDefinition.PROPAGATION_REQUIRES_NEW); TransactionStatusstatus=platformTransactionManager.getTransaction(def); List subList=null; if(i==0){ subList=ordLogSynList.subList(0,500); }elseif(i==1){ subList=ordLogSynList.subList(501,1000); } for(OrdLogSynordLogSyn:subList){ intq=ordLogSynMapper.updateChkFlag(ordLogSyn.getConfirmSeqId(),ordLogSyn.getAcctDate(),"I"); System.out.println("q="+q); } platformTransactionManager.commit(status); }
以上这篇JAVA设置手动提交事务,回滚事务,提交事务的操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。