【问题标题】:Grails 3.3 execute H2 script commandGrails 3.3 执行H2脚本命令
【发布时间】:2017-08-04 14:09:07
【问题描述】:

我正在使用基于 H2 文件的数据库运行一个小型、简单的 Grails 3.3.0 应用程序。出于简单的备份原因,我想使用 H2 特定的SCRIPT 命令将当前数据库状态转储到文件中:

SCRIPT TO /path/to/backup/dir/tempoDb.sql;

目前我正在尝试像这样执行本机 SQL 命令。

User.withSession { session ->

   NativeSQLQuerySpecification nativeSQLQuerySpecification = new NativeSQLQuerySpecification("SCRIPT TO /path/to/backup/dir/tempoDb.sql;", null, null)

   session.executeNativeUpdate(nativeSQLQuerySpecification, new QueryParameters())
}

但这不起作用。

【问题讨论】:

  • 添加为答案

标签: grails grails-orm h2 grails-3.3


【解决方案1】:

您可以自动连接dataSource 并尝试使用从数据源获得的连接运行您的 sql 查询,而无需通过 Hibernate。 dataSource bean 在 Grails Spring 上下文中注册,它是 javax.sql.DataSource 的一个实例。

这是一个将当前 H2 数据库备份到文件系统的 Grails 服务示例。

@ReadOnly
class BackupService {

    DataSource dataSource

    def backup() {

        def sql = "SCRIPT DROP TO '${System.properties['java.io.tmpdir']}/backup.sql'"

        Statement statement = dataSource.connection.createStatement()
        boolean result = statement.execute(sql)
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-07
    • 2016-12-07
    • 2017-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多