Hello, everyone. :raised_hand: I implement databas...
# ktor
d
Hello, everyone. I implement database connecting process (
Database.connect()
) on the beginning of Ktor’s booting process (the first line of
Application.module()
). When I use Ktor v2, the
testApplication
method creates a Ktor server application on each test method. So, errors happens which says
too many db clients
. How do I solve it? Does anyone know this? 🙏
h
What about disconnecting the client after each test?
d
Good suggestion, thanks. I found the way to close db connection on stopping application. https://github.com/ktorio/ktor-samples/blob/main/kweet/src/KweetApplication.kt#L109
j
I'm facing exactly the same problem, @doyaaaaaaken , the repository link you shared leads to 404 , could you please share your solution!?
d
@JPilson Sumbo I don’t remember much, but I now use the following methods.
Copy code
DbConnector.connect()

//This process is executed when application stops
environment.monitor.subscribe(ApplicationStopped) {
    DbConnector.close()
}
j
@doyaaaaaaken how does your test setup look like?
d
Implement below abstract class and inherit it in case using DB in test.
Copy code
abstract class DbAccessTest {

    fun <T> withDbAccess(proc: () -> T): T {
        return transaction {
            val result = proc()
            rollback()
            result
        }
    }

    companion object {
        init {
            //connection procesure
            //...
            Database.connect(ds)
        }
    }
}
j
I am ale to close the connection but I still have the issue For me I believe the case is that, despeite closing the connection there too many server instances