aspose file tools*
The moose likes Threads and Synchronization and the fly likes Concurrent access of a database row by a java program? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Concurrent access of a database row by a java program?" Watch "Concurrent access of a database row by a java program?" New topic
Author

Concurrent access of a database row by a java program?

Samuel Benjamin
Greenhorn

Joined: Mar 16, 2013
Posts: 4
I have a table with some rows in my database. In my Java program, there is a "Consumer class" which uses a row and a "Producer Class" which puts data into this row.
How do I ensure that the producer cannot alter the row unless it gets permission from the consumer ? I need some suggestions and ideas on how this can be done.
My database is SQL Server 2008 r2.

Thanks in advance.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
Welcome to the Ranch

Are you saying you have a producer‑consumer race condition in your application? You can synchronize access to the database internally; I suspect a transaction might work. But I am not sure that is what your question means.
Shall move this discussion to our concurrency forum.
Samuel Benjamin
Greenhorn

Joined: Mar 16, 2013
Posts: 4
Campbell Ritchie wrote:Welcome to the Ranch

Are you saying you have a producer‑consumer race condition in your application? You can synchronize access to the database internally; I suspect a transaction might work. But I am not sure that is what your question means.
Shall move this discussion to our concurrency forum.


I have a table with a rows that need to be updated sometimes. No new rows are added to this database. One part of my code (B) uses the data contained in a row, while the other part (C) CAN update the data in the row. C C should not be allowed to update a row as long as B is using it. How do I do this in Java ? I think synchronization in my java code will be needed, but not sure how to do it.

James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1030
    
    5

Samuel

This can all be done for you using database transactions. See here for more information:

http://docs.oracle.com/javase/tutorial/jdbc/basics/transactions.html

I would read over all of the JDBC tutorial so that you gain a complete understanding of how Java interacts with databases.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Concurrent access of a database row by a java program?