我尽量叙事的清楚点吧,可能稍微啰嗦点。报错内容让我很头疼,我都开始怀疑mybatits 3.0.2 和mysql 5.1是不是本身就有bug了。
请大神们稍事略过下面的代码,然后我再叙述
项目使用到了spring 3 + mybatits 3.0.2 + mysql 5.1
在存储历史记录是报错。偶尔报错,感觉应该是万分之几的概率吧。
Caused by: org.springframework.dao.DuplicateKeyException: SqlSession operation; SQL []; Duplicate entry '5879889-35399' for key 'user_id'; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '5879889-35399' for key 'user_id' at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:241) at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72) at org.mybatis.spring.SqlSessionTemplate.wrapException(SqlSessionTemplate.java:364) at org.mybatis.spring.SqlSessionTemplate$7$1.doInSqlSession(SqlSessionTemplate.java:344) at org.mybatis.spring.SqlSessionTemplate.execute(SqlSessionTemplate.java:173) at org.mybatis.spring.SqlSessionTemplate.execute(SqlSessionTemplate.java:155) at org.mybatis.spring.SqlSessionTemplate$7.invoke(SqlSessionTemplate.java:339) at $Proxy59.addFavorite(Unknown Source) at -------------------------------------.addFavorite(---------.java:104) at -------------------------------------$$FastClassByCGLIB$$bf29fae7.invoke(<generated>) at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149) at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:692) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:625) at ------------------------------------$$EnhancerByCGLIB$$1aaa1136.addFavorite(<generated>) at-------------------------------------.addFavorite(--------.java:277) ... 32 more Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '58179889-353994' for key 'user_id' at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) at com.mysql.jdbc.Util.getInstance(Util.java:386) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1039) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2002) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2163) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2624) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2127) at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1362) at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.execute(NewProxyPreparedStatement.java:989) at org.apache.ibatis.executor.statement.PreparedStatementHandler.update(PreparedStatementHandler.java:22) at org.apache.ibatis.executor.statement.RoutingStatementHandler.update(RoutingStatementHandler.java:51) at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:29) at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:73) at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:43) at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:102) at org.apache.ibatis.session.defaults.DefaultSqlSession.insert(DefaultSqlSession.java:91) at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:54) at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:35) at $Proxy59.addFavorite(Unknown Source) at sun.reflect.GeneratedMethodAccessor926.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.mybatis.spring.SqlSessionTemplate$7$1.doInSqlSession(SqlSessionTemplate.java:342) ... 48 more
上面一堆横杆是我替换了我项目的路径或者类名,
异常中得Duplicate entry '5879889-35399' for key 'user_id'我解释一下
5859889是用户id,35399是书籍id,我再写sql的时候,明明是写对对应的字段的。但是在写入数据库的时候就报错了。
sql如下
<insert id="addFavorite" useGeneratedKeys="true" keyProperty="id" > INSERT INTO favorites (<include refid="favorite.columns" /> <![CDATA[ ) VALUES ( #{id} , #{userId} , #{bookId} , NOW() ) ]]> </insert>
业务代码如下
favorite = new Favorite(); favorite.setBookId(bookInfo.getId()); favorite.setUserId(user.getId()); FavoriteFacade.addFavorite(favorite);// 加入收藏
对于favorite的bookId和userId我都设置的是Integer类型。
不知道为什么,两个字段的内容,再写入数据库的时候,就写在了一块。中间的 - ,是咋回事啊,哪里冒出来的啊????数据库中user_id是int类型。
项目跑了许久了。但是时不时就报着个错误。我感觉我的代码,写的没问题。所以就开始怀疑mybatits 3.0.2 和mysql 5.1是不是本身就有bug了。
大神们,给点建议吧~~~
Duplicate entry '5879889-35399' for key 'user_id'
应该就是主键重复的问题吧
你的userId 和 bookId 是不是联合主键
看了lz的问题,我也有些疑惑,经常遇到一些奇怪的问题,开始怀疑这怀疑那,后来找到问题的时候通常都是一些低级错误引起的。
你可以试试这样,
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '58179889-353994' for key 'user_id'
这个异常应该是mysql的驱动包里的,你用Maven自动下载了源码,找到抛异常的这段代码看是否是捕获的,如果是捕获的话就去catch里打断点,如果是抛出的那外层肯定也有捕获的代码,应该能查到到当前的执行的sql
另外你的mybatis是否有自定义拦截器?如果有仔细检查拦截器的代码,特别是if语句块的代码
你favorites 表的id字段是不是设置为auto_increment,而insert语句中又直接赋了#{id}
检查下是不是可能这个原因造成的
你可以在数据源上封装一层SQL日志框架,先看看MYBATIS生成的SQL对不对,你可以用这个SQL监控框架,p6spy.
很明显呀 索引 user_id(unique) 重复
你的表中有已经存在 user_id(5879889) bookid(35399)的数据了
应该在插入数据前检查数据是否已经存在