开发者

Hibernate query accessing previous version of data object?

I have a piece of code using hibernate which behaves unexpectedly; after trying many different debugging strategies, I am wondering if my understanding of Hibernate sessions / mapped object updates is incorrect. Here is the simplified scenario:

I have a mapped object (MO) which is defined with an id (long) and a date (date/time). There is a main routine which runs every 60 seconds which checks for any MOs that need to be processed, based on a date/time. The main routine runs in a single thread. Here is the pseudocode:

main () 
// open session
// get current date/time
// query all mapped objects where date/time field < current date/time
// for each mapped object which matches {
//   updateObject (session, current date/time, mapped object)
// }
// flush session
// close session

updateObject( Session s, Date currentTime, MappedObject mo ) 
// changes to mo object 
// add 1 day to currentTime and update date/time field in mo
// transaction begin
// update mo object
// transaction committed

Not shown: session factory initialization, exception handling routines

The session factory is configured with max connection pool size of 20 (c3p0). Default settings are being used for any caching.

The problem

The query is returning MOs whose date values are in the future (i.e date/time field > current date/time). According to the object watch, the value of the date/time field in the MO is the old, previous value (not the recalculated, updated value), which forces the query to pick it up and execute the updateObject routine.

I assume that the update was successful by verifying the update by monitoring the DB directly; also, none of the transaction exceptions are triggered according to the logs.

This problem occurs intermittently - sometime, it happens several times a day, other times it does not happen at all.

Platform details

Hibernate version: 3.3.2.GA

c3p0 version: 0.9.1

JRE version: 1.6.0_16

My thoughts

My first thought was a coding error, but it does not appear to be the base. I am wondering if somehow an ol开发者_如何学JAVAd version of the object is cached in one of the Session objects in the connection pool, and for some reason, it gets picked up from time-to-time.

I am worried about the larger implications if there is a problem with the updates.

I am using an older version of Hibernate at this time - migrating to a new version could be challenging, but I want to see if I have misunderstood something before going that route.

Any suggestions or directions to results would be most appreciated - thanks in advance!

Update

Moving to Apache DBCP seems to have resolved the problem. Will update if the problem is observed again.


There are two things you can try...

1) Set the following Hibernate property to turn statement caching off:-

<property name="hibernate.c3p0.max_statements">0</property>

If that doesn't work...

2) Instead of using C3P0, try out Apache DBCP to see if you have the same issue. It should an easy swap out.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜