Question about Database Connection Pools

Discussion in 'Java' started by apue, Jul 23, 2006.

  1. apue

    apue Guest

    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
    apue, Jul 23, 2006
    #1
    1. Advertising

  2. apue

    Dorian Guest

    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

    On 2006-07-23 11:41:23 -0500, apue <> said:

    > 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
    Dorian, Jul 23, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Darryl L. Pierce

    Tomcat 4.0, MySQL and connection pools...

    Darryl L. Pierce, Aug 18, 2003, in forum: Java
    Replies:
    0
    Views:
    364
    Darryl L. Pierce
    Aug 18, 2003
  2. marvind
    Replies:
    0
    Views:
    313
    marvind
    Feb 1, 2006
  3. Replies:
    0
    Views:
    474
  4. Gen
    Replies:
    2
    Views:
    1,039
  5. Eric Newton
    Replies:
    0
    Views:
    101
    Eric Newton
    Aug 15, 2003
Loading...

Share This Page