Database Transaction Leak in Java Application

Database Transaction Leak in Java Application

In a real time application the Database leak occurs due to Unclosed transactions created by the programmers. The stakes are high in this case because with every connection that remains unclosed, the connection pool gets exhausted which in-turn leads to the failure of entire application. As we are unable to establish a connection to the application (login), In most cases it remains untraceable.

 Here are a few scenario’s

  1. Exception occurs before commit transaction

In this case the developer performs the commit operation in try block but not within the catch block. Here the developer presumes that the particular piece of code works fine during the phase of development but fails at a later point of time due to NullPointerException which results in unclosed connection. This is one of the inevitable issue that goes by unnoticed during the development activity until and unless the exception occurs. Even-though you take measures to prevent it.

To prevent this we can include it as part of finally block either to commit or rollback the transaction.

2. Disconnected Object: 

In this case even though you close the session, the transaction object remains open unless you perform rollback or commit operation on it. The thread remains in active state or the object is alive until the particular thread is garbage collected.

Perform the commit or rollback operation before closing the session.

3. Specific catch block exception:

In some cases the developer ends up initially adding a catch block with generic Exception. Later, as part of the code review comments, the developer is asked to catch a specific exception in which case he does not add the commit or rollback operation as part of it.

Add the rollback operation as part of the new catch block.

4. Auto-commit:

This one is specific based on the design of the application by the architect, in some applications the auto-commit option is set to true by default. In this case if the developer is trying to perform a commit operation at a later point of time. It will result in a NullPointerException, since the transaction was already committed. This problem does not come into light until we reach a point where we are unable to login to the application after the predefined number of pool sessions(connections) get exhausted. eg: 100 sessions.

A complete pool release is said to be done when both connection and session are released. We can set the AutoCommit default option in try block to false, so that when the code reaches the finally block it can perform commit operation.

We can find these issues by looking into Thread dumps.

1. Thread state:

In case of no leak:

The thread consumes the transaction and if the thread is still active, it means it is working on the specified task.

In case of a leak:

The thread has moved on or thread is in idle state, but the transaction is still present then it is a leak. In this case we can look up the thread dump to know which transactions are open.

2. Obtain and Release of Object locks (Threads)

When you find a pattern wherein the pool acquires and releases it, but at a certain point the thread just borrows but does not return it as shown below is a lead to suspicious activity which needs further inspection.

If you enjoyed reading it ,you can click the like, share button and let others know about it. If you would like me to add anything else, please feel free to leave a response ??

Chitrakannan Balasubramanian

Product and Growth @ Ideas2IT | GX19

6 年

It's very informative Jay. Thank you ! :)

要查看或添加评论,请登录

Jayvardhan Reddy Vanchireddy的更多文章

  • Apache Spark-3.0 Sneek peak

    Apache Spark-3.0 Sneek peak

    Apache Spark has remained strong over the years and now is coming back with one of its major releases with its ongoing…

    14 条评论
  • Working of Sqoop Incremental Load

    Working of Sqoop Incremental Load

    In my series of BigData Architecture, we have seen the internal working of Sqoop. Now as part of this article, we'll…

    3 条评论
  • Deep-dive into Spark Internals & Architecture

    Deep-dive into Spark Internals & Architecture

    Apache Spark is an open-source distributed general-purpose cluster-computing framework. A spark application is a JVM…

    12 条评论
  • Sqoop Architecture in Depth

    Sqoop Architecture in Depth

    Apache Sqoop is a data ingestion tool designed for efficiently transferring bulk data between Apache Hadoop and…

    9 条评论
  • HDFS Architecture in Depth

    HDFS Architecture in Depth

    Hadoop consists of mainly two main core components HDFS, MapReduce. HDFS is the Hadoop Distributed File System ( HDFS )…

    3 条评论
  • Hive Architecture in?Depth

    Hive Architecture in?Depth

    Apache Hive is an ETL and Data warehousing tool built on top of Hadoop for data summarization, analysis and querying of…

  • Application Development: 4 Simple Steps to Resolve Remote Debugging Connection Problems

    Application Development: 4 Simple Steps to Resolve Remote Debugging Connection Problems

    As a developer, we frequently debug the application during the development activities. The real time applications are…

    1 条评论
  • 5 Useful Tools for a Full-stack Developer

    5 Useful Tools for a Full-stack Developer

    The below tools will help you increase your productivity and reduce compilation issues on running a debug job…

    1 条评论
  • Analysis of Memory Leak in Java Applications via Heap?Dump

    Analysis of Memory Leak in Java Applications via Heap?Dump

    Memory plays a vital role in any application performance and we cannot afford to waste the resources unnecessarily, as…

    6 条评论
  • Heap dump generation & Analysis using JMAP, JMAT, JvisualVM Tools

    Heap dump generation & Analysis using JMAP, JMAT, JvisualVM Tools

    Every Programmer is bound to use these tools at some point of time, As it plays a vital role in optimizing the…

社区洞察

其他会员也浏览了