-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fail on startup when inactive datasources are injected
By reimplementing Datasource eager startup through Arc's `startup` feature, and active/inactive through Arc's `checkActive` feature.
- Loading branch information
Showing
22 changed files
with
308 additions
and
201 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
...roal/deployment/src/main/java/io/quarkus/agroal/deployment/AgroalDataSourceBuildUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package io.quarkus.agroal.deployment; | ||
|
||
import jakarta.enterprise.inject.Default; | ||
|
||
import org.jboss.jandex.AnnotationInstance; | ||
|
||
import io.quarkus.agroal.DataSource; | ||
import io.quarkus.datasource.common.runtime.DataSourceUtil; | ||
|
||
public final class AgroalDataSourceBuildUtil { | ||
private AgroalDataSourceBuildUtil() { | ||
} | ||
|
||
public static AnnotationInstance qualifier(String dataSourceName) { | ||
if (dataSourceName == null || DataSourceUtil.isDefault(dataSourceName)) { | ||
return AnnotationInstance.builder(Default.class).build(); | ||
} else { | ||
return AnnotationInstance.builder(DataSource.class).value(dataSourceName).build(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...java/io/quarkus/agroal/test/ConfigActiveFalseDefaultDatasourceStaticallyInjectedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package io.quarkus.agroal.test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.sql.SQLException; | ||
|
||
import javax.sql.DataSource; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.inject.Inject; | ||
|
||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.arc.InactiveBeanException; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class ConfigActiveFalseDefaultDatasourceStaticallyInjectedTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.overrideConfigKey("quarkus.datasource.active", "false") | ||
.assertException(e -> assertThat(e).isInstanceOf(InactiveBeanException.class) | ||
.hasMessageContainingAll("Datasource '<default>' was deactivated through configuration properties.", | ||
"To solve this, avoid accessing this datasource at runtime, for instance by deactivating consumers (persistence units, ...).", | ||
"Alternatively, activate the datasource by setting configuration property 'quarkus.datasource.active'" | ||
+ " to 'true' and configure datasource '<default>'", | ||
"Refer to https://quarkus.io/guides/datasource for guidance.", | ||
"This bean is injected into", | ||
MyBean.class.getName() + "#ds")); | ||
|
||
@Inject | ||
MyBean myBean; | ||
|
||
@Test | ||
public void test() { | ||
Assertions.fail("Startup should have failed"); | ||
} | ||
|
||
@ApplicationScoped | ||
public static class MyBean { | ||
@Inject | ||
DataSource ds; | ||
|
||
public void useDatasource() throws SQLException { | ||
ds.getConnection(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...t/java/io/quarkus/agroal/test/ConfigActiveFalseNamedDatasourceStaticallyInjectedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package io.quarkus.agroal.test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.sql.SQLException; | ||
|
||
import javax.sql.DataSource; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.inject.Inject; | ||
|
||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.arc.InactiveBeanException; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class ConfigActiveFalseNamedDatasourceStaticallyInjectedTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.overrideConfigKey("quarkus.datasource.users.active", "false") | ||
// We need at least one build-time property for the datasource, | ||
// otherwise it's considered unconfigured at build time... | ||
.overrideConfigKey("quarkus.datasource.users.db-kind", "h2") | ||
.assertException(e -> assertThat(e).isInstanceOf(InactiveBeanException.class) | ||
.hasMessageContainingAll("Datasource 'users' was deactivated through configuration properties.", | ||
"To solve this, avoid accessing this datasource at runtime, for instance by deactivating consumers (persistence units, ...).", | ||
"Alternatively, activate the datasource by setting configuration property 'quarkus.datasource.active'" | ||
+ " to 'true' and configure datasource 'users'", | ||
"Refer to https://quarkus.io/guides/datasource for guidance.", | ||
"This bean is injected into", | ||
MyBean.class.getName() + "#ds")); | ||
|
||
@Inject | ||
MyBean myBean; | ||
|
||
@Test | ||
public void test() { | ||
Assertions.fail("Startup should have failed"); | ||
} | ||
|
||
@ApplicationScoped | ||
public static class MyBean { | ||
@Inject | ||
@io.quarkus.agroal.DataSource("users") | ||
DataSource ds; | ||
|
||
public void useDatasource() throws SQLException { | ||
ds.getConnection(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.