无法关闭与MyBatis的数据库连接

无法关闭与MyBatis的数据库连接

本文介绍了无法关闭与MyBatis的数据库连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做什么:

我有一个用Kotlin(JVM)编写的AWS Lambda,它可以从队列中读取消息并在MySQL表上写一些东西.

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

我为此目的使用MyBatis,这是我在Handler中所做的简短摘要:

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()
}

我的问题:

执行此Lambda时,某些连接在数据库上保持打开状态.仅当lambda容器自动销毁时,它们才会被销毁.

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?

推荐答案

您正在使用PooledDataSource创建连接池.更改为UnpooledDataSource,可能会解决该问题.

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

这篇关于无法关闭与MyBatis的数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 09:25