title: Specify hibernate.multi_tenant_connection_provider without using reflection
body: <p>Currently I am specifying the hibernate multi tenant connection provider and resolver in a properties file using this.</p>
  <pre><code>hibernate.multi_tenant_connection_provider: com.app.MapMultiTenantConnectionProvider
  hibernate.tenant_identifier_resolver: com.app.MultiTenantCurrentTenantIdentifierResolver
  </code></pre>
  <p>Hibernate is using reflection to load these classes. The problem is that I need these classes to have access to certain variables. E.g. The DropWizard config file and users organisation to know what the database URL is and the tenant id. Currently I'm having to make variables static so that the provider has access to it.</p>
  <p>The tutorials like <a href="https://www.baeldung.com/hibernate-5-multitenancy" rel="nofollow noreferrer">this</a> have all the required info specified in the properties file while I need mine to be dynamic depending on the currently connected user.</p>
  <p>Here is an example of the kind of thing I'm having to do with static variables. Overall it makes the code quite messy.</p>
  <pre><code>public class MapMultiTenantConnectionProvider extends AbstractMultiTenantConnectionProvider {
  
      private static Logger log = LoggerFactory.getLogger(MapMultiTenantConnectionProvider.class);
  
      private Map&lt;String, ConnectionProvider&gt; connectionProviderMap = new HashMap&lt;&gt;();
  
      public MapMultiTenantConnectionProvider() throws IOException {
      }
  
      @Override
      protected ConnectionProvider getAnyConnectionProvider() {
          return getConnectionProviderForTenant(&quot;chorus&quot;);
      }
  
      @Override
      protected ConnectionProvider selectConnectionProvider(final String tenantIdentifier) {
  
          return getConnectionProviderForTenant(tenantIdentifier);
      }
  
      private ConnectionProvider getConnectionProviderForTenant(final String tenantId) {
  
          final ConnectionProvider connectionProvider;
  
          if (!connectionProviderMap.containsKey(tenantId)) {
  
              // Access a static variable here that contains the database URL, username, etc
              final MyConfig config = MyApp.CONFIGURATION;
  
              final Properties properties = new Properties();
              properties.setProperty(&quot;hibernate.connection.url&quot;, config.connectionUrl);
              properties.setProperty(&quot;hibernate.connection.username&quot;, config.username);
              properties.setProperty(&quot;hibernate.connection.password&quot;, config.password);
              properties.setProperty(&quot;hibernate.dialect&quot;, config.databaseConfig.getHibernateDialect());
              
              final DriverManagerConnectionProviderImpl newConnectionProvider = new DriverManagerConnectionProviderImpl();
  
              newConnectionProvider.configure(properties);
  
              this.connectionProviderMap.put(tenantId, newConnectionProvider);
  
              connectionProvider = newConnectionProvider;
          } else {
              connectionProvider = connectionProviderMap.get(tenantId);
          }
          return connectionProvider;
      }
  }
  </code></pre>
  <p>I would like to create an instance and pass in the configuration (or anything else needed) like this.</p>
  <pre><code>new MapMultiTenantConnectionProvider(configuration)
  </code></pre>
  <p>Is it possible to specify the provider by creating an instance of it instead of defining it in a properties file?</p>