Search code examples
javaspringh2embedded-database

Does Spring embedded database support different SQL dialects?


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?


Solution

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