-->
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.  [ 15 posts ] 
Author Message
 Post subject: StaleStateException upon deletion of persistent object
PostPosted: Wed Mar 28, 2007 8:15 pm 
Newbie

Joined: Wed Mar 28, 2007 7:53 pm
Posts: 10
Greetings!

I have been fighting with this bug for a while now and I am stomped because my knowledge of Hibernate is fairly limited. Here is the gist:

I am trying to delete a user from the database (SQL server). Here is an idea of the code in my UserDAO:

//successfully retrieve the user
//get the session from my HibernateUtil class:
Session sess = HibernateUtil.getSession();
sess.beginTransaction();
sess.delete(persistedUser);
sess.getTransaction().commit(); <<------ It fails here
HibernateUtil.closeSession();

Another thing is that there are database triggers already in place to ensure that when a user is deleted/updated, other records associated with him in other tables are updated or deleted (as the case may be). So I wonder if there is some concurrent modification issue happening there. I also wonder if my drivers could be the problem. Another thing is that the same piece of code works great with an Oracle database (also with db triggers in place) - no exceptions occur, everything works great upon update/deletion of users.

Here is a small portion of the error I get:
org.hibernate.StaleStateException: Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1
at org.hibernate.jdbc.BatchingBatcher.checkRowCount(BatchingBatcher.java:92)
at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:78)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:57)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:174)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:226)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:137)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730)



Any wild/educated guesses on what may be happening here? I appreciate any input.

Cheers mates,
Lucise


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 28, 2007 11:35 pm 
Expert
Expert

Joined: Tue Jan 30, 2007 12:45 am
Posts: 283
Location: India
Hi Lucise,

try sess.flush() in place of sess.getTransaction().commit();

it might work.

_________________
Dharmendra Pandey


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 29, 2007 12:00 am 
Newbie

Joined: Wed Mar 28, 2007 7:53 pm
Posts: 10
Hey dharmendra,

I tried that, already => Same error.
I can't think why the code works with Oracle and fails with SQL db.

Any more ideas anyone? :)

~Lucise


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 29, 2007 12:10 am 
Expert
Expert

Joined: Tue Jan 30, 2007 12:45 am
Posts: 283
Location: India
Hi Lucise,

No idea about .Since in my hibernate exp, I have use Oracle 10 and 9i.Might be problem with connection pool not sure. try with c3p connection pool, It is more efficient that any other connection pool and also recommended with hibernate

_________________
Dharmendra Pandey


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 29, 2007 11:07 am 
Newbie

Joined: Wed Mar 28, 2007 7:53 pm
Posts: 10
Thanks, will look into that too. :)


L.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 29, 2007 12:07 pm 
Newbie

Joined: Wed Mar 28, 2007 7:53 pm
Posts: 10
I thought I'd post the full error I am getting:



My printout of the session after sess.delete(persistedUser);
10:05:21,750 INFO [STDOUT] ZZZZZ-----------------------Session IS CONNECTED--------------------ZZZZZ
10:05:21,750 INFO [STDOUT] ZZZZZ-----------------------Session IS OPEN--------------------ZZZZZ
10:05:21,750 INFO [STDOUT] ZZZZZ-----------------------Session IS DIRTY--------------------ZZZZZ


The Error:

10:05:21,765 ERROR [AbstractFlushingEventListener] Could not synchronize database state with session
org.hibernate.StaleStateException: Unexpected row count: 0 expected: 1

at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:27)
at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2363)
at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2516)
at org.hibernate.action.EntityDeleteAction.execute(EntityDeleteAction.java:73)
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:144)
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 com.tl.evtv.models.EvUserDAO.deleteUserByName(EvUserDAO.java:196)
at com.tl.evtv.controllers.AdminManager.deleteUser(AdminManager.java:95)
at org.apache.jsp.UserManagerServlet_jsp._jspService(UserManagerServlet_jsp.java:233)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
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.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:175)
at org.jboss.web.tomcat.tc5.session.ClusteredSessionValve.invoke(ClusteredSessionValve.java:95)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:74)
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.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
at java.lang.Thread.run(Thread.java:595)
10:05:21,968 ERROR [[jsp]] Servlet.service() for servlet jsp threw exception
org.hibernate.StaleStateException: Unexpected row count: 0 expected: 1
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:27)
at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2363)
at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:2516)
at org.hibernate.action.EntityDeleteAction.execute(EntityDeleteAction.java:73)
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:144)
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 com.tl.evtv.models.EvUserDAO.deleteUserByName(EvUserDAO.java:196)
at com.tl.evtv.controllers.AdminManager.deleteUser(AdminManager.java:95)
at org.apache.jsp.UserManagerServlet_jsp._jspService(UserManagerServlet_jsp.java:233)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
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.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:175)
at org.jboss.web.tomcat.tc5.session.ClusteredSessionValve.invoke(ClusteredSessionValve.java:95)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:74)
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.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
at java.lang.Thread.run(Thread.java:595)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 29, 2007 2:28 pm 
Regular
Regular

Joined: Sun Sep 17, 2006 2:48 am
Posts: 81
Location: California
1. Where and how are you retrieving the user object?
2. When you are trying to delete the user object, what is the hibernate state of this object? It must be in the persistent state.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 29, 2007 2:48 pm 
Newbie

Joined: Wed Mar 28, 2007 7:53 pm
Posts: 10
Hello Somu!


1. Where and how are you retrieving the user object?

This is the code :

public void deleteUserByName(String username) {
User user;
Query query = HibernateUtil.getSession().createQuery(
"from " + TABLE_NAME + " r where r.username = :uName");
query.setString("uName", username);
user = (EventViewerUser) query.setMaxResults(1).uniqueResult();

Session sess = HibernateUtil.getSession();
sess.beginTransaction();
sess.delete(user);
sess.getTransaction().commit();
HibernateUtil.closeSession();
}

2. When you are trying to delete the user object, what is the hibernate state of this object? It must be in the persistent state.


Yes, it is.


Thanks,
Lucise


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 29, 2007 2:55 pm 
Regular
Regular

Joined: Sun Sep 17, 2006 2:48 am
Posts: 81
Location: California
Can you pull these 2 lines right at the start of the method?

Session sess = HibernateUtil.getSession();
sess.beginTransaction();

so that the user retrival is inside the same transaction.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 29, 2007 3:06 pm 
Newbie

Joined: Wed Mar 28, 2007 7:53 pm
Posts: 10
Hey Somu,

Off to try that right now! =)

L.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 29, 2007 3:19 pm 
Newbie

Joined: Wed Mar 28, 2007 7:53 pm
Posts: 10
My method now looks like this:

public void deleteUserByName(String username) {
Session sess = HibernateUtil.getSession();
sess.beginTransaction();

User user;
Query query = sess.createQuery(
"from " + TABLE_NAME + " r where r.username = :uName");
query.setString("uName", username);
user = (EventViewerUser) query.setMaxResults(1).uniqueResult();

sess.delete(user);
sess.getTransaction().commit();
HibernateUtil.closeSession();
}


But I am still getting the same error! It fails on the sess.getTransaction().commit(); line .. Just like before.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 29, 2007 4:17 pm 
Regular
Regular

Joined: Sun Sep 17, 2006 2:48 am
Posts: 81
Location: California
Can you post the hbm file for the User object?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 29, 2007 4:25 pm 
Newbie

Joined: Wed Mar 28, 2007 7:53 pm
Posts: 10
Sure thing:

HBM file for SQL:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

<class name="com.tl.evtv.models.User" table="USER">
<id name="userId" >
<column name="USER_ID" sql-type="integer(4)" />
<generator class="assigned"/>
</id>
<property name="username" column="USERNAME"/>
<property name="password" column="PASSWORD"/>
<property name="description" column="DESCRIPTION"/>
<property name="roleId">
<column name="ROLE_ID" sql-type="integer(4)" />
</property>
</class>

</hibernate-mapping>


HBM file for ORACLE: (works fine)

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

<class name="com.tl.evtv.models.User" table="USER">
<id name="userId" column="USER_ID">
<generator class="assigned"/>
</id>
<property name="username" column="USERNAME"/>
<property name="password" column="PASSWORD"/>
<property name="description" column="DESCRIPTION"/>
<property name="roleId" column="ROLE_ID"/>
</class>

</hibernate-mapping>


Thanks.
L.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 29, 2007 4:52 pm 
Regular
Regular

Joined: Sun Sep 17, 2006 2:48 am
Posts: 81
Location: California
hbms look fine.
Not sure what is happenning but check for the following.

1. Print out the id of the user object before you are calling session.delete()
2. Check that this object (with this id ) is present in the DB.
3. What are other triggers/functions thats happenning? Any of those trying to delete the same object?
4. Are there more operations thats happenning in the same transaction/session than just delete of the user object?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 29, 2007 5:34 pm 
Newbie

Joined: Wed Mar 28, 2007 7:53 pm
Posts: 10
Thanks Somu.

I'll play around with this some more.
Please do drop a line here if you think of something later. ;)

I might just abandon the db trigger approach and go with constraints for cascade delete/updates..

Thanks again!
Lucise


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 15 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.