-->
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.  [ 3 posts ] 
Author Message
 Post subject: Single data read, multithreaded access and multiple writes
PostPosted: Sun Aug 31, 2014 8:41 am 
Newbie

Joined: Sun Aug 31, 2014 8:23 am
Posts: 2
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?


Top
 Profile  
 
 Post subject: Re: Single data read, multithreaded access and multiple writes
PostPosted: Mon Sep 01, 2014 8:39 am 
Senior
Senior

Joined: Tue Oct 28, 2008 10:39 am
Posts: 196
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?


Top
 Profile  
 
 Post subject: Re: Single data read, multithreaded access and multiple writes
PostPosted: Mon Sep 01, 2014 8:51 am 
Newbie

Joined: Sun Aug 31, 2014 8:23 am
Posts: 2
We designed 3 threads for our application. First (main) thread reads all relevant data from DB to persistent classes. and then it generates 2 more threads. One of these threads (simulation thread) manipulates the data periodically. (Only the classes) And after each n update, simulation thread tells DB thread to to write updated data back to the database. While these two threads are running concurrently and periodically, The main thread handles network messages and possibly manipulates the Data. The main problem is that, Writing all the data back to database takes longer than a simulation update. So I can not lock access to data while writing it back to DB. That will block the simulation thread.

[edit]
Simulation thread does not reread from database, and all DB thread has to do is to store everything back to database. Writing everything back takes some time that's why we wanted to run it on a separate thread.
[/edit]


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