简体   繁体   中英

Database hangs while using static connection

I have one basic question here - I am working with a new Database named XpressMP .

I wrote a multithreaded program to insert lots of records into the database. I noticed one most important thing while making a connection to database:

In my program, If I am using something like below-

class Task implements Runnable {

   private Connection dbConnection = null;
   private PreparedStatement preparedStatement = null;

   //other stuff

 @Override
  public void run() {

   try {

      dbConnection = getDBConnection();

   } finally {
        if (preparedStatement != null) {
            try {
                preparedStatement.close();
                preparedStatement = null;
            } catch (SQLException e) {
            }
        }
        if (dbConnection != null) {
            try {
                dbConnection.close();
                dbConnection = null;
            } catch (SQLException e) {
            }
        }
    }
  }
}

it works fine and I am closing each and every connection in the final block. And I can insert a lot more rows to database.

But as soon as I start using static connection intentionally (which I shouldn't be doing) with multiple threads-

class Task implements Runnable {

   private static Connection dbConnection = null;
   private static PreparedStatement preparedStatement = null;

   //other stuff

 @Override
  public void run() {

  try {

    dbConnection = getDBConnection();

   } finally {
        if (preparedStatement != null) {
            try {
                preparedStatement.close();
                preparedStatement = null;
            } catch (SQLException e) {

            }
        }
        if (dbConnection != null) {
            try {
                dbConnection.close();
                dbConnection = null;
            } catch (SQLException e) {
            }
        }
    }
  }
}

The whole Database hangs. I cannot work with that database until I restart the db. So that means there is some problem with the JDBC driver that I have. I have told the DBA about this problem and they are talking with the folks who owned that Database.

But my question is why it hangs. For what reason?

You are sharing one object reference variable among various threads... Sometimes, one connection object is created and before it is closed (and its reference be set to null), another thread starts, creating another object and never closing the old one. So, that connection is still alive and in some point of the time, too much connections will be opened and your DBMS may crash, spiting some error like "too many connections". The problem is not your drive, but your code. Note that some thread may close a connection that was created by another thread too!

I think you are not seeing the exception that is being throw because you not have a catch in you outer try . The exception will be throw when ou try to create an connection. Try to put a catch there to show the stack trace. It will work (it might work) if your database is detecting this problem.

JDBC is not thread-safe so you should not use one Connection object from several threads in parallel, regardless of whether it is stored in static field or not. Once you do this, it is possible that one thread will send its query in the middle of other thread's query, so database will receive complete garbage and, if database is not rock-solid, this garbage may break down the database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM