MYsql单独使用mybatis事务处理的无法回滚问题

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>


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值