I'm building a multi-threaded system that uses a central database.
The problem I'm having is that I want to create a fixed size of database connections.
I used Executors.newFixedSizePool
to create a maximum number of pooling threads equal to MAX_N
.
I'm executing my threads using execute
function available by ExecutorService
.
I want to build my system in a way that each pool thread within my pool holds a single connection to the database as long as this pool thread is still alive. So, when I execute
my threads within the pool, there is only MAX_N
connections to the database. Also, since Executors.newFixedSizePool
pays attention to the number of pooling thread, and creates new ones if some of them stopped because of an exception, I want the same to be hold for my database connection.
So what I'm looking for is to create a fixed size of pool thread using Executors.newFixedSizePool
, in a way that each pooling thread has it's own connection to database. If a pooling thread terminates this connection is closed, and if a new pooling thread is constructed, a new connection is also constructed and linked to this pooling thread.
I googled a way to do this but failed to find anything helpful. Is there a correct way to do this?
ExecutorService
? or to create a connection pool using my database? – Said A. Sryheni