-->
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.  [ 9 posts ] 
Author Message
 Post subject: SQL Server JDBC problems
PostPosted: Thu Dec 07, 2006 6:53 am 
Newbie

Joined: Tue Nov 28, 2006 9:34 am
Posts: 15
Location: Brasil
I'm using the Microsoft JDBC for SQLServer 2000, and even with the jTDS I have the same problem...
I can't update or load+delete a row, the error message is the follow:
Code:
ERROR AbstractFlushingEventListener(performExecutions:300) - Could not synchronize database state with session
org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [br.com.shc.database.cor.Cor#2]
   at org.hibernate.persister.entity.AbstractEntityPersister.check(AbstractEntityPersister.java:1699)
   at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2342)
   at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2242)
   at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2542)
   at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:92)
   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:140)
   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:1000)
   at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
   at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
   at br.com.shc.dao.teste.TesteDAO.main(TesteDAO.java:43)


I realized that even when I create a SQLQuery, the number of affected rows is always 0... maybe there is something connected...

My code is the same for Postgree, MySQL and others... only in SQLServer doesn't seems to work... :(

Any advice?
Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 07, 2006 8:43 am 
Regular
Regular

Joined: Wed Mar 23, 2005 8:43 am
Posts: 105
Location: Moscow, Russia
Do you use versioning? Do you (or your application) have exclusive access to database? Do you use unsaved-value attribute in id or version mapping?

_________________
Best Regards


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 11, 2006 4:21 pm 
Newbie

Joined: Tue Nov 28, 2006 9:34 am
Posts: 15
Location: Brasil
Yes, if i disable the versioning this is the error msg:
Code:
Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1

In this moment I'm running a Test Application, and only this access the database.
No, should I use unsaved-value?

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 11, 2006 4:44 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
that error usually means what is stored in memory doesn't match up with what's in the database/a session doesn't recognize an object you passed to it. SQL Server must be changing something you aren't aware of or something happens to the data as you read it in.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 12, 2006 6:52 am 
Newbie

Joined: Tue Nov 28, 2006 9:34 am
Posts: 15
Location: Brasil
The code is simple like this:
Code:
         t = session.beginTransaction();
         session.saveOrUpdate(blue);
         t.commit();

Works perfectly in anothers databases... When I try to insert or select any data, it works too...
But when I try to delete or update these things happen...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 12, 2006 7:05 am 
Regular
Regular

Joined: Wed Mar 23, 2005 8:43 am
Posts: 105
Location: Moscow, Russia
Hmm, it seems strange to me. Did you try to execute plain SQL via JDBC and count updated rows?

_________________
Best Regards


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 15, 2006 6:35 am 
Newbie

Joined: Tue Nov 28, 2006 9:34 am
Posts: 15
Location: Brasil
If i try to execute a SQL Query, it works and updates/deletes all the rows correctly... But the number of "rows affecteds" still 0.

Like this:
Code:
   t = session.beginTransaction();
   Query q = session.createSQLQuery("update cor set description = description + '1'");
   System.out.println("\nRows Affecteds: " + q.executeUpdate() + "\n\n");
   t.commit();


It prints:
Code:
    update
        cor
    set
        description = description + '3'

Rows Affecteds: 0

And actually it updated all the rows (more than 10).
I don't know if this problem of the "Rows Affecteds" has anything with the case that i can't update or delete like i said up there.

Thanks again.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 15, 2006 6:52 am 
Regular
Regular

Joined: Wed Mar 23, 2005 8:43 am
Posts: 105
Location: Moscow, Russia
I really don't know why Microsoft SQL Server JDBC Driver returns incorrect rows count after update. Fact is that versioning mechanism in Hibernate can't work without correctly returned updated rows count.

Look at quote from docs for JDTS JDBC Driver:

Quote:
PreparedStatement.executeUpdate() returns an incorrect update count.

This is caused by triggers; replication is also implemented through triggers, so you will encounter this issue on replicated databases too. The root cause is that triggers also return update counts and jTDS can't make the difference between these update counts and the "real" one (neither could any other SQL Server client). However, the update count you need is the last of them (because the actual UPDATE/INSERT/DELETE gets executed only after the triggers) and there is luckily a parameter you can specify in the URL (or the connection Properties or in the DataSource) in order for jTDS to return only this last update count (and ignore the rest of them). This parameter is lastUpdateCount and you can find out more about it here (http://jtds.sourceforge.net/faq.html#urlFormat).

Please note that setting lastUpdateCount to true could cause problems if you use queries that do actually return more than one update count (such as queries consisting of multiple updates/inserts), because it will also return only the last of these update counts. Although queries returning multiple results are not very common, it is the reason why this behavior is customizable from the URL. Please note that this flag only alters the behavior of executeUpdate(); execute() will still return all update counts.

If you have both queries consisting of multiple UPDATEs/INSERTs/DELETEs and triggers you won't be able to set lastUpdateCount to true if you want all the update counts, so you will have to skip over the irrelevant update counts by calling getMoreResults() yourself, when needed.
[/url]

_________________
Best Regards


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 15, 2006 7:05 am 
Newbie

Joined: Tue Nov 28, 2006 9:34 am
Posts: 15
Location: Brasil
Thank you, but lastUpdateCount is already true by default, has the page says... And if when i change it for true or false the problem is the same...
And I'm not using triggers or replication as far as I know...


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