views:

477

answers:

6

Problem solved: Thanks guys, see my answer below.

I have a website running in Tomcat 5.5 hooked up to a MySQL5 database using Hibernate3.

One record simply refuses to keep any changes performed on it. If I change the record programmatically, the values revert back to what they were previously.

If I manually modify the record in the database, the values will revert (seemingly once the webapp accesses them).

I have tried stopping Tomcat and changing the values manually then starting Tomcat again. Checking the database, the values remain changed after Tomcat has started the webapp but will revert back again once I load the site.

I have also tried deleting the Tomcat work folder for the webapp and the .ser cache file.

I have also checked the code for the values that are being reverted to and cannot find them.

I have only noticed it on this one particular record.

Edit: I've just had a look at the SQL output from Hibernate using hibernate.show_sql=true. There is an update query logged for the table my row is in. Does anyone know how to resolve the ? for the columns to actual values?

A: 

It's getting close to halloween, so you have to expect this sort of thing (plus it was just a full moon), but I'd keep looking for the culprit in the web application ... it HAS to be there. A couple values I'd immediately search for in the webapp source code:

  • The id of the record being changed.
  • The value that's being written into the record.

Good luck ... these can be real bears to find!

Steve Moyer
Unfortunately I've tried searching through the code for the values that it is being reverted to and, no dice.
Feet
Hmmm ... I don't know where to point you without looking at the code and/or table structure. Are you using Hibernate3 as a JPA provider or just Hibernate. With hbm files, javadoc/xdoclet or annotations?
Steve Moyer
Using it as a JPA with doclet. I am assuming it is somewhere in the Webapp rather than the database itself as a dump to a different machine gives the same results.
Feet
How are you generating the values for your PKs (ids)? Is it possible the generator is failing and producing the value in the PK of the record being changed?
Steve Moyer
Hibernate is handling the PK's (generator-class="increment"). How would I check if the generator is failing?
Feet
Had to look that one up ... and I believe it's correct for MySQL (and I'm assuming your provider definition is using the MySQLDialect).
Steve Moyer
Dialect is org.hibernate.dialect.MySQL5Dialect.
Feet
A: 

This smells a little like a test-case firing on start up that modifies the row to what it expects it to be before testing.

Bill James
Good one ... JUnit tests on the POJOs or a DBUnit set-up could both do that ... kudos!
Steve Moyer
Unfortunately there are very little tests written for this particular webapp. Of those that are there, none are written in such a way as to (explicitly) modify records.
Feet
A: 

Add a trigger BEFORE UPDATE, check row id, raise an SQL error if it matches your magic row. Then check the generated stacktrace, walk the code and locate the piece that updates the row.

Vladimir Dyuzhev
Added an interceptor to the SessionFactory (using Spring) It doesn't seem to be using it? <property name="entityInterceptor"> <ref bean="myInterceptor"/> </property><bean id="myInterceptor" class="com.blah.TestInterceptor"/> TestInterceptor has some trace in onSave().
Feet
Just got it working.
Feet
+1  A: 

You could temporarily enable the mysql query logging and see exactly what sql statement altered the value. Since you say it changes immediately after the server starts you should be able to figure out the statement pretty quickly.

http://dev.mysql.com/doc/refman/5.0/en/query-log.html

Zoredache
Turned it on, checked the logs and yes, there is a query being run to update the record. Finding out where it is coming from is the hard part.
Feet
+1  A: 

To answer your question:

Does anyone know how to resolve the ? for the columns to actual values?

You can do this with p6spy. Instructions for how to set this up in a Spring app are available here.

However, I think there's a mistake in these instructions, the file they refer to as p6spy.log should actually be name p6spy.properties.

Don
A: 

Thanks to everyone for the help. All of the suggestions came in handy for tracking it down.

I've managed to find out what was causing it. Bad database design, multiple data models and Hibernate makes for some nasty stuff. Another table had the value stored and that class was extending a base class with the same value.

Time to look at doing some normalisation.

Feet