Example usage for org.hibernate.cfg AvailableSettings CURRENT_SESSION_CONTEXT_CLASS

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

Introduction

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

Prototype

String CURRENT_SESSION_CONTEXT_CLASS

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

Click Source Link

Document

Context scoping impl for org.hibernate.SessionFactory#getCurrentSession() processing.

Usage

From source file:com.astonish.dropwizard.routing.hibernate.RoutingSessionFactoryFactory.java

License:Apache License

/**
 * Builds a {@link SessionFactory}/*  w  w  w.  ja va 2  s  . co m*/
 * @param bundle
 *            the bundle
 * @param dbConfig
 *            the dbconfig
 * @param connectionProvider
 *            the connection provider
 * @param properties
 *            the hibernate properties
 * @param entities
 *            the persistent entities
 * @return {@link SessionFactory}
 */
private SessionFactory buildSessionFactory(RoutingHibernateBundle<?> bundle, DataSourceFactory dbConfig,
        ConnectionProvider connectionProvider, Map<String, String> properties, List<Class<?>> entities) {
    final Configuration configuration = new Configuration();
    configuration.setProperty(AvailableSettings.CURRENT_SESSION_CONTEXT_CLASS, "managed");
    configuration.setProperty(AvailableSettings.USE_SQL_COMMENTS,
            Boolean.toString(dbConfig.isAutoCommentsEnabled()));
    configuration.setProperty(AvailableSettings.USE_GET_GENERATED_KEYS, "true");
    configuration.setProperty(AvailableSettings.GENERATE_STATISTICS, "true");
    configuration.setProperty(AvailableSettings.USE_REFLECTION_OPTIMIZER, "true");
    configuration.setProperty(AvailableSettings.ORDER_UPDATES, "true");
    configuration.setProperty(AvailableSettings.ORDER_INSERTS, "true");
    configuration.setProperty(AvailableSettings.USE_NEW_ID_GENERATOR_MAPPINGS, "true");
    configuration.setProperty("jadira.usertype.autoRegisterUserTypes", "true");
    for (Map.Entry<String, String> property : properties.entrySet()) {
        configuration.setProperty(property.getKey(), property.getValue());
    }

    addAnnotatedClasses(configuration, entities);
    bundle.configure(configuration);

    final ServiceRegistry registry = new StandardServiceRegistryBuilder()
            .addService(ConnectionProvider.class, connectionProvider).applySettings(properties).build();

    return configuration.buildSessionFactory(registry);
}

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  .ja  v a  2s. co m*/
 */
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.vmware.photon.controller.apife.db.HibernateTestModule.java

License:Open Source License

@Provides
@Singleton//from  w  ww .  j a  va2s .c om
public SessionFactory getSessionFactory() {
    Configuration configuration = new Configuration();

    Reflections reflections = new Reflections("com.vmware.photon.controller.apife.entities");
    Set<Class<?>> classes = reflections.getTypesAnnotatedWith(Entity.class);

    Reflections baseReflections = new Reflections("com.vmware.photon.controller.apife.entities.base");
    classes.addAll(baseReflections.getTypesAnnotatedWith(Entity.class));

    Reflections commonReflections = new Reflections("com.vmware.photon.controller.api.common");
    classes.addAll(commonReflections.getTypesAnnotatedWith(Entity.class));

    for (final Class<?> clazz : classes) {
        configuration.addAnnotatedClass(clazz);
    }

    configuration.setProperty(AvailableSettings.CURRENT_SESSION_CONTEXT_CLASS, "managed");
    configuration.setProperty(AvailableSettings.DIALECT, CustomH2Dialect.class.getName());
    configuration.setProperty(AvailableSettings.DRIVER, "org.h2.Driver");
    // in memory DB, wait up to 10 seconds after last connection closed before deleting data
    configuration.setProperty(AvailableSettings.URL, "jdbc:h2:mem:test;DB_CLOSE_DELAY=10");
    configuration.setProperty(AvailableSettings.HBM2DDL_AUTO, "create");
    configuration.setProperty(AvailableSettings.SHOW_SQL, "true");
    configuration.setNamingStrategy(ImprovedNamingStrategy.INSTANCE);

    ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties())
            .build();

    return configuration.buildSessionFactory(serviceRegistry);
}

From source file:com.vmware.photon.controller.apife.db.MigrationTest.java

License:Open Source License

@Test
public void testMigrations() throws SQLException, LiquibaseException {
    try (Connection connection = DriverManager.getConnection("jdbc:h2:mem:migrations", "sa", "")) {
        Liquibase liquibase = new Liquibase("migrations.xml", new ClassLoaderResourceAccessor(),
                new JdbcConnection(connection));
        liquibase.update("");

        Configuration configuration = new Configuration();

        Reflections reflections = new Reflections("com.vmware.photon.controller.apife.entities");
        Set<Class<?>> classes = reflections.getTypesAnnotatedWith(Entity.class);

        Reflections commonReflections = new Reflections("com.vmware.photon.controller.api.common");
        classes.addAll(commonReflections.getTypesAnnotatedWith(Entity.class));
        for (final Class<?> clazz : classes) {
            configuration.addAnnotatedClass(clazz);
        }//from   www . j  a  v  a 2 s  .  co  m

        configuration.setProperty(AvailableSettings.CURRENT_SESSION_CONTEXT_CLASS, "thread");
        configuration.setProperty(AvailableSettings.DIALECT, "org.hibernate.dialect.H2Dialect");
        configuration.setProperty(AvailableSettings.DRIVER, "org.h2.Driver");
        configuration.setProperty(AvailableSettings.URL, "jdbc:h2:mem:migrations");
        configuration.setProperty(AvailableSettings.USER, "sa");
        configuration.setProperty(AvailableSettings.PASS, "");
        configuration.setProperty(AvailableSettings.HBM2DDL_AUTO, "validate");
        configuration.setNamingStrategy(ImprovedNamingStrategy.INSTANCE);

        ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
                .applySettings(configuration.getProperties()).build();

        SessionFactory factory = configuration.buildSessionFactory(serviceRegistry);
        factory.close();
    }
}

From source file:com.yahoo.elide.contrib.dropwizard.elide.SessionFactoryFactory.java

License:Apache License

private SessionFactory buildSessionFactory(ElideBundle<?> bundle, PooledDataSourceFactory dbConfig,
        ConnectionProvider connectionProvider, Map<String, String> properties, List<Class<?>> entities) {
    final Configuration configuration = new Configuration();
    configuration.setProperty(AvailableSettings.CURRENT_SESSION_CONTEXT_CLASS, "managed");
    configuration.setProperty(AvailableSettings.USE_SQL_COMMENTS,
            Boolean.toString(dbConfig.isAutoCommentsEnabled()));
    configuration.setProperty(AvailableSettings.USE_GET_GENERATED_KEYS, "true");
    configuration.setProperty(AvailableSettings.GENERATE_STATISTICS, "true");
    configuration.setProperty(AvailableSettings.USE_REFLECTION_OPTIMIZER, "true");
    configuration.setProperty(AvailableSettings.ORDER_UPDATES, "true");
    configuration.setProperty(AvailableSettings.ORDER_INSERTS, "true");
    configuration.setProperty(AvailableSettings.USE_NEW_ID_GENERATOR_MAPPINGS, "true");
    configuration.setProperty("jadira.usertype.autoRegisterUserTypes", "true");
    for (Map.Entry<String, String> property : properties.entrySet()) {
        configuration.setProperty(property.getKey(), property.getValue());
    }// ww w. j a  va  2s .c  o  m

    addAnnotatedClasses(configuration, entities);
    bundle.configure(configuration);

    final ServiceRegistry registry = new StandardServiceRegistryBuilder()
            .addService(ConnectionProvider.class, connectionProvider)
            .applySettings(configuration.getProperties()).build();

    configure(configuration, registry);

    return configuration.buildSessionFactory(registry);
}

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 a  v  a  2  s  . co m
    ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configure.getProperties())
            .buildServiceRegistry();
    return configure.buildSessionFactory(serviceRegistry);
}