I use hibernate 2.1 with spring framework, and the database is mysql. Very occasionaly, I got duplicate entry error of duplicate entry. The persistence related code is as follows:
----------------------------------
public class HibernateDAO{
...........
public void save(Object o){
//o is a object with id of int type.
//The id is generated by Hibernate 'increment' ID generator
try{
getHibernateTemplate().save(o);
}catch(Exception ex){
Log.error(ex.getMessage());
}
}
...........
}
------------------------------------
It's initialized in spring in singleton mode. I think the 'increment' ID generator works fine definitely. I suspect HibernateDAO.save(Object) caused the problem in multithread environment, but I'm not sure. I do search many posts in Google reporting similiar issue, but no solution is found as far. :-(
------------------------------------
Here is the logging message:
------------------------------------
2006-08-21 17:20:49,925 WARN [org.hibernate.util.JDBCExceptionReporter] SQL Error: 1062, SQLState: 23000
2006-08-21 17:20:49,957 ERROR [org.hibernate.util.JDBCExceptionReporter] Duplicate entry '3699' for key 12006-08-21 17:20:49,958 ERROR [org.hibernate.event.def.AbstractFlushingEventListener] Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:249)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:993)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:287)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:181)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:148)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:170)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:176)
|