java - How would you test a Connection Pool -


i have implemented simple connectionpool in java. has no fancy features, get/release connection methods.

how can test working?

i know there plenty of connection pools ready use out there, more reliable i'll do, trying practice understand how connections pool work.

thank you!

here code in case helps:

public class connectionpoolimpl implements connectionpool {     private vector<pooledconnection> connections; // connections container     string url;     string username;      string password;      /**      * instanciates new mysqlconnectionpool      * @param nbconnectionsmax      */     public connectionpoolimpl(string dburl, string username, string password){         this.connections = new vector<pooledconnection>();         this.url = dburl;         this.username = username;         this.password = password;     }      /**      * returns connection pool, if available, or create new one.      *       * @return connection.      */     public connection getconnection() throws sqlexception {         synchronized(this.connections){             // checking if there available connection return             for(pooledconnection c : this.connections){                 if(!c.isused()){                     c.setused();                     return c.getconnection();                 }             }              // if there none, open new 1 , return             connection conn = drivermanager.getconnection(url, username, password);         pooledconnection pconn = new pooledconnection(conn);         pconn.setused();         connections.add(pconn);         return pconn.getconnection();         }     }      /**      * releases connection pool.      *       * @param con connection release.      */     public void releaseconnection(connection con) throws sqlexception {         synchronized(this.connections){             for(pooledconnection c : this.connections){                 if(c.getconnection().equals(con)){                     c.setfree();                     return;                 }             }         }       } } 

and pooledconnection.java:

public class pooledconnection {     private connection conn;     private boolean used;      public pooledconnection(connection conn){         this.conn = conn;         this.used = false;     }      public void setused(){         this.used = true;     }      public void setfree(){         this.used = false;     }      public boolean isused(){         return this.used;     }      public connection getconnection(){         return this.conn;     } } 

you test

  • getting connection when pool empty gives connection
  • getting connection when connection has been got , not released gives another, different connection
  • releasing connection doesn't throw exception
  • getting connection after 1 has been released gives same connection

note such unit test need real database, real user , password test. make connection pool depend on datasource, , build connectionpool using mock datasource returning mock connections, in order able test class without depending on real database.


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -