MYsql單獨使用mybatis事務處理的無法回滾問題
阿新 • • 發佈:2019-01-05
mysql有幾種引擎,當使用InnoDB的時候,才可以進行事務處理,在navicat中如下設定:
1:進入表設計頁面,選項按鈕頁面
2:修改資料庫引擎為InnoDB
3:mybatis進行事務處理的程式碼如下:
package com.better517na.LogCollection.dao.impl; import java.sql.SQLException; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.transaction.Transaction; import org.apache.ibatis.transaction.TransactionFactory; import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory; import com.better517na.LogCollection.dao.WriteACCindexDao; import com.better517na.LogCollection.model.MLogAcc; import com.better517na.LogCollection.service.base.LoadSpring; import com.better517na.LogCollection.util.SqlUtil; import com.better517na.logcompontent.model.MLogException; import com.better517na.logcompontent.util.ExceptionLevel; public class WriteACCindexDaoImpl implements WriteACCindexDao { @Override public boolean writeACClogToDB(List<MLogAcc> mLogAccs) { int result = -1; TransactionFactory transactionFactory = new JdbcTransactionFactory(); // 事務工廠 SqlSession session = null; Transaction newTransaction = null; try { session = SqlUtil.getAccSqlSessionFactory().openSession(); newTransaction = transactionFactory.newTransaction(session .getConnection()); Map<String, Object> map = new HashMap<String, Object>(); map.put("logAccList", mLogAccs); map.put("tableName", mLogAccs.get(0).getTableName()); result = session.insert("writeACClogToDB", map); } catch (Exception e) { try { newTransaction.rollback(); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } e.printStackTrace(); LoadSpring.getLogBusiness().writeExceptionLog( new MLogException(ExceptionLevel.Error, "123", e)); return false; } finally { try { newTransaction.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return result > 0 ? true : false; } }
4:mybatis的mapper.xml檔案如下:
<p><?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "<a target=_blank href="http://mybatis.org/dtd/mybatis-3-mapper.dtd">http://mybatis.org/dtd/mybatis-3-mapper.dtd</a>"> <!-- 這是mybatis的sql對映檔案 --> <mapper namespace="com.better517na.LogCollection.dao.WriteACCindexDao"></p><p> <insert id="writeACClogToDB" parameterType="Map"> INSERT INTO ${tableName}( LogID, TimeTicks, TrackID, TimePoint, TimePeriod, Sequence, Direction, LocalIP, MachineName, ServiceAddress, AppName, Method, Description, ExceptionID, ExceptionLevel, Paras, ReturnValue, Key1, Key2, Key3 ) values <foreach collection="logAccList" item="item" index="index" separator=","> ( #{item.logID}, #{item.timeTicks}, #{item.trackID}, #{item.timePoint}, #{item.timePeriod}, #{item.sequence}, #{item.direction}, #{item.localIP}, #{item.machineName}, #{item.serviceAddress}, #{item.appName}, #{item.method}, #{item.description}, #{item.exceptionID}, #{item.exceptionLevel}, #{item.paras}, #{item.returnValue}, #{item.key1}, #{item.key2}, #{item.key3}) </foreach> </insert></p><p></mapper></p>