-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: ConstraintViolationException when using generator increment
PostPosted: Thu Oct 26, 2006 2:24 pm 
Newbie

Joined: Mon Jul 25, 2005 6:42 pm
Posts: 5
I just updated to Hibernate 3.2 from 3.1, and to Spring 2.0. Now I occasionally am getting a ConstraintViolationException when several records are written to my transaction_history table in a single second.

After the exception is thrown every insert to the table fails with the same ConstraintViolationException. I have to restart Tomcat to resume operations.

Does Hibernate need me to do some sort of synchronization when using the generator increment?

Do I need to put in a 'synchronized' modifier in my function that inserts to the table? I never had this problem with Hibernate 3.1.

Thanks in advance.


Hibernate version:
3.2

Mapping documents:
<class
name="com.myCompany.wms.persist.TransactionHistory"
table="transaction_history"
>

<id
name="transactionHistoryId"
type="java.lang.Integer"
column="transactionHistoryId"
>
<generator class="increment" />
</id>
...

Full stack trace of any exception that occurs:
2006-10-26 11:13:40.218 WARN - SQL Error: 2627, SQLState: 23000
2210156 [http-8080-Processor24] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 2627, SQLState: 23000
2006-10-26 11:13:40.218 ERROR - Violation of PRIMARY KEY constraint 'PK__transaction_hist__4813E5CB'. Cannot insert duplicate key in object 'dbo.transaction_history'.
2210156 [http-8080-Processor24] ERROR org.hibernate.util.JDBCExceptionReporter - Violation of PRIMARY KEY constraint 'PK__transaction_hist__4813E5CB'. Cannot insert duplicate key in object 'dbo.transaction_history'.
2006-10-26 11:13:40.218 ERROR - Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: could not insert: [com.myComany.wms.persist.TransactionHistory]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2202)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2595)
at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:51)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.springframework.orm.hibernate3.HibernateTemplate$27.doInHibernate(HibernateTemplate.java:801)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:362)
at org.springframework.orm.hibernate3.HibernateTemplate.flush(HibernateTemplate.java:799)
at com.myComany.framework.persist.BaseDaoImpl.saveWithoutEditFields(BaseDaoImpl.java:216)
at sun.reflect.GeneratedMethodAccessor1202.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:203)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:162)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:209)
at $Proxy5.saveWithoutEditFields(Unknown Source)
at com.myComany.wms.logic.TransactionHistoryLogic.saveTransactionHistory(TransactionHistoryLogic.java:163)
at com.myComany.wms.logic.TransactionHistoryLogic.workCreate(TransactionHistoryLogic.java:1433)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:203)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:162)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:209)
at $Proxy6.workCreate(Unknown Source)
at com.myComany.wms.logic.PoReceiveLogic.createWorkRecord(PoReceiveLogic.java:1129)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:203)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:162)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:209)
at $Proxy70.createWorkRecord(Unknown Source)
at com.myComany.wms.logic.KitMaintenanceLogic.completeKitRequest(KitMaintenanceLogic.java:810)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:203)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:162)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:209)
at $Proxy71.completeKitRequest(Unknown Source)
at com.myComany.wms.ui.KitMaintenanceAction.buildKitRequest(KitMaintenanceAction.java:796)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:270)
at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:187)
at com.myComany.framework.ui.BaseAction.execute(BaseAction.java:238)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Unknown Source)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Violation of PRIMARY KEY constraint 'PK__transaction_hist__4813E5CB'. Cannot insert duplicate key in object 'dbo.transaction_history'.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(Unknown Source)
at com.microsoft.sqlserver.jdbc.IOBuffer.processPackets(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.sendExecute(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.doExecuteUpdate(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(Unknown Source)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:101)
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:23)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2182)
... 87 more

Name and version of the database you are using:
SqlServer 2005 express

Hardware /OS:
I am using a new Core 2 Duo Intel based computer, Windows XP Pro


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 27, 2006 12:02 am 
Regular
Regular

Joined: Tue Sep 26, 2006 11:37 am
Posts: 115
Location: Sacramento, CA
The increment generator is not thread safe so this was not supposed to work with either version. Maybe you are getting more load now. One easy approach is to switch to a different generator, native is simple to use if provided by your database. In Mysql you just have to alter the table to make the field autoincrement.

Marius


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 27, 2006 12:43 am 
Newbie

Joined: Mon Jul 25, 2005 6:42 pm
Posts: 5
Thanks for the reply. Using database specific features is not an option for me because I need to remain database independent. I think there is a way to have Hibernate always query the database for the current maximum before the insert, but this affects performance.

Is it documented that the increment generator is not thread safe? If it is not thread safe then it really isn't very useful. I do remember reading that the increment could not be used if there is any chance that some application other than Hibernate will do inserts. I don't remember reading that it wasn't useful if Hibernate itself in another thread did an insert!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 30, 2006 12:39 am 
Regular
Regular

Joined: Tue Sep 26, 2006 11:37 am
Posts: 115
Location: Sacramento, CA
How about using hilo then? This is not database specific.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 31, 2006 7:01 pm 
Newbie

Joined: Mon Jul 25, 2005 6:42 pm
Posts: 5
Once again it looks like we shot ourselves in the foot. One of my team members modified our configuration so that Spring was starting two copies of our application context and thus Hibernate. The problem was not in Hibernate at all.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 01, 2006 11:13 pm 
Regular
Regular

Joined: Tue Sep 26, 2006 11:37 am
Posts: 115
Location: Sacramento, CA
Ah, interesting. I never made THAT mistake, good to learn from other people's adventure for a change :)

Thanks for the extra info and credit.

Marius


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.