title: Change Multitenancy sessions manually
body: <p>I need to create a multitenanacy application with ability to switch between schemas inside my java-code (not based on a user request).</p>
  
  <p>I've read articles:
  <a href="https://fizzylogic.nl/2016/01/24/make-your-spring-boot-application-multi-tenant-aware-in-2-steps/" rel="noreferrer">https://fizzylogic.nl/2016/01/24/make-your-spring-boot-application-multi-tenant-aware-in-2-steps/</a>
  <a href="http://www.greggbolinger.com/tenant-per-schema-with-spring-boot/" rel="noreferrer">http://www.greggbolinger.com/tenant-per-schema-with-spring-boot/</a>
  Solution works fine, when the schema is passed in Rest-request.</p>
  
  <p>However I need to implement the following logic:</p>
  
  <pre><code>public void compare(String originalSchema, String secondSchema){
      TenantContext.setCurrentTenant(originalSchema);
      List&lt;MyObject&gt; originalData = myRepository.findData();
  
      TenantContext.setCurrentTenant(secondSchema);
      List&lt;MyObject&gt; migratedData = myRepository.findData();
  }
  </code></pre>
  
  <p>The point is, that connection is not switched, when I manually set up TenenantContext. MultiTenantConnectionProviderImpl.getConnection is invoked only on the first call to my repository.</p>
  
  <pre><code> @Component
   public class MultiTenantConnectionProviderImpl implements  MultiTenantConnectionProvider {
  
       @Override
       public Connection getConnection(String tenantIdentifier) throws SQLException {
            final Connection connection = getAnyConnection();
            try {
                 connection.createStatement().execute( "ALTER SESSION SET CURRENT_SCHEMA = " + tenantIdentifier );
            }
            catch ( SQLException e ) {
                throw new HibernateException(
        "Could not alter JDBC connection to specified schema [" + tenantIdentifier + "]",e);
            }
            return connection;
      }
   }
  </code></pre>
  
  <p>Is it possible to force switching sessions?</p>