0

I have a manager threads which has its respective worker threads.The JDBC Connection is created one time by the parent of the manager thread and is passed to each of the worker threads to prepare their set of PreparedStatement objects.The service runs forever until one point of time in a day during which the main thread is interrupted and the connection object is closed and after that i call

System.exit(0);

So with this approach there is no track of how and when preparedstatements that are with the workers would be closed. Will calling close() on Connection object that is with the main thread release the db resources and gracefully close the PreparedStatements.If not, how can i make the threads to break out from their forever loop that they check on interruption flag and execute their finally block then notify the parent somehow and close the Connection object too.

EDIT: Each worker thread has 9 PreparedStatements and hence max 54 threads can be open at a time and at stake.

EDIT2- The duplicate marked question states that

 With a pool in place you can open/close connections and prepared statements according to best-practice and leave the resource management to the pool.

I cannot close the PreparedStatements and leave the rest to the pool,as far as i think it will only clog my pool.

Vishal Tyagi
  • 560
  • 9
  • 26
  • `ExecutorService` might be your friend –  Dec 01 '16 at 12:28
  • Or in the comment, [this might be duplicate too](http://stackoverflow.com/questions/4507440/must-jdbc-resultsets-and-statements-be-closed-separately-although-the-connection) – AxelH Dec 01 '16 at 12:30
  • 1
    Personally there are very few cases where I would stop a program using System.exit() . You should always manage you threads thread/connection pools. If you don't know how to do this you will never be able to write code that is deployed on a servlet container, or other applications where the JVM has a longer livespan than you code/application. – Klaus Groenbaek Dec 01 '16 at 12:35

0 Answers0