• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Threads and database approach question

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wondering if this approach is safe and workable with Threads and hibernate?

One thread is responsible for writing data to the database every few seconds to a minute. I have other threads that start about every five minutes to read the database for the last record entered and do with the data what they will -- they do not update the record in the database at all.

I had thought about sharing the object with all the threads but the above would separate out writes and reads. The query should be easy to just grab the last record as most databases allow for that -- not sure about hibernate (still getting into it.) The only issue I see might be if need to create a thread that would read the database every three seconds and in that case it might be better to share the object.

So am I headed down a good path or a bunny trail?

Thanks
Mike

 
Sheriff
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you want is a read-many-write-one lock: reading threads can have access unless another thread is writing; other reading threads are not a problem. Writing threads must wait until all other reading and writing threads have finished. don't know if this has been implemented in Java yet but it's not that hard to implement yourself if it isn't.
 
Mike Bates
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob,

Thanks, I'll look into this.

Mike
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:What you want is a read-many-write-one lock: reading threads can have access unless another thread is writing; other reading threads are not a problem. Writing threads must wait until all other reading and writing threads have finished. don't know if this has been implemented in Java yet but it's not that hard to implement yourself if it isn't.



Further to Rob comments( thanks Rob). There is in java 5 ( concurrency util) something called ReadWriteLock.
If we look at the JAVA DOC; it says:

"A ReadWriteLock maintains a pair of associated Lock locks, one for read-only operations and one for writing. The read lock may be held simultaneously by multiple reader threads, so long as there are no writers. The write lock is exclusive.

All ReadWriteLock implementations must guarantee that the memory synchronization effects of writeLock operations (as specified in the Lock interface) also hold with respect to the associated readLock. That is, a thread successfully acquiring the read lock will see all updates made upon previous release of the write lock. "

Now the last statement is very interesting which means there will be no stale data so any read thread will capture the latest write thread so this is a guranttee of visibility across all reader threads.

Now if we compare the performance of ReadWriteLock implementation ( ReentrantReadWriteLock) w.r.t. ReentrantLock; we can see on the average for more then 4 threads we will get almost double throughput ( measure of taks compleleted per unit of time)

Now if we give an example here to clarify the concept more; please notice, I am assuming you are using java 5 and above.



I hope this could help




 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic