https://kotlinlang.org logo
#ktor
Title
# ktor
d

doyaaaaaaken

06/05/2022, 6:12 AM
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

hfhbd

06/05/2022, 1:52 PM
What about disconnecting the client after each test?
d

doyaaaaaaken

06/06/2022, 6:16 AM
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

JPilson Sumbo

03/31/2023, 12:53 AM
I'm facing exactly the same problem, @doyaaaaaaken , the repository link you shared leads to 404 , could you please share your solution!?
d

doyaaaaaaken

03/31/2023, 1:13 AM
@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

JPilson Sumbo

03/31/2023, 7:08 AM
@doyaaaaaaken how does your test setup look like?
d

doyaaaaaaken

03/31/2023, 7:16 AM
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

JPilson Sumbo

03/31/2023, 8:10 AM
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
5 Views