Skip to content

Commit 77d2e17

Browse files
authored
Merge pull request #43764 from yrodiere/i43703
Delegate detection of the database version to the Hibernate ORM dialect and enable checks for Hibernate Reactive
2 parents 1808caa + b1e43b2 commit 77d2e17

26 files changed

+513
-254
lines changed

docs/src/main/asciidoc/hibernate-orm.adoc

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,6 @@ Hibernate ORM may generate SQL that is invalid which would lead to runtime excep
214214
If the database cannot be reached, a warning will be logged but startup will proceed.
215215
You can optionally disable the version check if you know the database won't be reachable on startup
216216
using <<quarkus-hibernate-orm_quarkus-hibernate-orm-database-version-check-enabled,`quarkus.hibernate-orm.database.version-check.enabled=false`>>.
217-
218-
// TODO change the default to "always enabled" when we solve version detection problems
219-
// See https://github.com/quarkusio/quarkus/issues/43703
220-
// See https://github.com/quarkusio/quarkus/issues/42255
221-
The version check is disabled by default when a dialect is set explicitly,
222-
as a workaround for https://github.com/quarkusio/quarkus/issues/42255[#42255]/link:https://github.com/quarkusio/quarkus/issues/43703[#43703].
223217
====
224218

225219
[[hibernate-dialect-other-databases]]

docs/src/main/asciidoc/hibernate-reactive.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ A `Mutiny.SessionFactory` will be created based on the Quarkus `datasource` conf
122122

123123
The dialect will be selected based on the Reactive SQL client - unless you set one explicitly.
124124

125+
NOTE: For more information on dialect selection and database versions,
126+
see xref:hibernate-orm.adoc#hibernate-dialect[the corresponding section of the Hibernate ORM guide].
127+
125128
You can then happily inject your `Mutiny.SessionFactory`:
126129

127130
[source,java]

extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/config/dialect/DbVersionCheckDisabledAutomaticallyPersistenceXmlTest.java

Lines changed: 0 additions & 73 deletions
This file was deleted.

extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/config/dialect/DbVersionCheckDisabledAutomaticallyTest.java

Lines changed: 0 additions & 69 deletions
This file was deleted.

extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/config/dialect/DbVersionCheckDisabledExplicitlyTest.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@
1717
import io.quarkus.test.QuarkusUnitTest;
1818

1919
/**
20-
* Tests that the workaround for https://github.com/quarkusio/quarkus/issues/43703 /
20+
* Tests that DB version checks can be disabled explicitly.
21+
* <p>
22+
* This was originally introduced to work around problems with version checks,
23+
* such as https://github.com/quarkusio/quarkus/issues/43703 /
2124
* https://github.com/quarkusio/quarkus/issues/42255
22-
* is effective.
2325
*/
24-
// TODO remove this test when change the default to "always enabled" when we solve version detection problems
25-
// See https://github.com/quarkusio/quarkus/issues/43703
26-
// See https://github.com/quarkusio/quarkus/issues/42255
2726
public class DbVersionCheckDisabledExplicitlyTest {
2827

2928
private static final String ACTUAL_H2_VERSION = DialectVersions.Defaults.H2;

extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/config/dialect/DbVersionInvalidPersistenceXmlTest.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,8 @@ public class DbVersionInvalidPersistenceXmlTest {
2929
.doesNotStartWith(CONFIGURED_DB_VERSION);
3030
}
3131

32-
private static final String ACTUAL_H2_VERSION_REPORTED;
3332
private static final String CONFIGURED_DB_VERSION_REPORTED;
3433
static {
35-
// For some reason Hibernate ORM does not catch the actual micro version of H2 and default to 0; no big deal.
36-
ACTUAL_H2_VERSION_REPORTED = ACTUAL_H2_VERSION.replaceAll("\\.[\\d]+$", ".0");
3734
// For some reason Hibernate ORM infers a micro version of 0; no big deal.
3835
CONFIGURED_DB_VERSION_REPORTED = CONFIGURED_DB_VERSION + ".0";
3936
}
@@ -52,10 +49,10 @@ public class DbVersionInvalidPersistenceXmlTest {
5249
.hasMessageContainingAll(
5350
"Persistence unit 'templatePU' was configured to run with a database version"
5451
+ " of at least '" + CONFIGURED_DB_VERSION_REPORTED + "', but the actual version is '"
55-
+ ACTUAL_H2_VERSION_REPORTED + "'",
52+
+ ACTUAL_H2_VERSION + "'",
5653
"Consider upgrading your database",
5754
"Alternatively, rebuild your application with 'jakarta.persistence.database-product-version="
58-
+ ACTUAL_H2_VERSION_REPORTED + "'",
55+
+ ACTUAL_H2_VERSION + "'",
5956
"this may disable some features and/or impact performance negatively"));
6057

6158
@Inject

extensions/hibernate-orm/deployment/src/test/java/io/quarkus/hibernate/orm/config/dialect/DbVersionInvalidTest.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,8 @@ public class DbVersionInvalidTest {
2525
.doesNotStartWith(CONFIGURED_DB_VERSION);
2626
}
2727

28-
private static final String ACTUAL_H2_VERSION_REPORTED;
2928
private static final String CONFIGURED_DB_VERSION_REPORTED;
3029
static {
31-
// For some reason Hibernate ORM does not catch the actual micro version of H2 and default to 0; no big deal.
32-
ACTUAL_H2_VERSION_REPORTED = ACTUAL_H2_VERSION.replaceAll("\\.[\\d]+$", ".0");
3330
// For some reason Hibernate ORM infers a micro version of 0; no big deal.
3431
CONFIGURED_DB_VERSION_REPORTED = CONFIGURED_DB_VERSION + ".0";
3532
}
@@ -45,10 +42,10 @@ public class DbVersionInvalidTest {
4542
.hasMessageContainingAll(
4643
"Persistence unit '<default>' was configured to run with a database version"
4744
+ " of at least '" + CONFIGURED_DB_VERSION_REPORTED + "', but the actual version is '"
48-
+ ACTUAL_H2_VERSION_REPORTED + "'",
45+
+ ACTUAL_H2_VERSION + "'",
4946
"Consider upgrading your database",
5047
"Alternatively, rebuild your application with 'quarkus.datasource.db-version="
51-
+ ACTUAL_H2_VERSION_REPORTED + "'",
48+
+ ACTUAL_H2_VERSION + "'",
5249
"this may disable some features and/or impact performance negatively",
5350
"disable the check with 'quarkus.hibernate-orm.database.version-check.enabled=false'"));
5451

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package io.quarkus.hibernate.orm.offline;
2+
3+
import jakarta.inject.Inject;
4+
import jakarta.transaction.Transactional;
5+
6+
import org.assertj.core.api.Assertions;
7+
import org.hibernate.Session;
8+
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.api.extension.RegisterExtension;
10+
11+
import io.quarkus.hibernate.orm.MyEntity;
12+
import io.quarkus.hibernate.orm.SmokeTestUtils;
13+
import io.quarkus.test.QuarkusUnitTest;
14+
15+
/**
16+
* If nothing requires DB access on startup, we expect startup to proceed even if the database is not reachable.
17+
* <p>
18+
* There will likely be warnings being logged, but the application will start.
19+
*/
20+
public class DbOfflineOnStartupTest {
21+
22+
@RegisterExtension
23+
static QuarkusUnitTest runner = new QuarkusUnitTest()
24+
.withApplicationRoot((jar) -> jar
25+
.addClass(SmokeTestUtils.class)
26+
.addClass(MyEntity.class))
27+
.withConfigurationResource("application.properties")
28+
// Disable schema management
29+
.overrideConfigKey("quarkus.hibernate-orm.schema-management.strategy",
30+
"none")
31+
// Pick a DB URL that is offline
32+
.overrideConfigKey("quarkus.datasource.jdbc.url",
33+
"jdbc:h2:tcp://localhost:9999/~/sample");
34+
35+
@Inject
36+
Session session;
37+
38+
@Test
39+
@Transactional
40+
public void smokeTest() {
41+
// We expect startup to succeed, but any DB access would obviously fail
42+
var entity = new MyEntity("someName");
43+
Assertions.assertThatThrownBy(() -> {
44+
session.persist(entity);
45+
session.flush();
46+
})
47+
.hasMessageContaining("Connection refused");
48+
}
49+
}

extensions/hibernate-orm/deployment/src/test/resources/META-INF/some-persistence-with-h2-version-placeholder-and-explicit-dialect.xml

Lines changed: 0 additions & 30 deletions
This file was deleted.

extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/FastBootHibernatePersistenceProvider.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,12 @@ private RuntimeSettings buildRuntimeSettings(String persistenceUnitName, Recorde
238238
}
239239

240240
// Allow detection of driver/database capabilities on runtime init (was disabled during static init)
241-
runtimeSettingsBuilder.put("hibernate.boot.allow_jdbc_metadata_access", "true");
241+
runtimeSettingsBuilder.put(AvailableSettings.ALLOW_METADATA_ON_BOOT, "true");
242+
// Remove database version information, if any;
243+
// it was necessary during static init to force creation of a dialect,
244+
// but now the dialect is there, and we'll reuse it.
245+
// Keeping this information would prevent us from getting the actual information from the database on start.
246+
runtimeSettingsBuilder.put(AvailableSettings.JAKARTA_HBM2DDL_DB_VERSION, null);
242247

243248
if (!persistenceUnitConfig.unsupportedProperties().isEmpty()) {
244249
log.warnf("Persistence-unit [%s] sets unsupported properties."

0 commit comments

Comments
 (0)