在SSM框架中添加事務

首先在 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();
		}





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章