Stefan Helmerichs Stefan Helmerichs - 1 year ago 74
MySQL Question

Trouble with Integration Testing MySQL Syntax on an H2 Database in Spring

I am working on a project which uses different MySQL schemas for customer management, e.G. every customer has his own database schema, which is created with a skeleton template.

For compatibility issues, I have to create new MySQL schemas from Java, which was before done in PHP. I created a small service class, which works perfectly on a MySQL database by repeatedly using

, however this does not work with H2db since H2 does not support the
-part when creating tables. I then created a MySQL and modified the file to be easily processed with Java (
, which is fed into
). However, this dump also fails on a H2db because of Statements like
. Sadly, these are important to have, since we often have special chars which need to be encoded correctly, so simply removing said statements from the sql file is not recommended.

I initialize the DataSource like this:

public DataSource dataSource(ResourceLoader resourceLoader) {
return new EmbeddedDatabaseBuilder()

So, my configuration possibilities are scarce. We use
to build our application, so the suggested
-plugin won't help either - at least I have not found out ow to use it with
, if that is possible at all.

So my problem here is, that I'd need to check if the Service actually persists the data correctly, but I cannot do it in-memory since H2 does not support the syntax, and I am not allowed to use the "real" MySQL database, since all our tests must be detached from "real" connections and shall only use in-memory stores for databases, if necessary.

Answer Source

I ended up using as an embedded db, since we use MariaDB anyway, so this will definitely eliminate all dialect issues. Since this effectively works the same way as an embedded MongoDB, the extra cost for testing is acceptable.

A caveat though: this NEEDS a 64bit OS to work.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download