List of usage examples for org.hibernate SessionFactory close
void close() throws HibernateException;
From source file:org.nanocontainer.persistence.hibernate.annotations.ConstructableConfigurationTestCase.java
License:Open Source License
/** * Works Hibernate's configuration by attempting a write to the 'database'. With the latest * hiberanates, the configuration isn't really built until the session factory is built, and even * then, some of the data doesn't exist until a write occurs. * @param config//from w w w .j av a2 s .c o m */ private void attemptWrite(ConstructableConfiguration config) { Pojo pojo = new Pojo(); pojo.setFoo("Foo!"); SessionFactory sessionFactory = config.buildSessionFactory(); Session session = null; try { session = sessionFactory.openSession(); Integer result = (Integer) session.save(pojo); assertNotNull(result); session.close(); session = sessionFactory.openSession(); Pojo pojo2 = (Pojo) session.load(Pojo.class, result); assertNotNull(pojo); assertEquals(pojo.getId(), pojo2.getId()); assertEquals(pojo.getFoo(), pojo2.getFoo()); } finally { if (session != null && session.isOpen()) { session.close(); } sessionFactory.close(); } }
From source file:org.openeos.hibernate.internal.SessionFactoryProxyHandler.java
License:Apache License
private void releaseSessionFactory(SessionFactory realSessionFactory2) { realSessionFactory2.close(); }
From source file:org.ow2.bonita.util.DbMigration.java
License:Open Source License
/** * Migrate DB schema of the database defined in the environment. *///w w w. j a v a 2 s . c o m public static void migrateDb(final String domain, final String configurationName, final String db, final String currentVersion, final String targetVersion) throws Exception { BonitaConstants.getBonitaHomeFolder(); final SessionFactory sessionFactory = DbTool.getSessionFactory(domain, configurationName.replaceAll("-configuration", "-session-factory")); final String database = db.toLowerCase(); LOG.info("Running " + database + " " + configurationName + " DB migration..."); InputStream inputStream = null; try { inputStream = findMigrationScript(database, currentVersion, targetVersion); executeScript(sessionFactory, inputStream, database); LOG.info("---------------Schema migrated---------------"); sessionFactory.close(); } finally { if (inputStream != null) { inputStream.close(); } } }
From source file:org.rhs.ecommerce.hibernate.Main.java
License:Apache License
public static void main(String[] args) { Product product = new Product(); Order order = new Order(); product.setProductId(12000);/*from w ww .j a va 2 s .c o m*/ product.setName("Beadwork"); order.setOrderId(12000); order.setCustomer(128); order.setDateOrdered(new Date(2014, 5, 21)); SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); Session session = sessionFactory.openSession(); session.beginTransaction(); session.save(product); session.save(order); session.getTransaction().commit(); session.close(); sessionFactory.close(); }
From source file:org.specrunner.hibernate3.PluginSessionFactory.java
License:Open Source License
@Override public ENext doStart(IContext context, IResultSet result) throws PluginException { if (configuration == null && type == null && (factory == null || method == null)) { throw new PluginException( "Parameter 'configuration', 'type', or 'factory' and 'method' missing. In 'type' use an subtype of ISessionFactoryProvider, or choose a class in 'factory' whose 'method' is static and returns a Hibernate Configuration, or a 'configuration' by its name."); }//w w w . j a v a 2 s . c o m try { final SessionFactory sf; if (type != null) { if (UtilLog.LOG.isInfoEnabled()) { UtilLog.LOG.info("SessionFactory by type: " + type + "."); } Class<?> typeInstance = Class.forName(type); ISessionFactoryProvider conf = (ISessionFactoryProvider) typeInstance.newInstance(); sf = conf.getSessioFactory(); } else if (factory != null && method != null) { if (UtilLog.LOG.isInfoEnabled()) { UtilLog.LOG.info("SessionFactory by factory/method: " + factory + "." + method + "()"); } Class<?> typeInstance = Class.forName(factory); Method m = typeInstance.getMethod(method); sf = (SessionFactory) m.invoke(null); } else { if (UtilLog.LOG.isInfoEnabled()) { UtilLog.LOG.info("SessionFactory by Configuration named: " + configuration + "."); } Configuration cfg = PluginConfiguration.getConfiguration(context, configuration); sf = cfg.buildSessionFactory(); } String str = getName() != null ? getName() : SESSION_FACTORY; context.saveGlobal(str, new IDestructable() { @Override public Object getObject() { return sf; } @Override public void destroy() { sf.close(); } }); result.addResult(Success.INSTANCE, context.peek()); } catch (Exception e) { if (UtilLog.LOG.isDebugEnabled()) { UtilLog.LOG.debug(e.getMessage(), e); } throw new PluginException(e); } return ENext.SKIP; }
From source file:org.specrunner.hibernate4.PluginSessionFactory.java
License:Open Source License
@Override public ENext doStart(IContext context, IResultSet result) throws PluginException { if (configuration == null && type == null && (factory == null || method == null)) { throw new PluginException( "Parameter 'configuration', 'type', or 'factory' and 'method' missing. In 'type' use an subtype of ISessionFactoryProvider, or choose a class in 'factory' whose 'method' is static and returns a Hibernate Configuration, or a 'configuration' by its name."); }/*from w ww . j a v a 2 s.c o m*/ try { final SessionFactory sf; if (type != null) { if (UtilLog.LOG.isInfoEnabled()) { UtilLog.LOG.info("SessionFactory by type: " + type + "."); } Class<?> typeInstance = Class.forName(type); ISessionFactoryProvider conf = (ISessionFactoryProvider) typeInstance.newInstance(); sf = conf.getSessioFactory(); } else if (factory != null && method != null) { if (UtilLog.LOG.isInfoEnabled()) { UtilLog.LOG.info("SessionFactory by factory/method: " + factory + "." + method + "()"); } Class<?> typeInstance = Class.forName(factory); Method m = typeInstance.getMethod(method); sf = (SessionFactory) m.invoke(null); } else { if (UtilLog.LOG.isInfoEnabled()) { UtilLog.LOG.info("SessionFactory by Configuration named: " + configuration + "."); } Configuration cfg = PluginConfiguration.getConfiguration(context, configuration); ServiceRegistry serviceRegistry = createServiceRegistry(cfg); sf = cfg.buildSessionFactory(serviceRegistry); if (UtilLog.LOG.isInfoEnabled()) { UtilLog.LOG.info("SessionFactory : " + sf + "."); } setListeners(sf); } String str = getName() != null ? getName() : SESSION_FACTORY; context.saveGlobal(str, new IDestructable() { @Override public Object getObject() { return sf; } @Override public void destroy() { sf.close(); } }); result.addResult(Success.INSTANCE, context.peek()); } catch (Exception e) { if (UtilLog.LOG.isDebugEnabled()) { UtilLog.LOG.debug(e.getMessage(), e); } throw new PluginException(e); } return ENext.SKIP; }
From source file:org.springframework.orm.hibernate3.SessionFactoryBuilderSupport.java
License:Apache License
/** * Wrap the given {@code SessionFactory} with a proxy, if demanded. * <p>The default implementation wraps the given {@code SessionFactory} as a Spring * {@link DisposableBean} proxy in order to call {@link SessionFactory#close()} on * {@code ApplicationContext} {@linkplain ConfigurableApplicationContext#close() shutdown}. * <p>Subclasses may override this to implement transaction awareness through * a {@code SessionFactory} proxy for example, or even to avoid creation of the * {@code DisposableBean} proxy altogether. * @param rawSf the raw {@code SessionFactory} as built by {@link #buildSessionFactory()} * @return the {@code SessionFactory} reference to expose * @see #buildSessionFactory()//from www . ja v a 2 s .com */ protected SessionFactory wrapSessionFactoryIfNecessary(final SessionFactory rawSf) { return (SessionFactory) Proxy.newProxyInstance(this.beanClassLoader, new Class<?>[] { SessionFactory.class, DisposableBean.class }, new InvocationHandler() { public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if (ReflectionUtils.isToStringMethod(method)) { return String.format("DisposableBean proxy for SessionFactory [%s]", rawSf.toString()); } if (method.equals(DisposableBean.class.getMethod("destroy"))) { closeHibernateSessionFactory(SessionFactoryBuilderSupport.this, rawSf); rawSf.close(); return null; } return method.invoke(rawSf, args); } }); }
From source file:org.springframework.orm.hibernate3.SessionFactoryBuilderSupport.java
License:Apache License
static void closeHibernateSessionFactory(SessionFactoryBuilderSupport<?> builder, SessionFactory sessionFactory) { builder.logger.info("Closing Hibernate SessionFactory"); DataSource dataSource = builder.getDataSource(); if (dataSource != null) { // Make given DataSource available for potential SchemaExport, // which unfortunately reinstantiates a ConnectionProvider. SessionFactoryBuilderSupport.configTimeDataSourceHolder.set(dataSource); }/*from w w w . ja v a 2 s . c o m*/ try { builder.beforeSessionFactoryDestruction(); } finally { sessionFactory.close(); if (dataSource != null) { // Reset DataSource holder. SessionFactoryBuilderSupport.configTimeDataSourceHolder.remove(); } } }
From source file:org.unitime.timetable.StartupService.java
License:Apache License
@Override public void destroy() throws Exception { try {// w w w .ja v a 2 s .c o m Debug.info("******* UniTime " + Constants.getVersion() + " build on " + Constants.getReleaseDate() + " is going down *******"); Debug.info(" - Stopping Event Expiration Service ..."); EventExpirationService.getInstance().interrupt(); SolverInfo.stopInfoCacheCleanup(); ApplicationProperties.stopListener(); if (RoomAvailability.getInstance() != null) { Debug.info(" - Stopping Room Availability Service ... "); RoomAvailability.getInstance().stopService(); } QueueProcessor.stopProcessor(); Debug.info(" - Removing Message Log Appender ... "); Logger.getRootLogger().removeAppender(iMessageLogAppender); iMessageLogAppender.close(); Debug.info(" - Closing Hibernate ... "); (new _BaseRootDAO() { void closeHibernate() { SessionFactory sf = sSessionFactory; if (sf != null) { sSessionFactory = null; if (sf instanceof SessionFactoryImpl) { ConnectionProvider cp = ((SessionFactoryImpl) sf).getConnectionProvider(); if (cp instanceof DisposableConnectionProvider) { try { ((DisposableConnectionProvider) cp).destroy(); } catch (Exception e) { } } } sf.close(); } } protected Class getReferenceClass() { return null; } }).closeHibernate(); // CacheManager.getInstance().shutdown(); Debug.info("******* UniTime " + Constants.getVersion() + " shut down successfully *******"); } catch (Exception e) { Debug.error("UniTime Shutdown Failed : " + e.getMessage(), e); if (e instanceof RuntimeException) throw (RuntimeException) e; else throw new RuntimeException("UniTime Shutdown Failed : " + e.getMessage(), e); } }
From source file:org.wso2.appserver.hibernate.jndi.sample.listener.HibernateSessionFactoryListener.java
License:Open Source License
public void contextDestroyed(ServletContextEvent servletContextEvent) { SessionFactory sessionFactory = (SessionFactory) servletContextEvent.getServletContext() .getAttribute("SessionFactory"); if (sessionFactory != null && !sessionFactory.isClosed()) { logger.info("Closing sessionFactory"); sessionFactory.close(); }/*from w ww . j a v a 2s. c o m*/ logger.info("Released Hibernate sessionFactory resource"); }