Search in sources :

Example 51 with SessionFactory

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

the class OpenSessionInterceptor method invoke.

@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
    SessionFactory sf = getSessionFactory();
    if (!TransactionSynchronizationManager.hasResource(sf)) {
        // New Session to be bound for the current method's scope...
        Session session = openSession();
        try {
            TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session));
            return invocation.proceed();
        } finally {
            SessionFactoryUtils.closeSession(session);
            TransactionSynchronizationManager.unbindResource(sf);
        }
    } else {
        // Pre-bound Session found -> simply proceed.
        return invocation.proceed();
    }
}
Also used : SessionFactory(org.hibernate.SessionFactory) SessionHolder(org.springframework.orm.hibernate5.SessionHolder) Session(org.hibernate.Session)

Example 52 with SessionFactory

use of org.hibernate.SessionFactory in project midpoint by Evolveum.

the class SqlRepositoryServiceImpl method readDetailsFromConnection.

private void readDetailsFromConnection(RepositoryDiag diag, final SqlRepositoryConfiguration config) {
    final List<LabeledString> details = diag.getAdditionalDetails();
    Session session = baseHelper.getSessionFactory().openSession();
    try {
        session.beginTransaction();
        session.doWork(new Work() {

            @Override
            public void execute(Connection connection) throws SQLException {
                details.add(new LabeledString(DETAILS_TRANSACTION_ISOLATION, getTransactionIsolation(connection, config)));
                Properties info = connection.getClientInfo();
                if (info == null) {
                    return;
                }
                for (String name : info.stringPropertyNames()) {
                    details.add(new LabeledString(DETAILS_CLIENT_INFO + name, info.getProperty(name)));
                }
            }
        });
        session.getTransaction().commit();
        SessionFactory sessionFactory = baseHelper.getSessionFactory();
        if (!(sessionFactory instanceof SessionFactoryImpl)) {
            return;
        }
        SessionFactoryImpl sessionFactoryImpl = (SessionFactoryImpl) sessionFactory;
        // we try to override configuration which was read from sql repo configuration with
        // real configuration from session factory
        String dialect = sessionFactoryImpl.getDialect() != null ? sessionFactoryImpl.getDialect().getClass().getName() : null;
        details.add(new LabeledString(DETAILS_HIBERNATE_DIALECT, dialect));
    } catch (Throwable th) {
        //nowhere to report error (no operation result available)
        session.getTransaction().rollback();
    } finally {
        baseHelper.cleanupSessionAndResult(session, null);
    }
}
Also used : SessionFactory(org.hibernate.SessionFactory) SQLException(java.sql.SQLException) Work(org.hibernate.jdbc.Work) Connection(java.sql.Connection) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) Properties(java.util.Properties) SessionFactoryImpl(org.hibernate.internal.SessionFactoryImpl) Session(org.hibernate.Session)

Example 53 with SessionFactory

use of org.hibernate.SessionFactory in project dhis2-core by dhis2.

the class DhisTest method bindSession.

/**
     * Binds a Hibernate Session to the current thread.
     */
private void bindSession() {
    SessionFactory sessionFactory = (SessionFactory) getBean("sessionFactory");
    Session session = sessionFactory.openSession();
    TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
}
Also used : SessionFactory(org.hibernate.SessionFactory) SessionHolder(org.springframework.orm.hibernate5.SessionHolder) Session(org.hibernate.Session)

Example 54 with SessionFactory

use of org.hibernate.SessionFactory in project uPortal by Jasig.

the class PortalRawEventsAggregatorImpl method evictAggregates.

@AggrEventsTransactional
@Override
public void evictAggregates(Map<Class<?>, Collection<Serializable>> entitiesToEvict) {
    int evictedEntities = 0;
    int evictedCollections = 0;
    final Session session = getEntityManager().unwrap(Session.class);
    final SessionFactory sessionFactory = session.getSessionFactory();
    final Cache cache = sessionFactory.getCache();
    for (final Entry<Class<?>, Collection<Serializable>> evictedEntityEntry : entitiesToEvict.entrySet()) {
        final Class<?> entityClass = evictedEntityEntry.getKey();
        final List<String> collectionRoles = getCollectionRoles(sessionFactory, entityClass);
        for (final Serializable id : evictedEntityEntry.getValue()) {
            cache.evictEntity(entityClass, id);
            evictedEntities++;
            for (final String collectionRole : collectionRoles) {
                cache.evictCollection(collectionRole, id);
                evictedCollections++;
            }
        }
    }
    logger.debug("Evicted {} entities and {} collections from hibernate caches", evictedEntities, evictedCollections);
}
Also used : SessionFactory(org.hibernate.SessionFactory) Serializable(java.io.Serializable) Collection(java.util.Collection) EventSession(org.apereo.portal.events.aggr.session.EventSession) Session(org.hibernate.Session) Cache(org.hibernate.Cache)

Example 55 with SessionFactory

use of org.hibernate.SessionFactory in project ignite by apache.

the class CacheHibernateStoreSessionListenerSelfTest method sessionListenerFactory.

/**
 * {@inheritDoc}
 */
@Override
protected Factory<CacheStoreSessionListener> sessionListenerFactory() {
    return new Factory<CacheStoreSessionListener>() {

        @Override
        public CacheStoreSessionListener create() {
            CacheHibernateStoreSessionListener lsnr = new CacheHibernateStoreSessionListener();
            SessionFactory sesFactory = new Configuration().setProperty("hibernate.connection.url", URL).addAnnotatedClass(Table1.class).addAnnotatedClass(Table2.class).buildSessionFactory();
            lsnr.setSessionFactory(sesFactory);
            return lsnr;
        }
    };
}
Also used : SessionFactory(org.hibernate.SessionFactory) Configuration(org.hibernate.cfg.Configuration) Factory(javax.cache.configuration.Factory) SessionFactory(org.hibernate.SessionFactory)

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