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?