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);
}
}
}
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());
}
}
}
}
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);
}
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();
}
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);
}
Aggregations