Search in sources :

Example 91 with SessionFactory

use of org.hibernate.SessionFactory in project hibernate-orm by hibernate.

the class AbstractGeneralDataRegionTest method withSessionFactoriesAndRegions.

protected void withSessionFactoriesAndRegions(int num, SFRConsumer consumer) throws Exception {
    StandardServiceRegistryBuilder ssrb = createStandardServiceRegistryBuilder().applySetting(AvailableSettings.CACHE_REGION_FACTORY, TestInfinispanRegionFactory.class.getName());
    Properties properties = CacheTestUtil.toProperties(ssrb.getSettings());
    List<StandardServiceRegistry> registries = new ArrayList<>();
    List<SessionFactory> sessionFactories = new ArrayList<>();
    List<GeneralDataRegion> regions = new ArrayList<>();
    for (int i = 0; i < num; ++i) {
        StandardServiceRegistry registry = ssrb.build();
        registries.add(registry);
        SessionFactory sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
        sessionFactories.add(sessionFactory);
        InfinispanRegionFactory regionFactory = (InfinispanRegionFactory) registry.getService(RegionFactory.class);
        GeneralDataRegion region = (GeneralDataRegion) createRegion(regionFactory, getStandardRegionName(REGION_PREFIX), properties, null);
        regions.add(region);
    }
    try {
        consumer.accept(sessionFactories, regions);
    } finally {
        for (SessionFactory sessionFactory : sessionFactories) {
            sessionFactory.close();
        }
        for (StandardServiceRegistry registry : registries) {
            StandardServiceRegistryBuilder.destroy(registry);
        }
    }
}
Also used : SessionFactory(org.hibernate.SessionFactory) InfinispanRegionFactory(org.hibernate.cache.infinispan.InfinispanRegionFactory) TestInfinispanRegionFactory(org.hibernate.test.cache.infinispan.util.TestInfinispanRegionFactory) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) ArrayList(java.util.ArrayList) MetadataSources(org.hibernate.boot.MetadataSources) Properties(java.util.Properties) BaseGeneralDataRegion(org.hibernate.cache.infinispan.impl.BaseGeneralDataRegion) GeneralDataRegion(org.hibernate.cache.spi.GeneralDataRegion) InfinispanRegionFactory(org.hibernate.cache.infinispan.InfinispanRegionFactory) RegionFactory(org.hibernate.cache.spi.RegionFactory) TestInfinispanRegionFactory(org.hibernate.test.cache.infinispan.util.TestInfinispanRegionFactory) TestInfinispanRegionFactory(org.hibernate.test.cache.infinispan.util.TestInfinispanRegionFactory) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry)

Example 92 with SessionFactory

use of org.hibernate.SessionFactory in project spring-framework by spring-projects.

the class OpenSessionInViewFilter method doFilterInternal.

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
    SessionFactory sessionFactory = lookupSessionFactory(request);
    boolean participate = false;
    WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
    String key = getAlreadyFilteredAttributeName();
    if (TransactionSynchronizationManager.hasResource(sessionFactory)) {
        // Do not modify the Session: just set the participate flag.
        participate = true;
    } else {
        boolean isFirstRequest = !isAsyncDispatch(request);
        if (isFirstRequest || !applySessionBindingInterceptor(asyncManager, key)) {
            logger.debug("Opening Hibernate Session in OpenSessionInViewFilter");
            Session session = openSession(sessionFactory);
            SessionHolder sessionHolder = new SessionHolder(session);
            TransactionSynchronizationManager.bindResource(sessionFactory, sessionHolder);
            AsyncRequestInterceptor interceptor = new AsyncRequestInterceptor(sessionFactory, sessionHolder);
            asyncManager.registerCallableInterceptor(key, interceptor);
            asyncManager.registerDeferredResultInterceptor(key, interceptor);
        }
    }
    try {
        filterChain.doFilter(request, response);
    } finally {
        if (!participate) {
            SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.unbindResource(sessionFactory);
            if (!isAsyncStarted(request)) {
                logger.debug("Closing Hibernate Session in OpenSessionInViewFilter");
                SessionFactoryUtils.closeSession(sessionHolder.getSession());
            }
        }
    }
}
Also used : SessionFactory(org.hibernate.SessionFactory) WebAsyncManager(org.springframework.web.context.request.async.WebAsyncManager) SessionHolder(org.springframework.orm.hibernate5.SessionHolder) Session(org.hibernate.Session)

Example 93 with SessionFactory

use of org.hibernate.SessionFactory in project spring-framework by spring-projects.

the class HibernateEntityManagerFactoryIntegrationTests method testCanCastNativeEntityManagerFactoryToHibernateEntityManagerFactoryImpl.

@Test
public void testCanCastNativeEntityManagerFactoryToHibernateEntityManagerFactoryImpl() {
    EntityManagerFactoryInfo emfi = (EntityManagerFactoryInfo) entityManagerFactory;
    assertTrue(emfi.getNativeEntityManagerFactory() instanceof org.hibernate.jpa.HibernateEntityManagerFactory);
    // as of Hibernate 5.2
    assertTrue(emfi.getNativeEntityManagerFactory() instanceof SessionFactory);
}
Also used : SessionFactory(org.hibernate.SessionFactory) EntityManagerFactoryInfo(org.springframework.orm.jpa.EntityManagerFactoryInfo) Test(org.junit.Test)

Example 94 with SessionFactory

use of org.hibernate.SessionFactory in project onebusaway-gtfs-modules by OneBusAway.

the class GtfsDatabaseLoaderMain method run.

private void run(String[] args) throws IOException {
    CommandLine cli = parseCommandLineOptions(args);
    args = cli.getArgs();
    if (args.length != 1) {
        printUsage();
        System.exit(-1);
    }
    Configuration config = new Configuration();
    config.setProperty("hibernate.connection.driver_class", cli.getOptionValue(ARG_DRIVER_CLASS));
    config.setProperty("hibernate.connection.url", cli.getOptionValue(ARG_URL));
    if (cli.hasOption(ARG_USERNAME)) {
        config.setProperty("hibernate.connection.username", cli.getOptionValue(ARG_USERNAME));
    }
    if (cli.hasOption(ARG_PASSWORD)) {
        config.setProperty("hibernate.connection.password", cli.getOptionValue(ARG_PASSWORD));
    }
    config.setProperty("hibernate.connection.pool_size", "1");
    config.setProperty("hibernate.cache.provider_class", "org.hibernate.cache.NoCacheProvider");
    config.setProperty("hibernate.hbm2ddl.auto", "update");
    config.addResource("org/onebusaway/gtfs/model/GtfsMapping.hibernate.xml");
    config.addResource("org/onebusaway/gtfs/impl/HibernateGtfsRelationalDaoImpl.hibernate.xml");
    SessionFactory sessionFactory = config.buildSessionFactory();
    HibernateGtfsFactory factory = new HibernateGtfsFactory(sessionFactory);
    GtfsReader reader = new GtfsReader();
    reader.setInputLocation(new File(args[0]));
    GtfsMutableRelationalDao dao = factory.getDao();
    reader.setEntityStore(dao);
    reader.run();
    reader.close();
}
Also used : SessionFactory(org.hibernate.SessionFactory) GtfsMutableRelationalDao(org.onebusaway.gtfs.services.GtfsMutableRelationalDao) CommandLine(org.apache.commons.cli.CommandLine) GtfsReader(org.onebusaway.gtfs.serialization.GtfsReader) Configuration(org.hibernate.cfg.Configuration) HibernateGtfsFactory(org.onebusaway.gtfs.services.HibernateGtfsFactory) File(java.io.File)

Example 95 with SessionFactory

use of org.hibernate.SessionFactory in project onebusaway-gtfs-modules by OneBusAway.

the class GtfsHibernateReaderExampleMain method createHibernateGtfsFactory.

private static HibernateGtfsFactory createHibernateGtfsFactory(String resource) {
    Configuration config = new Configuration();
    if (resource.startsWith(KEY_CLASSPATH)) {
        resource = resource.substring(KEY_CLASSPATH.length());
        config = config.configure(resource);
    } else if (resource.startsWith(KEY_FILE)) {
        resource = resource.substring(KEY_FILE.length());
        config = config.configure(new File(resource));
    } else {
        config = config.configure(new File(resource));
    }
    SessionFactory sessionFactory = config.buildSessionFactory();
    return new HibernateGtfsFactory(sessionFactory);
}
Also used : SessionFactory(org.hibernate.SessionFactory) Configuration(org.hibernate.cfg.Configuration) HibernateGtfsFactory(org.onebusaway.gtfs.services.HibernateGtfsFactory) File(java.io.File)

Aggregations

SessionFactory (org.hibernate.SessionFactory)108 Test (org.junit.Test)62 Session (org.hibernate.Session)50 Configuration (org.hibernate.cfg.Configuration)35 Transaction (org.hibernate.Transaction)20 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)19 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)13 MetadataSources (org.hibernate.boot.MetadataSources)11 HibernateEntityManagerFactory (org.hibernate.jpa.HibernateEntityManagerFactory)9 TestForIssue (org.hibernate.testing.TestForIssue)9 Properties (java.util.Properties)8 Query (org.hibernate.Query)8 Metadata (org.hibernate.boot.Metadata)8 ArrayList (java.util.ArrayList)7 InfinispanRegionFactory (org.hibernate.cache.infinispan.InfinispanRegionFactory)7 List (java.util.List)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 TimeUnit (java.util.concurrent.TimeUnit)5 AnnotationException (org.hibernate.AnnotationException)5 Collections (java.util.Collections)4