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?
-
3If your application is the only thing accessing the database, then yes, probably. But databases are pretty good at handling it, so I wouldn’t attempt this unless you have strong evidence that the database’s concurrency handling is a problem.VGR– VGR2018-12-24 16:50:29 +00:00Commented Dec 24, 2018 at 16:50
-
Coding directly in your database should be enough. Take a look on locking reads, disabling autocommits etc. If you consider it is not enough, you can implement more concurrency control from your client side.Keka Bron– Keka Bron2018-12-24 17:02:18 +00:00Commented Dec 24, 2018 at 17:02
-
2No, these are 2 different things. Learn about transactions, isolation levels, and select for update with lock. See stackoverflow.com/q/43420216/217324. You should include if you’re talking about rdbms or something else.Nathan Hughes– Nathan Hughes2018-12-24 17:05:56 +00:00Commented Dec 24, 2018 at 17:05
-
@NathanHughes I suggest you make your comment an Answer, so it can be accepted to close this Question.Basil Bourque– Basil Bourque2018-12-24 17:55:53 +00:00Commented Dec 24, 2018 at 17:55
2 Answers
The short answer is no you would not. You can try, but in practice it will not perform better or be more maintainable than utilizing what a good RBMS already provides.
The features of a good transactional RBMS are advantageous to you. Try not to inherently perceive those features as problems.
So assuming your question is regarding multiple threads of an application, or even multiple instances of an application, accessing the same common resources at the same time, likely in a database, chances are the database has the best feature set to guarantee atomicity and integrity where you need it.