Grails数据源在Service中变为null [英] Grails datasource becoming null in Service

查看:114
本文介绍了Grails数据源在Service中变为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Grails 2.2.1,我有一个自定义的dataSource注入服务,以便我可以执行一些SQL查询。



第一次执行时,是一个dataSource,但在每次后续调用时,对dataSource的引用都已变为null。

  class ReportService {
def dataSource_myds

Object [] reportRecords(int a){
String query =SELECT ...

Object [] resultSet;

Sql sql = new Sql(dataSource_myds)
// ^这里NullPointerException被抛出
//但它总是在第一次执行时工作

sql.eachRow(query,[a]){
...
resultSet + = result
}
返回结果集
}
}

class ReportController {
ReportService reportService

def report = {
...
Object [] resultSet1 = reportService.reportRecords(1)
...
Object [] resultSet2 = reportService.reportRecords(2)
// ^ java.lang.NullPointerException:必须指定一个非空的Connection
...
}
}

有没有人见过这个,如果是这样的话,我怎么能这是我的DataSource.groovy

  environments 




development {
dataSource_myds {
url =jdbc:oracle:thin:@ ...
driverClassName =oracle.jdbc.driver.OracleD河
用户名=...
密码=...
}
}
}

解决方案

James Kleeh的评论为我解决了它 - grails clean 然后重新启动应用程序。


I am using Grails 2.2.1, and I have a custom dataSource injected into the service so that I can execute some SQL queries.

Upon first execution, there is a dataSource, but on each subsequent call, the reference to the dataSource has become null.

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
     ...
  }
}

Has anyone ever seen this before, and if so, how can I avoid this?

Here is my DataSource.groovy

environments {
  development {
    dataSource_myds {
      url = "jdbc:oracle:thin:@..."
      driverClassName = "oracle.jdbc.driver.OracleDriver"
      username = "..."
      password = "..."
    }
  }
}

解决方案

James Kleeh's comment solved it for me - grails clean and then restart the app.

这篇关于Grails数据源在Service中变为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆