• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Getting a exclusive lock on a cell.

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all..I am working on a small program with threads. In the middle of the program i want to obtain an exclusive lock on cell in table , so that no other thread should even be able to read the contents of that cell untill the lock is released.

Not just other threads i dont want to allow read/write operations on that cell from any other applications as well, if the database is shared across applications.

What i need exactly is some type of EXCLUSIVE LOCK on the database level.

I have no idea how to do this. Can someone help?

Thanks in Advance.
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Various databases support different set of tools to accomplish this. I don't know whether there are databases which would support locking individual columns, but I know for sure there are ones that don't, so this might not be feasible for you depending on the database. Generally the most compatible solution would be to lock an entire row, ideally using a SELECT ... FOR UPDATE commad. Look up the FOR UPDATE clause in the SQL reference of your database (and come back here if you encounter problems with it).

In your program you'll begin a transaction and issue the SELECT ... FOR UPDATE when entering the exclusive block, and you'll rollback or commit when you're exiting. This also means that you need to keep the connection open for the entire execution of the exclusive block. If this is not possible in your code, a more complicated design will have to be implemented in all of the applications that need to be synchronized this way.

A further consideration is that some databases, such as Oracle, don't allow locks that would block read operations. If blocking reads is essential to you, it must be addressed differently.
 
reply
    Bookmark Topic Watch Topic
  • New Topic