File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Concurrent access of a database row by a java program?

 
Samuel Benjamin
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47232
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic