Spring嵌入式数据库支持不同的SQL方言吗?

发布于 2025-01-03 07:45:14 字数 427 浏览 3 评论 0原文

H2 具有一系列兼容模式,适用于各种其他数据库,例如 MS SQL Server、支持不同 SQL 方言的 MySQL、Oracle 等。但是,在设置 Spring中的嵌入式数据库我没有找到任何相应的设置。例如,如果我在生产中使用 Oracle,在测试期间使用 H2,这是否意味着我必须使用没有任何方言特定功能的“纯”SQL?我是不是忽略了什么?

H2 has a range of compatibility modes for various other databases such as MS SQL Server, MySQL, Oracle, etc that support different SQL dialects. However, when setting up an embedded database in Spring I do not find any corresponding setting. Does this imply that I have to use "plain" SQL without any dialect specific features if I for example use Oracle in production and H2 during test? Have I overlooked something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吾家有女初长成 2025-01-10 07:45:14

H2数据库哪个版本?根据文档,您可以通过 SQL 语句设置兼容模式(http://www.h2database.com/html/features .html#compatibility

SET MODE PostgreSQL

只需将此语句添加到 Spring jdbc 嵌入式数据库加载的第一个 sql 脚本文件中

which version of H2 database? per the documentation, you can set compatible mode by SQL statement (http://www.h2database.com/html/features.html#compatibility)

SET MODE PostgreSQL

just add this statement into your first sql script file loaded by Spring jdbc embedded-database

不醒的梦 2025-01-10 07:45:14

根据 H2 文档,Oracle 兼容模式非常有限。

例如,您不能使用 PL/SQL 过程。

如果使用Spring的EmbeddedDatabase,则无法按原样设置兼容模式;你必须实现你自己的 EmbeddedDatabaseConfigurer 并通过 JDBC URL 指定兼容模式(见下文)。

而且,要使用 H2 和 Spring 的兼容模式,您只需使用 DataSource 以经典方式在 JDBC URL 中设置模式(因此它与 Spring 无关):

jdbc:h2:~/test;MODE=Oracle

如果您使用 Hibernate,则必须指定 Oracle 方言而不是 H2 方言。

According to the H2 doc, the Oracle compatibility mode is quite limited.

For instance, you can not use PL/SQL procedures.

If you use Spring's EmbeddedDatabase, you cannot set the compatibility mode as-is; you have to implement you own EmbeddedDatabaseConfigurer and specify the compatibility mode through the JDBC URL (see below).

But also, to use the compatibility mode with H2 and Spring, you just have to set the mode in your JDBC URL (so it is not Spring related) in a classic way, using a DataSource:

jdbc:h2:~/test;MODE=Oracle

And if you use Hibernate, you have to specify the Oracle dialect instead of the H2 one.

枯叶蝶 2025-01-10 07:45:14

您有 2 个选择:

  1. 使用 spring 启动 H2 数据库,如下所示(检查 setName() 以了解如何将 H2 特定 URL 参数传递给 spring builder):

Spring 代码生成 URL,如下所示:

String.format("jdbc:h2: mem:%s;DB_CLOSE_DELAY=-1",databaseName)

因此,在 setName() 中,您可以在 URL 中使用所有 H2 特定参数。

private DataSource dataSource() {
    EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
    EmbeddedDatabase db = builder
        .setType(EmbeddedDatabaseType.H2)
        .setName("testdb;DATABASE_TO_UPPER=false;MODE=Oracle")
        .addScript("schema.sql")
        .addScript("data.sql")
        .build();
    return db;
}
  1. 直接配置数据库URL,例如:

    org.h2.jdbcx.JdbcDataSource dataSource = new org.h2.jdbcx.JdbcDataSource();
    dataSource.setURL("jdbc:h2:testdb;MODE=MySQL;DATABASE_TO_UPPER=false;INIT=来自'src/test/resources/schema.sql'的runscript\;来自'src/test/resources/data.sql'的runscript" );

主要区别在于 (2) 为每个数据库连接创建执行在 INIT 中定义的脚本,而不是每个数据库创建执行一次!这会导致各种问题,例如 INSERT 由于重复键而失败等。

You have 2 options:

  1. use spring to start the H2 database as follows (check setName() to see how to pass H2 specific URL parameters to spring builder):

Spring code generates the URL as follows:

String.format("jdbc:h2:mem:%s;DB_CLOSE_DELAY=-1", databaseName)

So, in setName() you can all any H2 specific parameter in the URL.

private DataSource dataSource() {
    EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
    EmbeddedDatabase db = builder
        .setType(EmbeddedDatabaseType.H2)
        .setName("testdb;DATABASE_TO_UPPER=false;MODE=Oracle")
        .addScript("schema.sql")
        .addScript("data.sql")
        .build();
    return db;
}
  1. configure directly the DB URL, sth like:

    org.h2.jdbcx.JdbcDataSource dataSource = new org.h2.jdbcx.JdbcDataSource();
    dataSource.setURL("jdbc:h2:testdb;MODE=MySQL;DATABASE_TO_UPPER=false;INIT=runscript from 'src/test/resources/schema.sql'\;runscript from 'src/test/resources/data.sql'");

The main different is that (2) is executing scripts defined at INIT for every database connection creation and not once per DB creation! This causes various issues, like INSERTs failing due to duplicate keys etc..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文