【发布时间】:2013-09-08 08:24:53
【问题描述】:
我使用的是 Grails 2.2.1,并且我有一个自定义数据源注入到服务中,以便我可以执行一些 SQL 查询。
第一次执行时,有一个数据源,但在随后的每次调用中,对数据源的引用都变为空。
class ReportService {
def dataSource_myds
Object[] reportRecords(int a) {
String query = "SELECT ..."
Object[] resultSet;
Sql sql = new Sql(dataSource_myds)
// ^ Here the NullPointerException is thrown
// But it always works at the first execution
sql.eachRow(query, [a]) {
...
resultSet += result
}
return resultSet
}
}
class ReportController {
ReportService reportService
def report = {
...
Object[] resultSet1 = reportService.reportRecords(1)
...
Object[] resultSet2 = reportService.reportRecords(2)
// ^ java.lang.NullPointerException : Must specify a non-null Connection
...
}
}
以前有没有人见过这种情况,如果有,我该如何避免这种情况?
这是我的 DataSource.groovy
environments {
development {
dataSource_myds {
url = "jdbc:oracle:thin:@..."
driverClassName = "oracle.jdbc.driver.OracleDriver"
username = "..."
password = "..."
}
}
}
【问题讨论】:
-
尝试在
sql.eachRow()方法之后调用sql.close()。 -
ReportController 中的 ReportService reportService 应该是 def reportService 可能不会影响您的用例。
-
@AnujAneja 我不认为严格类型的字段有什么问题。
-
@micha:是的,你是对的!!!
-
运行
grails clean并重新启动您的应用程序。
标签: grails service null datasource