abierto
abierto

Reputation: 1467

Can't close db connection with MyBatis

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?

Upvotes: 2

Views: 2954

Answers (1)

Jeff Butler
Jeff Butler

Reputation: 991

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

Upvotes: 1

Related Questions