在SSM框架中添加事务
其他
2018-05-30 18:37:38
阅读次数: 0
首先在 applicationContext-mybatis.xml 添加一个事务批处理的bean
<bean class="org.mybatis.spring.SqlSessionTemplate" id="sqlSession">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory" />
<constructor-arg name="executorType" value="BATCH" />
</bean>
// 新获取一个模式为BATCH,自动提交为false的session
// 如果自动提交设置为true,将无法控制提交的条数,改为最后统一提交,可能导致内存溢出
SqlSession session = sst.getSqlSessionFactory().openSession(ExecutorType.BATCH,false);
moMapper= session.getMapper(MOMapper.class);
中间写事务内容
try {
for (PDA_JWL_INTERFACE_H in : inList) {
moMapper.insert(in);
}
cc.put("zyh", zyh);
moMapper.call(cc);
System.out.println("存储过程执行完毕");
session.commit();
//清理缓存,防止溢出
session.clearCache();
return true;
} catch (Exception e) {
session.rollback();
return false;
} finally {
session.close();
}
转载自blog.csdn.net/ccemma/article/details/79106933