Example usage for org.hibernate SessionFactoryObserver SessionFactoryObserver

List of usage examples for org.hibernate SessionFactoryObserver SessionFactoryObserver

Introduction

In this page you can find the example usage for org.hibernate SessionFactoryObserver SessionFactoryObserver.

Prototype

SessionFactoryObserver

Source Link

Usage

From source file:Baari.DbAccess.java

public void alustaHibernate() {

    Configuration config = new Configuration().configure("Baari/hibernate.cfg.xml");

    StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
            .applySettings(config.getProperties()).build();

    //StandardServiceRegistryBuilder builder
    //       = new StandardServiceRegistryBuilder().applySettings(config.getProperties());
    istuntotehdas = config.buildSessionFactory(serviceRegistry);

    config.setSessionFactoryObserver(new SessionFactoryObserver() {
        @Override/*from w  ww  . j  ava 2 s.  c  o m*/
        public void sessionFactoryCreated(SessionFactory factory) {
        }

        @Override
        public void sessionFactoryClosed(SessionFactory factory) {
            ((StandardServiceRegistryImpl) serviceRegistry).destroy();
        }
    });

    alustettu = true;

}

From source file:com.storenotes.util.HibernateStuff.java

public HibernateStuff() {
    Configuration config = new Configuration();

    config.addAnnotatedClass(com.storenotes.domain.Employee.class);
    config.addAnnotatedClass(com.storenotes.domain.StoreSection.class);
    config.addAnnotatedClass(com.storenotes.domain.Task.class);

    config = config.configure();/*from   w  ww  . ja va  2s. co m*/

    new SchemaExport(config).create(true, true);

    StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder();
    serviceRegistryBuilder.applySettings(config.getProperties());

    final ServiceRegistry serviceRegistry = serviceRegistryBuilder.build();

    config.setSessionFactoryObserver(new SessionFactoryObserver() {
        @Override
        public void sessionFactoryCreated(SessionFactory factory) {
        }

        @Override
        public void sessionFactoryClosed(SessionFactory factory) {
            System.out.println("sessionFactoryClosed()");
            ((StandardServiceRegistryImpl) serviceRegistry).destroy();
        }
    });

    this.sessionFactory = config.buildSessionFactory(serviceRegistry);
}

From source file:org.beangle.orm.hibernate.SessionFactoryBean.java

License:Open Source License

@SuppressWarnings("unchecked")
public void init() throws Exception {
    configuration = newConfiguration();/*from   www.j a v  a 2  s  .  c o  m*/
    configuration.getProperties().put("hibernate.classLoader.application", beanClassLoader);
    // Set Hibernate 4.0+ CurrentSessionContext implementation,
    // provide the Beangle-managed Session as current Session.
    configuration.setProperty(Environment.CURRENT_SESSION_CONTEXT_CLASS, BeangleSessionContext.class.getName());
    if (this.namingStrategy != null)
        configuration.setNamingStrategy(this.namingStrategy);
    if (dataSource != null)
        configuration.getProperties().put(Environment.DATASOURCE, dataSource);
    if (this.hibernateProperties != null)
        configuration.addProperties(this.hibernateProperties);

    try {
        if (null != configLocations) {
            for (Resource resource : configLocations)
                configuration.configure(resource.getURL());
        }
        if (null != persistLocations) {
            for (Resource resource : persistLocations) {
                InputStream is = resource.getURL().openStream();
                Properties props = new Properties();
                if (null != is)
                    props.load(is);

                Object module = props.remove("module");
                if (null == module) {
                    logger.warn("Cannot find module in {}", resource.getURL());
                    continue;
                }

                Class<? extends AbstractPersistModule> moduleClass = (Class<? extends AbstractPersistModule>) ClassLoaders
                        .loadClass(module.toString());
                addPersistInfo(moduleClass.newInstance().getConfig());
                Enumeration<String> enumer = (Enumeration<String>) props.propertyNames();
                while (enumer.hasMoreElements()) {
                    String propertyName = enumer.nextElement();
                    configuration.setProperty(propertyName, props.getProperty(propertyName));
                }
                IOs.close(is);
            }
        }
        // configuration.
        Stopwatch watch = new Stopwatch(true);

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

        configuration.setSessionFactoryObserver(new SessionFactoryObserver() {
            private static final long serialVersionUID = 1L;

            @Override
            public void sessionFactoryCreated(SessionFactory factory) {
            }

            @Override
            public void sessionFactoryClosed(SessionFactory factory) {
                ServiceRegistryBuilder.destroy(serviceRegistry);
            }
        });
        this.sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        logger.info("Building Hibernate SessionFactory in {}", watch);
    } finally {
    }
}

From source file:org.exoplatform.services.organization.idm.CustomHibernateServiceImpl.java

License:Open Source License

protected SessionFactory buildSessionFactory() {
    Configuration conf = getHibernateConfiguration();

    BootstrapServiceRegistry bootstrapRegistry = createHibernateBootstrapServiceRegistry();

    final ServiceRegistry serviceRegistry = new ServiceRegistryBuilder(bootstrapRegistry)
            .applySettings(conf.getProperties()).buildServiceRegistry();
    conf.setSessionFactoryObserver(new SessionFactoryObserver() {
        @Override/*from   w  w  w .  java 2  s .c  om*/
        public void sessionFactoryCreated(SessionFactory factory) {
        }

        @Override
        public void sessionFactoryClosed(SessionFactory factory) {
            ((StandardServiceRegistryImpl) serviceRegistry).destroy();
        }
    });

    final ClassLoader old = SecurityHelper.doPrivilegedAction(new PrivilegedAction<ClassLoader>() {
        public ClassLoader run() {
            return Thread.currentThread().getContextClassLoader();
        }
    });

    try {
        SecurityHelper.doPrivilegedAction(new PrivilegedAction<Void>() {
            public Void run() {
                DelegatingClassLoader cl = new DelegatingClassLoader(old,
                        org.picketlink.idm.api.IdentitySessionFactory.class.getClassLoader());
                Thread.currentThread().setContextClassLoader(cl);
                return null;
            }
        });
        return conf.buildSessionFactory(serviceRegistry);
    } finally {
        if (old != null) {
            SecurityHelper.doPrivilegedAction(new PrivilegedAction<Void>() {
                public Void run() {
                    Thread.currentThread().setContextClassLoader(old);
                    return null;
                }
            });
        }
    }
}

From source file:org.grails.orm.hibernate.cfg.GrailsAnnotationConfiguration.java

License:Apache License

@Override
public SessionFactory buildSessionFactory() throws HibernateException {

    // set the class loader to load Groovy classes

    // work around for HHH-2624
    Map<String, Type> empty = new HashMap<String, Type>();
    addFilterDefinition(new FilterDefinition("dynamicFilterEnabler", "1=1", empty));

    SessionFactory sessionFactory = null;

    ClassLoader appClassLoader = (ClassLoader) getProperties().get(AvailableSettings.APP_CLASSLOADER);
    Thread currentThread = Thread.currentThread();
    ClassLoader threadContextClassLoader = currentThread.getContextClassLoader();
    boolean overrideClassLoader = (appClassLoader != null && !appClassLoader.equals(threadContextClassLoader));
    if (overrideClassLoader) {
        currentThread.setContextClassLoader(appClassLoader);
    }//from  www.j  av a 2 s .  co m

    try {
        ConfigurationHelper.resolvePlaceHolders(getProperties());

        EventListenerIntegrator eventListenerIntegrator = new EventListenerIntegrator(hibernateEventListeners,
                eventListeners);
        BootstrapServiceRegistry bootstrapServiceRegistry = new BootstrapServiceRegistryBuilder()
                .with(eventListenerIntegrator).build();

        setSessionFactoryObserver(new SessionFactoryObserver() {
            private static final long serialVersionUID = 1;

            public void sessionFactoryCreated(SessionFactory factory) {
            }

            public void sessionFactoryClosed(SessionFactory factory) {
                ((ServiceRegistryImplementor) serviceRegistry).destroy();
            }
        });

        StandardServiceRegistryBuilder standardServiceRegistryBuilder = new StandardServiceRegistryBuilder(
                bootstrapServiceRegistry).applySettings(getProperties());
        sessionFactory = super.buildSessionFactory(standardServiceRegistryBuilder.build());
        serviceRegistry = ((SessionFactoryImplementor) sessionFactory).getServiceRegistry();
    } finally {
        if (overrideClassLoader) {
            currentThread.setContextClassLoader(threadContextClassLoader);
        }
    }

    return sessionFactory;
}