Example usage for org.hibernate.cfg AvailableSettings DATASOURCE

List of usage examples for org.hibernate.cfg AvailableSettings DATASOURCE

Introduction

In this page you can find the example usage for org.hibernate.cfg AvailableSettings DATASOURCE.

Prototype

String DATASOURCE

To view the source code for org.hibernate.cfg AvailableSettings DATASOURCE.

Click Source Link

Document

Names a javax.sql.DataSource .

Usage

From source file:com.baomidou.hibernateplus.HibernateSpringSessionFactoryBuilder.java

License:Apache License

/**
 * Create a new LocalSessionFactoryBuilder for the given DataSource.
 *
 * @param dataSource      the JDBC DataSource that the resulting Hibernate SessionFactory should be using
 *                        (may be {@code null})
 * @param resourceLoader  the ResourceLoader to load application classes from
 * @param metadataSources the Hibernate MetadataSources service to use (e.g. reusing an existing one)
 * @since 4.3//from   w ww.  j a v  a2s . com
 */
public HibernateSpringSessionFactoryBuilder(DataSource dataSource, ResourceLoader resourceLoader,
        MetadataSources metadataSources) {
    super(metadataSources);

    getProperties().put(AvailableSettings.CURRENT_SESSION_CONTEXT_CLASS, SpringSessionContext.class.getName());
    if (dataSource != null) {
        getProperties().put(AvailableSettings.DATASOURCE, dataSource);
    }

    // Hibernate 5.1/5.2: manually enforce connection release mode ON_CLOSE (the former default)
    try {
        // Try Hibernate 5.2
        AvailableSettings.class.getField("CONNECTION_HANDLING");
        getProperties().put("hibernate.connection.handling_mode", "DELAYED_ACQUISITION_AND_HOLD");
    } catch (NoSuchFieldException ex) {
        // Try Hibernate 5.1
        try {
            AvailableSettings.class.getField("ACQUIRE_CONNECTIONS");
            getProperties().put("hibernate.connection.release_mode", "ON_CLOSE");
        } catch (NoSuchFieldException ex2) {
            // on Hibernate 5.0.x or lower - no need to change the default there
        }
    }

    getProperties().put(AvailableSettings.CLASSLOADERS, Collections.singleton(resourceLoader.getClassLoader()));
    this.resourcePatternResolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader);
}

From source file:com.blogspot.hwellmann.multitenancy.hibernate.SchemaMultiTenantConnectionProvider.java

License:Apache License

@Override
public void injectServices(ServiceRegistryImplementor serviceRegistry) {
    final Object dataSourceConfigValue = serviceRegistry.getService(ConfigurationService.class).getSettings()
            .get(AvailableSettings.DATASOURCE);
    if (dataSourceConfigValue == null || !String.class.isInstance(dataSourceConfigValue)) {
        throw new HibernateException("Improper set up of DataSourceBasedMultiTenantConnectionProviderImpl");
    }//from w  ww .j a v  a 2s .c o  m
    final String jndiName = (String) dataSourceConfigValue;

    jndiService = serviceRegistry.getService(JndiService.class);
    if (jndiService == null) {
        throw new HibernateException(
                "Could not locate JndiService from DataSourceBasedMultiTenantConnectionProviderImpl");
    }

    final Object namedObject = jndiService.locate(jndiName);
    if (namedObject == null) {
        throw new HibernateException("JNDI name [" + jndiName + "] could not be resolved");
    }

    if (DataSource.class.isInstance(namedObject)) {
        final int loc = jndiName.lastIndexOf("/");
        this.baseJndiNamespace = jndiName.substring(0, loc);
        this.tenantIdentifierForAny = jndiName.substring(loc + 1);
        dataSourceMap().put(tenantIdentifierForAny, (DataSource) namedObject);
    } else if (Context.class.isInstance(namedObject)) {
        this.baseJndiNamespace = jndiName;
        this.tenantIdentifierForAny = (String) serviceRegistry.getService(ConfigurationService.class)
                .getSettings().get(TENANT_IDENTIFIER_TO_USE_FOR_ANY_KEY);
        if (tenantIdentifierForAny == null) {
            throw new HibernateException(
                    "JNDI name named a Context, but tenant identifier to use for ANY was not specified");
        }
    } else {
        throw new HibernateException("Unknown object type [" + namedObject.getClass().getName()
                + "] found in JNDI location [" + jndiName + "]");
    }
}

From source file:com.github.javarch.persistence.orm.hibernate.conf.HibernatePropertiesConfig.java

License:Apache License

/**
 * Retorna um objeto Properties com todas as configuraes de propriedades que devem
 * ser passadas ao {@link SessionFactory}. As propriedades so definidas no arquivo
 * hibernate.properties que deve estar presente no raiz do classpath.
 * /*from w  ww  .  jav  a2s  .  c o  m*/
 * @return Properties do Hibernate.
 */
@Bean
public Properties hibernateProperties() {

    Properties props = new Properties();

    props.put(AvailableSettings.DIALECT, env.getRequiredProperty(AvailableSettings.DIALECT));

    if (env.acceptsProfiles(Profiles.TEST)) {
        props.put(AvailableSettings.HBM2DDL_AUTO, env.getProperty(AvailableSettings.HBM2DDL_AUTO, "update"));
    }

    if (env.acceptsProfiles(Profiles.MULT_TENANT)) {
        if (log.isDebugEnabled()) {
            log.debug("Profile Multi Tenancy ativado! Realizando configurao...");
        }
        if (currentIdentifierResolver == null || env.acceptsProfiles(Profiles.PRODUCTION)) {
            throw new DatabaseConfigurationException(
                    "No foi encontrado nenhum objeto CurrentIdentifierResolver.");
        }
        props.put(AvailableSettings.MULTI_TENANT_IDENTIFIER_RESOLVER, currentIdentifierResolver);
        props.put(AvailableSettings.DATASOURCE, env.getRequiredProperty(AvailableSettings.DATASOURCE));
        props.put(AvailableSettings.MULTI_TENANT_CONNECTION_PROVIDER, multiTenantConnectionProvider);
        props.put(AvailableSettings.MULTI_TENANT, env.getRequiredProperty(AvailableSettings.MULTI_TENANT));
    } else {

        props.put(AvailableSettings.USE_SECOND_LEVEL_CACHE, true);
        props.put(AvailableSettings.USE_QUERY_CACHE, true);
        props.put(AvailableSettings.CACHE_REGION_FACTORY, "org.hibernate.cache.ehcache.EhCacheRegionFactory");
    }

    if (env.acceptsProfiles(Profiles.ENVERS)) {
        props.put("org.hibernate.envers.versionsTableSuffix", "_audit");
        props.put("org.hibernate.envers.revisionFieldName", "revision");
        props.put("org.hibernate.envers.default_schema", "audit");
    }

    return props;
}

From source file:org.overlord.rtgov.common.jpa.JpaStore.java

License:Apache License

/**
 * This method returns a Session./*  w ww.  j  av a 2  s .com*/
 * 
 * @return The Session
 */
private Session getSession() {
    if (_persistenceUnit == null) {
        if (_sessionFactory == null) {
            final Configuration cfg = new Configuration().configure(_configXml);
            final Properties properties = RTGovProperties.getProperties();
            if (_jndiProperty != null) {
                String prop = RTGovProperties.getProperty(_jndiProperty);
                if (prop != null) {
                    properties.setProperty(AvailableSettings.DATASOURCE, prop);
                }
            }
            final String jtaPlatform = RTGovProperties.getProperty(JTA_PLATFORM_PROPERTY);
            if (jtaPlatform == null) {
                LOG.warning("JpaStore.jtaPlatform not set in overlord-rtgov.properties!");
                _isJta = false;
            } else {
                properties.setProperty(AvailableSettings.JTA_PLATFORM,
                        RTGovProperties.getProperty(JTA_PLATFORM_PROPERTY));
            }
            cfg.getProperties().putAll(properties);
            _sessionFactory = cfg.buildSessionFactory();
        }

        return _sessionFactory.openSession();
    } else {
        // NOTE: For backward compatibility in EAP, I'm allowing the
        // continued use of a persistence unit.
        // Simply use it to create an EntityManager, then unwrap to the
        // native Hibernate Session.
        if (_entityManagerFactory == null) {
            final Properties properties = RTGovProperties.getProperties();
            if (_jndiProperty != null) {
                properties.setProperty(AvailableSettings.DATASOURCE,
                        RTGovProperties.getProperty(_jndiProperty));
            }
            final String jtaPlatform = RTGovProperties.getProperty(JTA_PLATFORM_PROPERTY);
            if (jtaPlatform == null) {
                LOG.warning("JpaStore.jtaPlatform not set in overlord-rtgov.properties!");
                _isJta = false;
            } else {
                properties.setProperty(AvailableSettings.JTA_PLATFORM,
                        RTGovProperties.getProperty(JTA_PLATFORM_PROPERTY));
            }
            _entityManagerFactory = Persistence.createEntityManagerFactory(_persistenceUnit, properties);
        }

        return _entityManagerFactory.createEntityManager().unwrap(Session.class);
    }
}

From source file:zcu.xutil.orm.SessionFactoryBean.java

License:Apache License

public SessionFactory getObject() {
    configure.getProperties().put(AvailableSettings.DATASOURCE, dataSource);
    configure.setProperty(AvailableSettings.CURRENT_SESSION_CONTEXT_CLASS, XutilSessionContext.class.getName());
    if (TxManager.getTxManager().getUserTransaction() != null) {
        configure.setProperty(AvailableSettings.JTA_PLATFORM, JTA.class.getName());
        configure.setProperty(AvailableSettings.TRANSACTION_STRATEGY, JtaTransactionFactory.class.getName());
    }/* w w w. j av a 2s.  co m*/
    ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configure.getProperties())
            .buildServiceRegistry();
    return configure.buildSessionFactory(serviceRegistry);
}