Skip to content

Commit

Permalink
Disable autocommit at the db connection level and activate the requir…
Browse files Browse the repository at this point in the history
…ed datasource programatically
  • Loading branch information
carlesarnal committed Nov 14, 2024
1 parent 02ecd4c commit 5fc4a6c
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package io.apicurio.registry.services;

import io.apicurio.registry.storage.impl.sql.RegistryDatabaseKind;
import io.smallrye.config.ConfigSourceInterceptor;
import io.smallrye.config.ConfigSourceInterceptorContext;
import io.smallrye.config.ConfigValue;
import jakarta.annotation.Priority;

@Priority(100)
public class DatabaseConfigInterceptor implements ConfigSourceInterceptor {

@Override
public ConfigValue getValue(ConfigSourceInterceptorContext context, String name) {
ConfigValue storageKind = context.proceed("apicurio.storage.sql.kind");
RegistryDatabaseKind databaseKind = RegistryDatabaseKind.valueOf(storageKind.getValue());

switch (name) {
case "quarkus.datasource.postgresql.active" -> {
if (databaseKind.equals(RegistryDatabaseKind.postgresql)) {
return ConfigValue.builder().withName(name).withValue("true").build();
} else {
return ConfigValue.builder().withName(name).withValue("false").build();
}
}
case "quarkus.datasource.mssql.active" -> {
if (databaseKind.equals(RegistryDatabaseKind.mssql)) {
return ConfigValue.builder().withName(name).withValue("true").build();
} else {
return ConfigValue.builder().withName(name).withValue("false").build();
}
}
case "quarkus.datasource.mysql.active" -> {
if (databaseKind.equals(RegistryDatabaseKind.mysql)) {
return ConfigValue.builder().withName(name).withValue("true").build();
} else {
return ConfigValue.builder().withName(name).withValue("false").build();
}
}
case "quarkus.datasource.h2.active" -> {
if (databaseKind.equals(RegistryDatabaseKind.h2)) {
return ConfigValue.builder().withName(name).withValue("true").build();
} else {
return ConfigValue.builder().withName(name).withValue("false").build();
}
}
}

return context.proceed(name);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.apicurio.registry.storage.impl.sql.jdb.HandleImpl;
import org.slf4j.Logger;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -34,7 +35,10 @@ public <R, X extends Exception> R withHandle(HandleCallback<R, X> callback) thro
try {
// Create a new handle if necessary. Increment the "level" if a handle already exists.
if (state.handle == null) {
state.handle = new HandleImpl(dataSource.getConnection());
Connection connection = dataSource.getConnection();
// We must disable autocommit since we're managing the transactions ourselves.
connection.setAutoCommit(false);
state.handle = new HandleImpl(connection);
state.level = 0;
} else {
state.level++;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.apicurio.registry.services.DatabaseConfigInterceptor
2 changes: 1 addition & 1 deletion app/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ quarkus.native.additional-build-args=--initialize-at-run-time=org.apache.kafka.c
--allow-incomplete-classpath

# Package
quarkus.package.type=legacy-jar
quarkus.package.jar.type=legacy-jar
quarkus.index-dependency.jaxrs.group-id=jakarta.ws.rs
quarkus.index-dependency.jaxrs.artifact-id=jakarta.ws.rs-api

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class MssqlTestProfile implements QuarkusTestProfile {

@Override
public Map<String, String> getConfigOverrides() {
return Collections.singletonMap("apicurio.storage.sql.kind", "mssql");
return Map.of("apicurio.storage.sql.kind", "mssql");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class MysqlTestProfile implements QuarkusTestProfile {

@Override
public Map<String, String> getConfigOverrides() {
return Collections.singletonMap("apicurio.storage.sql.kind", "mysql");
return Map.of("apicurio.storage.sql.kind", "mysql");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class PostgresqlTestProfile implements QuarkusTestProfile {

@Override
public Map<String, String> getConfigOverrides() {
return Collections.singletonMap("apicurio.storage.sql.kind", "postgresql");
return Map.of("apicurio.storage.sql.kind", "postgresql");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
quarkus.package.main-class=ConfluentExport

quarkus.log.level=WARN
quarkus.package.type=uber-jar
quarkus.package.jar.type=legacy-jar

0 comments on commit 5fc4a6c

Please sign in to comment.