从groovy中的dataSource获取连接对象
我们的应用程序定义了 Datasource.groovy。 我在这样的控制器中使用它,
import javax.sql.DataSource
class xxx {
javax.sql.DataSource dataSource
def myappsql = new Sql(dataSource)
myappsql.row(querystring) .....
}
我需要从数据源获取连接对象,这是否可以直接像这样:
Connection conn = myappsql.getConnection()
既然这给了我错误,它是否必须是 groovy.sql.Sql 拨打此电话?
我试图避免使用用户、密码和 url 字符串从头开始创建连接对象,而是重用数据源中声明的连接对象。
Our Application has a Datasource.groovy defined.
I am using it in a controller like this
import javax.sql.DataSource
class xxx {
javax.sql.DataSource dataSource
def myappsql = new Sql(dataSource)
myappsql.row(querystring) .....
}
I need to get connection object from the datasource, is this possible directly like this :
Connection conn = myappsql.getConnection()
Since this is giving me error, does it have to be a groovy.sql.Sql
to do this call?
I am trying to avoid creating a connection object from scratch, with user, password and url string and instead reuse the one declared in datasource.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该能够执行以下操作:
我相信您在 使用数据源构造 Sql 对象
You should be able to do:
I believe you're getting the error as you construct the Sql object with a datasource
您可以依赖注入
SessionFactory
,而不是依赖注入DataSource
Instead of dependency-injecting the
DataSource
, you can dependency inject theSessionFactory