Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jOOQ fetchResultSet未关闭与Kotlin的连接_Kotlin_Jooq_Hikaricp - Fatal编程技术网

jOOQ fetchResultSet未关闭与Kotlin的连接

jOOQ fetchResultSet未关闭与Kotlin的连接,kotlin,jooq,hikaricp,Kotlin,Jooq,Hikaricp,我正在使用Kotlin与HikariCP和jOOQ来查询我的数据库。我逐渐意识到,这段代码按预期工作,获取行并在之后关闭连接: class CountriesService(private val datasource: DataSource) { private val countries = Countries() fun getCountries(): List<Country> { DSL.using(datasource, SQLDial

我正在使用Kotlin与HikariCP和jOOQ来查询我的数据库。我逐渐意识到,这段代码按预期工作,获取行并在之后关闭连接:

class CountriesService(private val datasource: DataSource) {

    private val countries = Countries()

    fun getCountries(): List<Country> {
        DSL.using(datasource, SQLDialect.POSTGRES_10)
            .use { ctx ->
                return ctx.select(...)
                    .from(...)
                    .orderBy(...)
                    .fetch(Country.mapper) // val mapper: (CountriesRecord) -> Country = {...}
            }
    }
}
我已经看到AbstractResultQueryfetchResultSet正在委托给fetchLazy方法,所以不确定它是否与此有关

如果我自己获得连接,而不是将其委托给DSLContext,那么它可以工作:

class CountriesService(private val datasource: DataSource) {

    private val countries = Countries()

    fun getCountries(): List<Country> {
        val conn = datasource.connection
        conn.use {
            val rs = DSL.using(it, SQLDialect.POSTGRES_10)
                .select(...)
                .from(...)
                .orderBy(...)
                .fetchResultSet()
            return Country.mapper.stream(rs).toList() // val mapper = JdbcMapperFactory.newInstance()...
        }
    }
}

这是我应该使用的最后一种方法吗?

生成资源的代码总是负责关闭它。那就是你。资源就是结果集。您的代码应该如下所示:

类CountriesServiceprivate val数据源:数据源{ 私人val国家=国家 有趣的国家:列表{ val ctx=DSL.usingdatasource,sqldial.POSTGRES_10 回来 ctx。选择。。。 从…起 .orderBy。。。 .fetchResultSet .使用{ return Country.mapper.streamit.toList } } } 关于调用DSLContext.use 请注意,就像我建议您不要在jOOQ的DSLContext类型上调用use一样,因为您不需要它。在您的情况下,当您将数据源传递给DSLContext时,它并没有足够的资源

关于调用ResultSet.use 相反,您应该在ResultSet上调用use,这保证了它在消费之后是关闭的。在本例中,我假设您对toList的调用将急切地消耗封装结果集的整个流

这里需要记住的重要一点是,您可以通过调用jOOQ的ResultQuery.fetchResultSet来生成资源,即使您将其传递给另一个库,也不需要另一个库来关闭它。但你是

class CountriesService(private val datasource: DataSource) {

    private val countries = Countries()

    fun getCountries(): List<Country> {
        val conn = datasource.connection
        conn.use {
            val rs = DSL.using(it, SQLDialect.POSTGRES_10)
                .select(...)
                .from(...)
                .orderBy(...)
                .fetchResultSet()
            return Country.mapper.stream(rs).toList() // val mapper = JdbcMapperFactory.newInstance()...
        }
    }
}