java - Using DbUtils QueryRunner doesnt close database connections -
im using dbutils library queryrunner class run queries. underestanding of dbutils docs dont need worry closing connections. reason not close connections automaticly or manually. here code:
public static datasource createdatasource() { basicdatasource d = new basicdatasource(); d.setdriverclassname(globals.getinstance().getkonfiguracija().dajpostavku("driver")); d.setusername(globals.getinstance().getkonfiguracija().dajpostavku("dbusername")); d.setpassword(globals.getinstance().getkonfiguracija().dajpostavku("dbpassword")); d.seturl(globals.getinstance().getkonfiguracija().dajpostavku("serverdatabase")); return d; } public static void insertsql(string sql) { datasource datasource = createdatasource(); queryrunner qr = new queryrunner(datasource); try { int inserts = qr.update(sql); qr.getdatasource().getconnection().close(); } catch (sqlexception ex) { logger.getlogger(dbcontroller.class.getname()).log(level.severe, null, ex); } }
since im having thread periodically calls insertsql method, after time "too many connections" error. im using newest version of tomcat , database mysql.
you're creating new datasource
on every call insertsql()
method: cause of exception.
instead, should create datasource
once, , pass instance queryrunner
constructor, example:
queryrunner queryrunner = new queryrunner(datasource);
using datasource
, don't need manually close connection.
see https://commons.apache.org/proper/commons-dbutils/examples.html more examples.
Comments
Post a Comment