Skip to content

Commit

Permalink
Merge pull request #22502 from gsmet/fix-tenant-resolver-resolution
Browse files Browse the repository at this point in the history
Properly determine tenant resolver in HibernateMultiTenantConnectionProvider
  • Loading branch information
gsmet authored Dec 23, 2021
2 parents 5a1bdd9 + 7aab5f9 commit 58cf76b
Showing 1 changed file with 7 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import javax.enterprise.context.RequestScoped;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.inject.Default;

import org.hibernate.engine.jdbc.connections.spi.AbstractMultiTenantConnectionProvider;
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
Expand All @@ -17,7 +16,6 @@
import io.quarkus.arc.InjectableInstance;
import io.quarkus.arc.InstanceHandle;
import io.quarkus.arc.ManagedContext;
import io.quarkus.hibernate.orm.PersistenceUnit.PersistenceUnitLiteral;
import io.quarkus.hibernate.orm.runtime.PersistenceUnitUtil;

/**
Expand Down Expand Up @@ -107,20 +105,18 @@ private static ConnectionProvider resolveConnectionProvider(String persistenceUn
* @return Current tenant resolver.
*/
private static InstanceHandle<TenantResolver> tenantResolver(String persistenceUnitName) {
InstanceHandle<TenantResolver> resolverInstance;
if (PersistenceUnitUtil.isDefaultPersistenceUnit(persistenceUnitName)) {
resolverInstance = Arc.container().instance(TenantResolver.class, Default.Literal.INSTANCE);
} else {
resolverInstance = Arc.container().instance(TenantResolver.class,
new PersistenceUnitLiteral(persistenceUnitName));
}
if (!resolverInstance.isAvailable()) {
InjectableInstance<TenantResolver> instance = PersistenceUnitUtil
.legacySingleExtensionInstanceForPersistenceUnit(
TenantResolver.class, persistenceUnitName);

if (instance.isUnsatisfied()) {
throw new IllegalStateException(String.format(Locale.ROOT,
"No instance of %1$s was found for persistence unit %2$s. "
+ "You need to create an implementation for this interface to allow resolving the current tenant identifier.",
TenantResolver.class.getSimpleName(), persistenceUnitName));
}
return resolverInstance;

return instance.getHandle();
}

}

0 comments on commit 58cf76b

Please sign in to comment.