问题描述
我正在使用带有viertix.io和scala monix的HikariCP-3.4.2。
使用Hikari连接池就像创建
def createDataSource(autoCommit: Boolean = false,connectionTimeout: Long = 60000,idleTimeout: Long = 30000,maxLifetime: Long = 180000,maxPoolSize: Int = 50,minIdlePoolSize: Int = 2,poolName: String = "Database Connection Pool"): HikariDataSource = {
val hc = new HikariConfig()
hc.setPoolName(poolName)
hc.setConnectionTestQuery("select 1 from dummy")
hc.setDriverClassName("com.MysqL.jdbc.Driver")
hc.setConnectionTimeout(connectionTimeout)
hc.setIdleTimeout(idleTimeout)
hc.setMaxLifetime(maxLifetime)
hc.setMaximumPoolSize(maxPoolSize)
hc.setMinimumIdle(minIdlePoolSize)
hc.setJdbcUrl(host)
hc.setUsername(user)
hc.setPassword(password)
hc.setAutoCommit(autoCommit)
hc.setRegisterMbeans(true)
hc.setLeakDetectionThreshold(40000);
new HikariDataSource(hc)
}
数据源池的构建方式类似于
val ds: HikariDataSource = HanaConnection.createDataSource(maxPoolSize = 40,poolName = "my-db-pool")
我有两个使用vertx.io从api调用的不同函数。 noLeak
函数正在获取数据源,然后构建JDBC模板并执行简单的select语句。 jprofiler显示没有连接泄漏。
但是另一个功能leak
使用SingleConnectionDataSource构建的jdbctemplate,即使我显式关闭连接,该功能也会显示连接泄漏。
def noLeak(router: Router)(implicit ds: HikariDataSource): Route ={
implicit val jt: JdbcTemplate = new JdbcTemplate(ds)
val route = router.route(HttpMethod.GET,"/noleak").handler(BodyHandler.create())
route.handler(ResponseTimeHandler.create())
route.handler(rc=>{
rc.response.putHeader("content-type","application/json")
Task( jt.execute("SELECT * FROM DUMMY") ).runAsync{
result =>
result match {
case Left(value) => rc.response.end(value.getMessage)
case Right(value) => rc.response.end("DONE")
}
}
})
}
def leak(router: Router)(implicit ds: HikariDataSource): Route ={
val con = ds.getConnection
implicit val jt = new JdbcTemplate(new SingleConnectionDataSource(con,true))
val route = router.route(HttpMethod.GET,"/leak").handler(BodyHandler.create())
route.handler(ResponseTimeHandler.create())
route.handler(rc=>{
rc.response.putHeader("content-type","application/json")
Task(jt.execute("SELECT * FROM DUMMY")).runAsync{
result =>
result match {
case Left(value) => {
con.close()
rc.response.end(value.getMessage)
}
case Right(value) => {
con.close()
rc.response.end("DONE")
}
}
}
})
}
下面是jprofiler连接泄漏的屏幕快照,即使显式连接关闭后,第二个功能的错误也没有关闭连接。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)