serkan.ergun wrote:
Hi there,
I have a bit of a different scenario here and I would like to know whether Hibernate would be suitable for this task.
We have a database of say a few thousands of objects (with one-to-many and many-to-one relations) . We would like to retrieve all of this information and run a simulation on this data every second. Simulation can modify the data, add new entities or remove existing ones. After each n seconds (say 60 for example) we would like to store this data back to database on another thread. The data that needs to be updated can be large and it can be modified from the other thread.
Since there will be a single read and multiple writes, I tried implementing long running conversations. (this would also help keeping track of changes and only update changed data) I created a session and load all of the data. I then create another thread which will write data back to database periodically. However, same data can be change from the simulation thread. So I need to either lock accesses to data or clone it. locking would prevent simulation thread to run smoothly so I tried cloning the data. But then I get a "A different object with the same identifier value was already associated with the session" error.
My question is: Can this be done in Hibernate? If so how can we do it? If not what do you suggest as an alternative?
It depends on what has to be done when your "write back"-Thread has changed data and your "simulation"-thread did something else... Is a simulation result valid if another thread modified your data? How many conflicts are there? Who writes more? "write back" or "simluation"? If there are only some conflicts, I'd use a @Version-Field.
Why do you split simulation and the other thread? What changes does the other thread do to entities that your simulation did create/modify? Does the simulation re-read and in what situation?