Skip to content

Commit 26a3ca5

Browse files
authored
Merge pull request quarkusio#50327 from lucamolteni/50210
Application using Hibernate offline startup tries to connect to database in DEV mode on startup
2 parents f0ff3d7 + 97c50b6 commit 26a3ca5

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package io.quarkus.hibernate.orm.offline;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.hamcrest.Matchers.is;
5+
6+
import jakarta.ws.rs.GET;
7+
import jakarta.ws.rs.Path;
8+
import jakarta.ws.rs.Produces;
9+
import jakarta.ws.rs.core.MediaType;
10+
11+
import org.junit.jupiter.api.Test;
12+
import org.junit.jupiter.api.extension.RegisterExtension;
13+
14+
import io.quarkus.hibernate.orm.MyEntity;
15+
import io.quarkus.test.QuarkusDevModeTest;
16+
import io.restassured.RestAssured;
17+
18+
/**
19+
* Test that an application is running
20+
* even if the database is offline when the application starts
21+
* in DEV mode
22+
*/
23+
public class StartOfflineDevModeTest {
24+
25+
@RegisterExtension
26+
static QuarkusDevModeTest runner = new QuarkusDevModeTest()
27+
.withApplicationRoot((jar) -> jar
28+
.addClass(MyEntity.class)
29+
.addClass(GreetingResource.class)
30+
.addAsResource("application-start-offline.properties", "application.properties"))
31+
.setLogRecordPredicate(record -> true);
32+
33+
@Test
34+
public void testUnitSchemaManagementStrategyIsNone() {
35+
RestAssured.when().get("/hello").then()
36+
.statusCode(200)
37+
.body(is("DB is offline but application is running"));
38+
39+
assertThat(runner.getLogRecords())
40+
.map(l -> l.getMessage())
41+
.doesNotContain("Failed to run post-boot validation");
42+
}
43+
44+
@Path("/hello")
45+
public static class GreetingResource {
46+
47+
@GET
48+
@Produces(MediaType.TEXT_PLAIN)
49+
public String hello() {
50+
return "DB is offline but application is running";
51+
}
52+
}
53+
54+
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,10 @@ public void doValidation(String puName) {
290290
String schemaManagementStrategy = hibernateOrmRuntimeConfigPersistenceUnit.database().generation().generation()
291291
.orElse(hibernateOrmRuntimeConfigPersistenceUnit.schemaManagement().strategy());
292292

293-
//if hibernate is already managing the schema we don't do this
294-
if (!"none".equals(schemaManagementStrategy)) {
293+
boolean startsOffline = hibernateOrmRuntimeConfigPersistenceUnit.database().startOffline();
294+
295+
//if hibernate is already managing the schema or if we're in offline mode we don't do this
296+
if (!"none".equals(schemaManagementStrategy) || startsOffline) {
295297
return;
296298
}
297299
new Thread(new Runnable() {

0 commit comments

Comments
 (0)