Using Java locks for database concurrency
NickName:Medu Ask DateTime:2012-10-09T06:52:00

Using Java locks for database concurrency

I have the following scenario. I have two tables. One stores multi values that are counters for transactions. Through a java application the first table value is read, incremented and written to the second table, as well as the new value being written back to the first table. Obviously there is potential for this to go wrong as it's a multiple user system. My solution, in Java, to the issue is to provide Locks that have to, well should, be aquired before any action can be taken on either table. These Locks, ReentrantLocks, are static and there is one for each column in Table 1 as the values are completely independent of each other.

Is this a recommended approached?

Cheers.

Copyright Notice:Content Author:「Medu」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/12790593/using-java-locks-for-database-concurrency

Answers
user166390 2012-10-08T22:55:24

No. Use implicit Database Locks1 for Database Concurrency. Relational databases support Transactions which are a vital part of ACID: use them.\n\nJava-centric locks will not work cross-VM and as such will not help in multi-User/Server environments.\n\n\n\n1 Databases are smart enough to acquire/release locks to ensure Consistency and Isolation and may even use \"lock free\" implementations such as MVCC. There are rare occasions when explicit database locks must be requested, but this is an advanced use-case.",


More about “Using Java locks for database concurrency” related questions

Using Java locks for database concurrency

I have the following scenario. I have two tables. One stores multi values that are counters for transactions. Through a java application the first table value is read, incremented and written to the

Show Detail

Locks and concurrency in java

I am new to java concurrency and working with locks. I am trying to solve the dining problem and I don't have deadlocks yet only one thread gets actual runtime. Can anyone tell me what am I doing ...

Show Detail

Oracle Database - Grant/Revoke High Concurrency

We have an Oracle 10g release 2 database running on a production environment. It's experiencing a lot of concurrency, as Sql Developer 17.4 "Waits for past 1 hour" graph shows. When the database is

Show Detail

Java Concurrency: Paired locks with shared access

I am looking for a Java implementation of the following concurrency semantics. I want something similar to ReadWriteLock except symmetrical, i.e. both the read and write sides can be shared amongst...

Show Detail

Java Concurrency using locks

I'm processing a graph and it works sequentially and now I want to improve its performance by processing it concurrently. Here's what I'm doing: I traverse all of the edges in the graph on each

Show Detail

Condition vs Semaphore in java concurrency

I'm having trouble getting my head around a homework problem. The question asks us to mimic the implementation of Condition by only using Semaphore as a concurrency control class and nothing else. ...

Show Detail

Can I solve database concurrency problems in java code?

I wonder can I resolve relational database concurrency problems (like locking) by java concurrency? If I write correct concurrent code in java then can I avoid concurrency issues in database?

Show Detail

non-blocking and blocking concurrency

As far as I understand - for example in java - when we are using locks, synchronized, concurrentMaps, atomic et al. we are using blocking concurrency as threads cannot proceed until the locks are

Show Detail

LinkedBlockingQueue and write locks in java

i am a neophyte in java concurrency and wanted to know when to use a LinkedBlockingQueue. Say there is an upload servlet. There can be concurrent uploads. When the file is being written, we are

Show Detail

Why mongodb locks database instead of collecton

I read some information in mongodb manual regarding locking of database. It says that mongodb implements some sort of reader-writer lock for multiple clients working with database. It seems absolut...

Show Detail