Research on the deadlock problem in the Java EE

Source: Internet
Author: User

Most important applications involve a high degree of concurrency and multiple layers of abstraction. Concurrency is associated with resource contention and is one of the factors that cause the deadlock problem to increase. Multiple layers of abstraction make it more difficult to isolate and repair the deadlock environment.

Typically, when two or more threads are executed concurrently, if each thread occupies one resource and requests another, a deadlock situation occurs. Because if a thread cannot get a resource, all threads cannot continue to execute, and we call that particular thread blocked, and if each thread is blocked by another thread in the same group, we call the thread group deadlocked.

In this article, we will discuss two types of deadlock situations that occur in a typical important Java EE application: "Simple" database deadlock and cross resource deadlock. Although our discussion is based on the Java EE platform, it is also suitable for other technical platforms.

Database deadlock

In a database, a connection can block another connection if it occupies a database lock required by another connection. If two or more than two connections are blocking each other, they cannot continue, which is called a deadlock.

The database deadlock problem is not easy to handle because the locks involved are usually not explicit. Typically, when you implicitly update a row of data, you need to lock the data row, perform the update, and then release the lock when the enclosing transaction is committed or rolled back. Because of the database platform, the isolation level of the configuration, and the query hints, the locks acquired may be fine-grained or coarse-grained, blocking (or not blocking) other queries against the same data row, table, or database.

The acquired lock relies on an internally generated query plan. The plan can also change as the data size and step change over time. This allows a query that acquires a set of locks in an environment to try to get a completely different set of locks in another environment. When necessary, the database can increase its locks arbitrarily. For example, a database might choose to lock an entire page instead of locking 10 rows of data in the same data page, blocking read and write access to data rows that do not need to be locked.

Based on the database schema, read and write operations require traversing or updating multiple indexes, validating constraints, executing triggers, and so on. More locks will be introduced for each requirement. In addition, other applications may be accessing certain objects in the same database schema and acquiring locks that are different from those of your application.

All of these factors are combined, and the database deadlock is almost impossible to eliminate. Thankfully, database deadlocks are usually recoverable: When a database discovers a deadlock, it forces a connection to be destroyed (usually with a minimal connection) and rolls back its transaction. This frees all locks associated with the closed transaction, allowing at least one of the other connections to get a lock that they are blocking.

Because the database has this typical deadlock handling behavior, the database can often only retry the entire transaction when a database deadlock problem occurs. When a database connection is destroyed, an exception that can be caught by the application is thrown and identified as a database deadlock condition. If a deadlock exception is allowed to propagate beyond the layer of code that initializes the transaction, the code layer can start only one new transaction and do all previous work. To use this policy correctly, its code cannot have other operations until the transaction is successfully committed. Note: To limit the number of retries, blocks of code that easily lead to deadlocks are permanently cycled.

If there is a problem, try again, this method is a bit stupid. However, since the database is free to acquire locks, it is almost impossible to guarantee that a database deadlock does not occur for two or more two threads. This approach can at least guarantee that the application will function correctly in the event of some rare database deadlock. This is much better than requiring the user to retry the operation.

In the Java EE application, a developer can set up an EJB call to use the Bean managed transaction (BMT)--a developer initiates, commits, or rolls back a particular transaction or container managed transaction (CMT)--to start the transaction before the method is invoked, and to commit or rollback the transaction after the method completes. If the EJB vendor provides retry-on-deadlock parameters so that it can be done automatically through a container managed transaction, that would certainly be great. Without this automatic feature, the developer will eventually force the EJB call to use the Bean managed transaction only to retry the deadlock.

The specific frequency of deadlock problems and locks on other threads depends largely on the database platform, hardware, database schema, and query. In databases using lock-based concurrency control, such as MSSQL, UNCOMMITTED writes block read operations, while uncommitted reads Block writes and make the database more likely to deadlock. In multiple-version concurrency control (MVCC) databases, such as Oracle, Uncommitted writes do not block reads-read operations view only old versions of data rows. While this introduces other problems, it does not create the same number of deadlock opportunities. We're going to familiarize ourselves with these database locking patterns and pay attention to the types we're using.

There are some good reference methods in finding, fixing, and avoiding database deadlocks, but none of them can completely eliminate the possibility of deadlocks.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.