Today:
I continue to work on an application, which, constantly creates and closes a LOT of (sql) connections to the same (Oracle) database, using the same credentials running very basic selects, updates and inserts.
Currently, a singleton CustomConnectionManager
checks in its pool to see if any CustomConnector
s are available to be given out. Once given out they may be closed by the client. Upon close()
the underlying SQLConnection
(created and maintained by the CustomConnector
) is also closed. If CustomConnector
is unavailable, new CustomConnector
is created.
The good thing about this is that ultimately SQLConnection
remains closed after each use, however, very little reuse is going on as the value lies in SQLConnection
not in CustomConnector
.
Since all users of the system will connect to the same database, the same single connection may be used to accommodate all requests. Original solution that created new Connectors upon each request seems very wasteful.
Proposed:
singleton CustomConnectionManager
will maintain 2 queues:
- a queue of available
CustomConnector
, each of which will maintain it's ownSQLConnection
and - a queue of
inUse
CustomConnector
s. Upon request newCustomConnector
will be created and given out.
Client interaction only happens with the singleton CustomConnectionManager
.
When new connection is needed, manager creates it, gives it out to client and places it in inUse
queue. When client is done using the connection, instead of closing it, client will .markConnectorAvailable()
, which would put it back into the availableConnectors
queue ( Client will no longer be able to control underlying SQLConnection)
Question 1: What do you think? Will this work? Is there an existing solution that already does this well?
Question 2: If proposed approach is not a complete waste, what is a good point for CustomConnector
's to close it's SQLConnections?