java - Proper way to do jdbc calls from GWT? -
i doing rpc's in gwt query database , have memory leak in program , have strong suspicion in way have class set up. currently, have connect database within each method have , close out connection @ end of method. doing way cause memory leak? feel programming way leads more code , highly inefficient can't find many samples go off, can suggest better way work? names, ip's have been changed, , i've posted connect method sample of how other method's written.
public class databaseserviceimpl extends remoteserviceservlet implements databaseservice { private connection con = null; string database = "ioma"; string host = "localhost"; string password = "foo"; string url = "jdbc:mysql://" + host + "/" + database; string user = "foo"; public string connect() { string connect = ""; try { class.forname("com.mysql.jdbc.driver"); con = drivermanager.getconnection(url, user, password); } catch (sqlexception e) { (throwable t : e) system.err.println("error connecting database: " + t); } catch (classnotfoundexception e) { e.printstacktrace(); } connect = "database connection aquired"; return connect; } public int getassemblercount() { connect(); try { int assemblercount = 0; string query = "select count(*) assemblers"; preparedstatement ps = con.preparestatement(query); resultset rs = ps.executequery(); rs.next(); assemblercount = rs.getint(1); rs.close(); ps.close(); con.close(); return assemblercount; } catch (sqlexception e) { (throwable t : e) system.err.println("database error in getassemblercount " + e); return 0; } }}
you should close in "finally" block, close happens in event of exception.
see answers this related question on doing properly.
this common source of memory leaks , problem.
Comments
Post a Comment