Question about Database Connection Pools

A

apue

Hi Newsgroup,

i have written an Objectpool for Database connections. All locked
connections are stored within a Vector (called _used) and all unlocked
connections are stored within a second Vector (called _unused).

Now consider the following situation:

------------------- code ---------------------------------------
DbConnectionPool pool = new DbConnectionPool( driver, url, user, passwd);
Connection dbcon = pool.getConnection();

int sizeUsed = pool.getUsed(); // returns 1, thats ok

// ...
// ... lets do some actions with dbcon ....
// ...
// ... programmer does not notice
// ... that dbcon is still in use.
// ... so he checks out a new connection
//
dbcon = pool.getConnection();

sizeUsed = pool.getUsed(); // return 2, uppps!!!

------------------- code ---------------------------------------

Because the programmer checks out a new connections, two database
connections are now marked as _used.

Is there any possibility to avoid such situations?

Thank you in advance

mike
 
D

Dorian

Well it's possible that the behavior you currently have would meet the
expectations of someone using your pool but if you want to enforce the
same connection being returned you could store it in a ThreadLocal
variable when getConnection() is called. This would require that the
connection returned is subclassed to override the close() method which
would remove it from the ThreadLocal and add it back to the pool.

With all the freely available connection pools why are you writing your own?

Regards,
Bob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top