/** {@inheritDoc} */
  public ConnectionProvider initiateService(Map configurationValues, ServicesRegistry registry) {
    final ClassLoaderService classLoaderService = registry.getService(ClassLoaderService.class);

    ConnectionProvider connectionProvider = null;
    String providerClassName = (String) getConfiguredConnectionProviderName(configurationValues);
    if (providerClassName != null) {
      connectionProvider =
          instantiateExplicitConnectionProvider(providerClassName, classLoaderService);
    } else if (configurationValues.get(Environment.DATASOURCE) != null) {
      connectionProvider = new DatasourceConnectionProviderImpl();
    }

    if (connectionProvider == null) {
      if (c3p0ConfigDefined(configurationValues) && c3p0ProviderPresent(classLoaderService)) {
        connectionProvider =
            instantiateExplicitConnectionProvider(C3P0_PROVIDER_CLASS_NAME, classLoaderService);
      }
    }

    if (connectionProvider == null) {
      if (proxoolConfigDefined(configurationValues) && proxoolProviderPresent(classLoaderService)) {
        connectionProvider =
            instantiateExplicitConnectionProvider(PROXOOL_PROVIDER_CLASS_NAME, classLoaderService);
      }
    }

    if (connectionProvider == null) {
      if (configurationValues.get(Environment.URL) != null) {
        connectionProvider = new DriverManagerConnectionProviderImpl();
      }
    }

    if (connectionProvider == null) {
      log.warn(
          "No appropriate connection provider encountered, assuming application will be supplying connections");
      connectionProvider = new UserSuppliedConnectionProviderImpl();
    }

    final Map injectionData = (Map) configurationValues.get(INJECTION_DATA);
    if (injectionData != null && injectionData.size() > 0) {
      final ConnectionProvider theConnectionProvider = connectionProvider;
      new BeanInfoHelper(connectionProvider.getClass())
          .applyToBeanInfo(
              connectionProvider,
              new BeanInfoHelper.BeanInfoDelegate() {
                public void processBeanInfo(BeanInfo beanInfo) throws Exception {
                  PropertyDescriptor[] descritors = beanInfo.getPropertyDescriptors();
                  for (int i = 0, size = descritors.length; i < size; i++) {
                    String propertyName = descritors[i].getName();
                    if (injectionData.containsKey(propertyName)) {
                      Method method = descritors[i].getWriteMethod();
                      method.invoke(theConnectionProvider, injectionData.get(propertyName));
                    }
                  }
                }
              });
    }

    return connectionProvider;
  }
 public void releaseAnyConnection(Connection connection) throws SQLException {
   try {
     connection.createStatement().execute("USE " + Constants.TENANT_SCHEMA_DEFAULT);
   } catch (SQLException e) {
     throw new HibernateException("Could not alter JDBC connection to default schema", e);
   }
   currentConnection.remove();
   connectionProvider.closeConnection(connection);
 }
 public void release() throws SQLException {
   // we only release the connection
   if (connection != null) {
     JDBCExceptionReporter.logAndClearWarnings(connection);
     if (toggleAutoCommit) {
       connection.setAutoCommit(false);
     }
     provider.closeConnection(connection);
     connection = null;
   }
 }
 public void prepare(boolean needsAutoCommit) throws SQLException {
   connection = provider.getConnection();
   toggleAutoCommit = needsAutoCommit && !connection.getAutoCommit();
   if (toggleAutoCommit) {
     try {
       connection.commit();
     } catch (Throwable ignore) {
       // might happen with a managed connection
     }
     connection.setAutoCommit(true);
   }
 }
 public Connection getConnection(String tenantIdentifier) throws SQLException {
   final Connection connection = connectionProvider.getConnection();
   String schema = null;
   if (Strings.isNullOrEmpty(tenantIdentifier)) {
     schema = Constants.TENANT_SCHEMA_DEFAULT;
   } else {
     schema = Constants.TENANT_SCHEMA_PREFIX + tenantIdentifier;
   }
   connection.createStatement().execute("USE " + schema);
   currentConnection.set(connection);
   return connection;
 }
  @Override
  public ConnectionProvider initiateService(
      Map configurationValues, ServiceRegistryImplementor registry) {
    if (MultiTenancyStrategy.determineMultiTenancyStrategy(configurationValues)
        != MultiTenancyStrategy.NONE) {
      // nothing to do, but given the separate hierarchies have to handle this here.
    }

    final ClassLoaderService classLoaderService = registry.getService(ClassLoaderService.class);

    ConnectionProvider connectionProvider = null;
    String providerClassName = getConfiguredConnectionProviderName(configurationValues);
    if (providerClassName != null) {
      connectionProvider =
          instantiateExplicitConnectionProvider(providerClassName, classLoaderService);
    } else if (configurationValues.get(Environment.DATASOURCE) != null) {
      connectionProvider = new DatasourceConnectionProviderImpl();
    }

    if (connectionProvider == null) {
      if (c3p0ConfigDefined(configurationValues) && c3p0ProviderPresent(classLoaderService)) {
        connectionProvider =
            instantiateExplicitConnectionProvider(C3P0_PROVIDER_CLASS_NAME, classLoaderService);
      }
    }

    if (connectionProvider == null) {
      if (proxoolConfigDefined(configurationValues) && proxoolProviderPresent(classLoaderService)) {
        connectionProvider =
            instantiateExplicitConnectionProvider(PROXOOL_PROVIDER_CLASS_NAME, classLoaderService);
      }
    }

    if (connectionProvider == null) {
      if (configurationValues.get(Environment.URL) != null) {
        connectionProvider = new DriverManagerConnectionProviderImpl();
      }
    }

    if (connectionProvider == null) {
      LOG.noAppropriateConnectionProvider();
      connectionProvider = new UserSuppliedConnectionProviderImpl();
    }

    final Map injectionData = (Map) configurationValues.get(INJECTION_DATA);
    if (injectionData != null && injectionData.size() > 0) {
      final ConnectionProvider theConnectionProvider = connectionProvider;
      new BeanInfoHelper(connectionProvider.getClass())
          .applyToBeanInfo(
              connectionProvider,
              new BeanInfoHelper.BeanInfoDelegate() {
                public void processBeanInfo(BeanInfo beanInfo) throws Exception {
                  PropertyDescriptor[] descritors = beanInfo.getPropertyDescriptors();
                  for (int i = 0, size = descritors.length; i < size; i++) {
                    String propertyName = descritors[i].getName();
                    if (injectionData.containsKey(propertyName)) {
                      Method method = descritors[i].getWriteMethod();
                      method.invoke(theConnectionProvider, injectionData.get(propertyName));
                    }
                  }
                }
              });
    }

    return connectionProvider;
  }
 public Connection getAnyConnection() throws SQLException {
   final Connection connection = connectionProvider.getConnection();
   return connection;
 }