gpt4 book ai didi

grails - groovy.sql.Sql.firstRow 执行后是否关闭连接?

转载 作者:行者123 更新时间:2023-12-02 14:59:59 26 4
gpt4 key购买 nike

在 MyService 我有以下内容:

import groovy.sql.Sql

class MyService {
Sql groovySql

def serviceMethod(){
groovySql.firstRow("some query.....")
}
}

在resources.groovy groovySql 注入(inject)如下:
groovySql(groovy.sql.Sql, ref('dataSource'))

这是一个 Grails 2.4.5 应用程序。现在,问题是何时 serviceMethod被调用,连接会自动关闭吗?

最佳答案

Sql 中的每个方法必要时创建和释放资源。

Under the covers the facade hides away details associated with getting connections, constructing and configuring statements, interacting with the connection, closing resources and logging errors.



如果您创建 SqlDataSource ,每次都会获得一个新的连接,并在操作结束时关闭它。

If we are using a DataSource and we haven't enabled statement caching, then strictly speaking the final close() method isn't required - as all connection handling is performed transparently on our behalf; however, it doesn't hurt to have it there as it will return silently in that case.

关于grails - groovy.sql.Sql.firstRow 执行后是否关闭连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35588115/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com