All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hibernate.testing.jdbc.ConnectionProviderDelegate Maven / Gradle / Ivy

There is a newer version: 7.0.0.Beta2
Show newest version
/*
 * Hibernate, Relational Persistence for Idiomatic Java
 *
 * License: GNU Lesser General Public License (LGPL), version 2.1 or later.
 * See the lgpl.txt file in the root directory or .
 */
package org.hibernate.testing.jdbc;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.hibernate.cfg.AvailableSettings;
import org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator;
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
import org.hibernate.engine.jdbc.connections.spi.DatabaseConnectionInfo;
import org.hibernate.service.spi.Configurable;
import org.hibernate.service.spi.ServiceRegistryAwareService;
import org.hibernate.service.spi.ServiceRegistryImplementor;
import org.hibernate.service.spi.Stoppable;

/**
 * This {@link ConnectionProvider} extends any other ConnectionProvider
 * that would be used by default taken the current configuration properties.
 *
 * @author Vlad Mihalcea
 */
public class ConnectionProviderDelegate implements
		ConnectionProvider,
		Configurable,
		ServiceRegistryAwareService,
		Stoppable {

	private ServiceRegistryImplementor serviceRegistry;

	private ConnectionProvider connectionProvider;
	private boolean configured;
	private final boolean forceSupportsAggressiveRelease;

	public ConnectionProviderDelegate(){
		this(false);
	}

	public ConnectionProviderDelegate(boolean forceSupportsAggressiveRelease) {
		this.forceSupportsAggressiveRelease = forceSupportsAggressiveRelease;
	}

	public ConnectionProviderDelegate(ConnectionProvider connectionProvider) {
		this.connectionProvider = connectionProvider;
		this.forceSupportsAggressiveRelease = false;
	}

	public ConnectionProvider getConnectionProvider() {
		return connectionProvider;
	}

	public void setConnectionProvider(ConnectionProvider connectionProvider) {
		this.connectionProvider = connectionProvider;
	}

	@Override
	public void injectServices(ServiceRegistryImplementor serviceRegistry) {
		this.serviceRegistry = serviceRegistry;
	}

	@Override
	public void configure(Map configurationValues) {
		if ( !configured ) {
			if ( connectionProvider == null ) {
				Map settings = new HashMap<>( configurationValues );
				settings.remove( AvailableSettings.CONNECTION_PROVIDER );
				connectionProvider = ConnectionProviderInitiator.INSTANCE.initiateService(
						settings,
						serviceRegistry
				);
			}
			if ( connectionProvider instanceof ServiceRegistryAwareService ) {
				( (ServiceRegistryAwareService) connectionProvider ).injectServices( serviceRegistry );
			}
			if ( connectionProvider instanceof Configurable ) {
				Configurable configurableConnectionProvider = (Configurable) connectionProvider;
				configurableConnectionProvider.configure( configurationValues );
			}
			configured = true;
		}
	}

	@Override
	public Connection getConnection() throws SQLException {
		return connectionProvider.getConnection();
	}

	@Override
	public void closeConnection(Connection connection) throws SQLException {
		connectionProvider.closeConnection( connection );
	}

	@Override
	public boolean supportsAggressiveRelease() {
		if ( forceSupportsAggressiveRelease ) {
			return true;
		}
		return connectionProvider.supportsAggressiveRelease();
	}

	@Override
	public DatabaseConnectionInfo getDatabaseConnectionInfo() {
		return connectionProvider.getDatabaseConnectionInfo();
	}

	@Override
	public boolean isUnwrappableAs(Class unwrapType) {
		return connectionProvider.isUnwrappableAs( unwrapType );
	}

	@Override
	public  T unwrap(Class unwrapType) {
		return connectionProvider.unwrap( unwrapType );
	}

	@Override
	public void stop() {
		if ( connectionProvider instanceof Stoppable ) {
			( (Stoppable) connectionProvider ).stop();
		}
	}


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy