Can't close db connection with MyBatis

亡梦爱人 提交于 2019-12-12 19:20:45

问题


What I'm doing:

I've got an AWS Lambda, written in Kotlin (JVM) which reads a message from a queue and writes something on a MySQL table.

I'm using MyBatis for this purpose, and this is a short simplified snippet of what I'm doing inside the Handler:

// initializing configuration
val dataSource = PooledDataSource(driver, url, username, password)
val environment = Environment(environmentName, JdbcTransactionFactory(), dataSource)
val configuration = Configuration(environment)

try {
    val builder = SqlSessionFactoryBuilder()
    val session = builder.build(configuration).openSession()
    val mapper: CustomMapper = session.getMapper(CustomMapper::class.java)
    mapper.doSomething()
    session.commit()
} finally {
    session.close()
}

My problem:

When this Lambda is executed, some connections remain opened on the database. They get destroyed only when the lambda container is automatically destroyed.

Why is this happening since I'm closing all the sessions? Is there anything that I can do in order to prevent this behaviour?


回答1:


You are using PooledDataSource which creates a connection pool. Change to UnpooledDataSource and that will likely resolve the issue.



来源:https://stackoverflow.com/questions/49153277/cant-close-db-connection-with-mybatis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!