I have a java application where on thread is the socket connection which captures data and pass on to another thread for databaprocessing using the producer and consumer concept by putting into the queue. The problem is that database processing thread at times fail to run and I notice that there are some of resultset and statement have not been closed. Can this be the reason for it to fail and or is there any other reason. How I know it fail is using the isAlive method to check on the database processing thread which shows no value means is dead.
Snippet of my codes.
private LinkedBlockingQueue<String> databaseQueue = new LinkedBlockingQueue<String>();
class DatabaseProcessor implements Runnable {
public void run()
{
try
{
createConnection();
while (true)
{
message = databaseQueue.take();
//all the database processing here with multiple queries and resultsets
}
}
catch (Exception e)
{
e.printStackTrace(System.out);
try
{
dbconn.rollback();
}
catch (Exception rollback)
{
rollback.printStackTrace(System.out);
}
}
}
}
Who knows without looking at all of your code?
Failing to close scarce resources of all kinds, not just those associated with databases, eventually manifests itself in exhaustion and unavailability. Connections to databases are finite. Result sets are cursors, which are also limited.
You should close resources in the scope of the method in which they were created. Acquire them in a try block and close them in a finally block, wrapped in individual try/catch blocks.