我使用的是 Grails 2.2.1,并且我在服务中注入(inject)了一个自定义数据源,以便我可以执行一些 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 = "..."
}
}
}
最佳答案
尝试使用 resources.groovy 方式。这也将为您提供环境基础数据源的选项。
在下面给出的链接中解释得很好:
Grails 2 multiple dynamic datasources in services
谢谢
关于Grails 数据源在服务中变为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18620448/