MyBatis+MySql Insert操作失败解决办法

来源:互联网 发布:随机森林算法matlab 编辑:程序博客网 时间:2024/04/30 12:30

[转]MyBatis+MySql Insert操作失败解决办法

2013-5-21阅读238 评论0

初次接触MyBatis,遇到数据库使用MySql时Insert失败的问题(select时正常)。在日志中复制SQL语句是可以执行的,不知道什么原因,怀疑是事务问题。

google后看到一哥们也遇到了此问题,不过他找到了一个解决办法,下面是他原话(他的症状和我遇到的一样,不过我不知道哪里有问题,他知道是数据库引擎的问题~):

The Engine in MySql is innoDB. Which retrieves the data from the table successfully, but i cant insert the data, i cant get any exception or error.

which works fine in MyISAM type.

有他这段信息就好办了,下面是我的2个解决办法:

方法1:把数据库表都改成了MyISAM,果然数据插入成功了!

方法2:数据库引擎不变

搜了一些资料,看到说 innoDB是自动提交事务的,而MyBatis管理事务时默认不提交。

资料地址:

http://tech.b3log.org/articles/2012/05/29/1338271764507.html讲述innoDB和MyISAM的区别及MyBatis管理事务时默认不提交。

http://sishuok.com/forum/blogPost/list/3897.html有openSession方法调用的 

openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit)方法的原型。

 看到openSession有autoCommit这个参数,那这样调用(openSession(true))就OK了。测试成功!

0 1
原创粉丝点击