question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

HibernateEntityManagerFactoryIntegrator evaluates wrong DBMS name when using Hibernate with MariaDB103Dialect and H2

See original GitHub issue

Description

DBMS name determination fails for H2 in MariaDB-Mode. Logged output is

WARNING [com.bla.per.spi.EntityManagerFactoryIntegrator] (main) Could not register the function 'json_get' because there is neither an implementation for the dbms 'mysql' nor a default implementation!

Expected behavior

DBMS name is evaluated as mysql8

Actual behavior

DBMS name is evaluated as mysql -> database functions newer than mysql5/mariadb5 are not usable

Steps to reproduce

  1. use jdbc connection: url: jdbc:h2:mem:maindb;MODE=MySQL;DATABASE_TO_LOWER=TRUE;;init=SET collation german;DB_CLOSE_DELAY=-1
  2. use hibernate dialect: org.hibernate.dialect.MariaDB103Dialect
  3. use com.blazebit.persistence.CriteriaBuilderFactory

Logged output is

WARNING [com.bla.per.spi.EntityManagerFactoryIntegrator] (main) Could not register the function 'json_get' because there is neither an implementation for the dbms 'mysql' nor a default implementation!

Suspected defect

https://github.com/Blazebit/blaze-persistence/blob/e969fbfcc10b4d09bf4ffafdaeb025d60e3fefb8/integration/hibernate-base/src/main/java/com/blazebit/persistence/integration/hibernate/base/function/AbstractHibernateEntityManagerFactoryIntegrator.java#L96-L118

Explanation:

  1. org.hibernate.dialect.MariaDB103Dialect is a MySQLDialect
  2. H2 runs in MySQL/MariaDB - compatibility mode (see http://h2database.com/html/features.html#compatibility)
  3. calling connection.getMetaData().getDatabaseMajorVersion() returns 1 here, this seems to be the version of H2

I think it is wrong to create a connection here, as all needed information is already present in the form of the configured hibernate dialect.

Proposed solution:

if (dialect instanceof org.hibernate.dialect.MySQL8Dialect) {
    return "mysql8";
} else if (dialect instanceof org.hibernate.dialect.MariaDB10Dialect) {
    return "mysql8";
} else if (dialect instanceof MySQLDialect) {
    return "mysql";
} else if (dialect instanceof DB2Dialect) {
    return "db2";
} else if (dialect instanceof PostgreSQL81Dialect) {
    return "postgresql";
} else [...]

Environment

Version: 1.5.1 JPA-Provider: Hibernate 5.4.28 DBMS: H2 in SQL Mode (which is to be used for MySQL and MariaDB) Application Server: Quarkus

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
beikovcommented, Mar 30, 2021

Hi @mickroll,

thanks for the report. I’ll try to adapt the code to try to rely more on the dialect, but since the MySQL8 Dialect was only added in a very late Hibernate version, the connection acquisition is sometimes necessary. It would be a bummer not to support detecting MySQL8 for older Hibernate versions or when people use a custom dialect.

Apart from that, you won’t be able to use the json_get function with H2 as it, as far as I know, does not support the underlying json access functions even in MySQL compatible mode. So if you want to use the json functions, you will have to use MySQL or MariaDB.

0reactions
mickrollcommented, Apr 6, 2021

Closing the entitymanager does not close the underlying connection.

It should. Or do you have a case that shows this is a problem?

From what i can see by debugging our application, hibernate on H2 does not close the logical connection, because the underlying physical connection is not present. Have to check as soon as we have a real MariaDB to connect to.

Nevermind, that’s completely out of scope of this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Blazebit - Bountysource
There are use cases where entity views contain a list of strings as a comma ... HibernateEntityManagerFactoryIntegrator evaluates wrong DBMS name when using...
Read more >
Hibernate uses wrong column name despite all anotations
Table of my database is created using following sql : CREATE TABLE students ( "ID" UUID, "First_Name" VARCHAR(50), "Last_Name" ...
Read more >
Common Hibernate Exceptions - Baeldung
Many conditions can cause exceptions to be thrown while using Hibernate. These can be mapping errors, infrastructure problems, SQL errors, ...
Read more >
JDBC Connection and Dialect Configuration in Hibernate
You can use the following configuration parameters to configure the JDBC connection in Hibernate: javax.persistence.jdbc.driver – The fully qualified class name ...
Read more >
Hibernate ORM 5.2.18.Final User Guide - Red Hat on GitHub
Using Hibernate's built-in (and unsupported) pooling; 7.7. ... in mapping the Contact table, all attributes except for name would be basic types.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found