Java MySQL connetion pool is not working -


i've written function in java runs mysql query , returns results. i've implemented connection pooling using method here: http://www.kodejava.org/how-do-i-create-a-database-connection-pool/. function working, connection time still same without pooling ~190 ms. can tell me doing wrong?

this code:

public static arraylist<map<string,object>> query(string q) throws exception {      long start, end;      genericobjectpool connectionpool = null;      string driver = "com.mysql.jdbc.driver";      string url = "jdbc:mysql://localhost/dbname";     string user = "root";     string pass = "";      class.forname(driver).newinstance();      connectionpool = new genericobjectpool();     connectionpool.setmaxactive(10);      connectionfactory cf = new drivermanagerconnectionfactory(url, user, pass);      poolableconnectionfactory pcf = new poolableconnectionfactory(cf, connectionpool, null, null, false, true);      datasource ds = new poolingdatasource(connectionpool);      //create statement     statement stm = null;      try {          connection con = null;         preparedstatement stmt = null;          start = system.currenttimemillis();          con = ds.getconnection();          end = system.currenttimemillis();         system.out.println("db connection: " + long.tostring(end - start) + " ms");          //fetch out rows         arraylist<map<string, object>> rows = new arraylist<map<string,object>>();          stm = con.createstatement();          //query         resultset result = null;          boolean returning_rows = stm.execute(q);          if (returning_rows) {             result = stm.getresultset();         } else {             return new arraylist<map<string,object>>();         }          //get metadata         resultsetmetadata meta = null;         meta = result.getmetadata();          //get column names         int col_count = meta.getcolumncount();         arraylist<string> cols = new arraylist<string>();         (int index=1; index<=col_count; index++) {             cols.add(meta.getcolumnname(index));         }          while (result.next()) {             hashmap<string,object> row = new hashmap<string,object>();             (string col_name:cols) {                 object val = result.getobject(col_name);                 row.put(col_name,val);             }             rows.add(row);         }          //close statement         stm.close();           //pass rows         return rows;      } catch (exception ex) {          system.out.print(ex.getmessage());         return new arraylist<map<string,object>>();      } {          if (stm != null) {             stm.close();         }         if (stm != null) {             stm.close();         }          system.out.println("max connections: " + connectionpool.getmaxactive());         system.out.println("active connections: " + connectionpool.getnumactive());         system.out.println("idle connections: " + connectionpool.getnumidle());      }  } 

this console output every time:

db connection: 186 ms max connections: 10  active connections: 1  idle connections: 0  

update: should note, java application uses works this: executes, runs 1 query , closes. figured if php works , it's using connection pooling default, should java? correct me if i'm wrong.

you create connection pool, don't put it. connection pool empty when created, first request guaranteed to create new connection , slow getting connection manually.

try putting code loop, repeatedly connection pool. try once, 5 times, ten times , fifteen times. note how results change.

some connection pools support automatically creating , holding minimum number of connections ready use maximum. when pool initialised pre-fetch connections first few calls aren't delayed.


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -