Repository: quarkusio/quarkus
Issue: No instance of TenantResolver was found for persistence unit <default>

### Describe the bug

After updating from quarkus 2.2.0 to version 2.3.1.Final (I tried also with version 2.4.1.Final) I added the annotation @PersistenceUnitExtension as mentioned in the migration guide here:
[Migration Guide 2.3 Quarkus](https://github.com/quarkusio/quarkus/wiki/Migration-Guide-2.3)

I am dynamically retrieving the schema. No named persistence Unit.

Quarkus is working with the new annotation and also the schema is correctly retrieved after quarkus has booted up, but **at startup** I get the following error message:

`HHH000342: Could not obtain connection to query metadata: java.lang.IllegalStateException: No instance of TenantResolver was found for persistence unit <default>. You need to create an implementation for this interface to allow resolving the current tenant identifier.`

But I get this error message only at startup. To me it looks like it's a startup order issue.

Can somebody approve this?

My Code:

```
import io.quarkus.hibernate.orm.PersistenceUnitExtension;
import io.quarkus.hibernate.orm.runtime.tenant.TenantResolver;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;

@RequestScoped
@PersistenceUnitExtension
@Slf4j
public class CustomTenantResolver implements TenantResolver {

    @PostConstruct
    private void postConstruct() {
        log.info( "CustomTenantResolver constructed!" );
    }

    @Override
    public String getDefaultTenantId() {
        log.info( "getDefaultTenantId" );
        return "public";
    }

    @Override
    public String resolveTenantId() {
        log.info( "resolveTenantId" );
        String schema = TenantContext.getCurrentTenant();

        if ( schema == null ) {
            return "public";
        }
        log.info( "current schema {}", schema );
        return schema;
    }
}

```

Only if needed my application.properties
```
# datasource settings
quarkus.http.port=8080
quarkus.hibernate-orm.multitenant=SCHEMA
quarkus.datasource.jdbc.url=jdbc:postgresql://localhost:5432/db
quarkus.datasource.db-kind=postgresql
quarkus.datasource.username=useraa
quarkus.datasource.password=myPassword
quarkus.datasource.jdbc.initial-size=2
quarkus.datasource.jdbc.min-size=2
quarkus.datasource.jdbc.max-size=8
quarkus.transaction-manager.default-transaction-timeout=300S
quarkus.flyway.migrate-at-start=false
quarkus.flyway.schemas=public
quarkus.package.type=uber-jar

# Logging
quarkus.log.level=INFO
quarkus.log.min-level=INFO
#%dev.quarkus.log.level=DEBUG
#%dev.quarkus.log.min-level=DEBUG

# Rest-api settings
portal-api/mp-rest/url=http://localhost:8180/
portal-api/mp-rest/scope=javax.inject.Singleton

org.eclipse.microprofile.rest.client.propagateHeaders=Authorization


# Rest-api settings
# OIDC Configuration
quarkus.oidc.auth-server-url=http://localhost:8280/auth/realms/software
quarkus.oidc.token.issuer=https://mywebsite.at/auth/realms/turniersoftware
quarkus.oidc.client-id=quarkus-pea
quarkus.oidc.credentials.secret=mySecretCode
# Enable Policy Enforcement
quarkus.keycloak.policy-enforcer.enable=false
# Dev variables
#quarkus.log.level=INFO
#quarkus.log.min-level=DEBUG
#quarkus.log.category."io.undertow".level=DEBUG
```

**//SOLUTION found by nmvr2600:**

Add `@default` annotation to your class from the package: `import javax.enterprise.inject.Default;`
```
import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Default;

@Default
@RequestScoped
@PersistenceUnitExtension
@Slf4j
public class CustomTenantResolver implements TenantResolver {
...
```

### Expected behavior

No error message on startup.

### Actual behavior

I get the no instance of tenantResolver was found exception.

### How to Reproduce?

_No response_

### Output of `uname -a` or `ver`

_No response_

### Output of `java -version`

11

### GraalVM version (if different from Java)

_No response_

### Quarkus version or git rev

2.4.1.Final

### Build tool (ie. output of `mvnw --version` or `gradlew --version`)

_No response_

### Additional information

_No response_

Changed files:
extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/tenant/HibernateMultiTenantConnectionProvider.java

diff --git a/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/tenant/HibernateMultiTenantConnectionProvider.java b/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/tenant/HibernateMultiTenantConnectionProvider.java
index c5fef6c9cc1..1455be9ea39 100644
--- a/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/tenant/HibernateMultiTenantConnectionProvider.java
+++ b/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/tenant/HibernateMultiTenantConnectionProvider.java
@@ -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;
@@ -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;
 
 /**
@@ -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();
     }
 
 }