java - Avoid automatic jdbc connection close in servlet -


i have servlet deployed on jetty 9 server connects mysql 5.6.17 server using connector/j jdbc driver http://dev.mysql.com/downloads/connector/j/5.0.html.

this particular servlet fires sql statement inside loop iterates around 10 times. have include

drivermanager.getconnection(db_url, user, password); 

line within loop because connection closes automatically after sql statement has been executed in every iteration of loop.

is there way keep connection open, getconnection() need executed once before loop starts , can manually close in block.

i have found many posts on particular issue, refer connection pooling concept solution. interested in avoiding connection being closed automatically after each query execution. shouldn't simple parameter? not facing particular performance problem right now, seems waste of processor , network cycles.

servlet code :

public class checkphonenumberregistrationservlet extends httpservlet {  @override protected void dopost(httpservletrequest req, httpservletresponse resp)         throws servletexception {      system.err.println("started checkphonenumberregistrationservlet");       // define database connection details     final string jdbc_driver = "com.mysql.jdbc.driver";     final string db_url = databaseparameters.slappdburl;     final string user = databaseparameters.dbserver_username;     final string password = databaseparameters.dbserver_password;     preparedstatement prpd_stmt = null;     connection conn = null;     resultset rs = null;     int resultvalue;      // open connection     /*try {         conn = drivermanager.getconnection(db_url, user, password);     } catch (sqlexception e2) {         // todo auto-generated catch block         e2.printstacktrace();     }*/      jsonparser jsparser;     jsonelement jselement;     jsonarray jsrequestarray;     jsonarray jsresponsearray = new jsonarray();     stringbuffer jb = new stringbuffer();     string line = null;     try {         bufferedreader reader = req.getreader();         while ((line = reader.readline()) != null)             jb.append(line);     } catch (exception e) {         e.printstacktrace();     }      try {         jsparser = new jsonparser();         jselement = (jsonelement) jsparser.parse(jb.tostring());         jsrequestarray = jselement.getasjsonarray();            (int = 0; < jsrequestarray.size(); i++) {             // system.err.println("i : " + +             // jsrequestarray.get(i).tostring());              try {                  conn = drivermanager.getconnection(db_url, user, password);                  prpd_stmt = conn                         .preparestatement("select slappdb.isphonenumberregistered(?)");                  prpd_stmt.setstring(1, jsrequestarray.get(i).tostring()                         .replace("\"", ""));                  rs = prpd_stmt.executequery();                 if (rs.first()) {                     //system.err.println("result sert sql server : " + rs.getstring(1));                     //slappdb.isphonenumberregistered() returns boolean                     //but converting result value int here there no appropriate boolean conversion function available.                     resultvalue = integer.parseint(rs.getstring(1));                     if(resultvalue == 1)                         jsresponsearray.add(jsparser.parse("y"));                     else if(resultvalue == 0)                         jsresponsearray.add(jsparser.parse("n"));                     else throw new sqlexception("the value returned mysql server slappdb.isphonenumberregistered(" + jsrequestarray.get(i).tostring().replace("\"", "") + ") unexpected : " + rs.getstring(1) + ".\n");                     // system.err.println("y");                 }                  else throw new sqlexception("unexpected empty result set returned mysql server slappdb.isphonenumberregistered(" + jsrequestarray.get(i).tostring().replace("\"", "") + ").\n");              } catch (sqlexception e) {                  e.printstacktrace();             } catch (exception e) {                 e.printstacktrace();             } {                 try {                     if (prpd_stmt != null)                         prpd_stmt.close();                 } catch (sqlexception e1) {                 }                 try {                     if (conn != null)                         conn.close();                 } catch (sqlexception e1) {                 }             }         }          resp.setstatus(httpservletresponse.sc_ok);         resp.setcontenttype("text/plain");         // resp.setcontentlength(1024);         resp.getwriter().write(jsresponsearray.tostring());         system.err.println(jsresponsearray.tostring());      } catch (exception e) {         // crash , burn         system.err.println(e);     } 

the problem you're closing connection inside for loop. move both statements: connection opening , connection close, outside loop.

conn = drivermanager.getconnection(db_url, user, password); (int = 0; < jsrequestarray.size(); i++) {     try {         //current code...     } catch (sqlexception e) {         e.printstacktrace();     } catch (exception e) {         e.printstacktrace();     } {         try {             if (prpd_stmt != null)                 prpd_stmt.close();         } catch (sqlexception e1) {         }     } } try {     if (conn != null)         conn.close(); } catch (sqlexception e1) { } 

Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -