如何在Groovy中对SQL查询使用缓存?

时间:2019-04-30 07:31:39

标签: sql caching grails groovy

我正在使用以下代码中所示的现有DataSource创建全局groovy.sql.Sql连接:

def checkConnection() {
    if (sql == null || sql.getConnection() == null) {
        sql = new Sql(dataSource)
    }
}

在这里,我正在调用多种方法来使用此全局SQL连接从数据库获取不同的不同数据。

注意

当我不使用sql.cacheStatements = true时,我的代码工作正常。但是,当我使用sql.cacheStatements = true时,它每次都会关闭我的连接。

首先,对于每种方法,我都调用checkConnection()方法。

def getCount() {
    checkConnection()
    sql.cacheStatements = true
    rows = sql.rows("select count(columnName) from tableName where date>= someValue and age>= someValue")
    return rows
}

问题: 如何使用groovy的groovy.sql.Sql连接对语句使用缓存概念。

0 个答案:

没有答案