Grails 数据源在服务中变为空

标签 grails service null datasource

我使用的是 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/

相关文章:

date - View 中混合了Grails groovy代码

grails - 在子文件夹中的 Controller 中使用Grails Form标签

android - 如果我尝试启动服务,为什么会收到 InstantiationException?

objective-c - 子类化UIAlertView

c# - SqlDataReader 对象的内联空检查

timer - 电晕 : timer. 取消()返回 "Attempt to index a nil value"

grails - 在Grails中,如何从 Controller 返回到调用当前 View ?

grails - Grails上的MVC架构

c# - 从服务中获取图像

javascript - 将数组从服务传递到 Controller