Example usage for org.hibernate SessionFactory openStatelessSession

List of usage examples for org.hibernate SessionFactory openStatelessSession

Introduction

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

Prototype

StatelessSession openStatelessSession();

Source Link

Document

Open a new stateless session.

Usage

From source file:com.trifork.stamdata.persistence.PersistenceModule.java

License:Mozilla Public License

@Provides
protected StatelessSession provideStatelessSession(SessionFactory factory) {
    return factory.openStatelessSession();
}

From source file:debop4k.data.orm.hibernate.StatelessEx.java

License:Apache License

public static StatelessSessionImpl of(@NonNull SessionFactory factory) {
    return (StatelessSessionImpl) factory.openStatelessSession();
}

From source file:org.dcm4chee.archive.ejb.query.IANQueryBean.java

License:LGPL

@PostConstruct
public void init() {
    SessionFactory sessionFactory = ((HibernateEntityManagerFactory) emf).getSessionFactory();
    session = sessionFactory.openStatelessSession();
}

From source file:org.dcm4chee.archive.ejb.query.LocateInstancesBean.java

License:LGPL

@PostConstruct
public void init() {
    SessionFactory sessionFactory = ((HibernateEntityManagerFactory) emf).getSessionFactory();
    session = sessionFactory.openStatelessSession();
    seriesQuery = session.createQuery(QUERY_SERIES_ATTRS);
}

From source file:org.jahia.modules.external.id.ExternalProviderInitializerServiceImpl.java

License:Open Source License

@Override
public void removeProvider(String providerKey) throws RepositoryException {
    SessionFactory hibernateSession = getHibernateSessionFactory();
    StatelessSession session = null;//from w w w.  j a  va  2s .  com
    try {
        session = hibernateSession.openStatelessSession();
        session.beginTransaction();
        int deletedCount = session.createQuery("delete from ExternalProviderID where providerKey=:providerKey")
                .setString("providerKey", providerKey).executeUpdate();
        if (deletedCount > 0) {
            logger.info("Deleted external provider entry for key {}", providerKey);
            deletedCount = session.createQuery("delete from UuidMapping where providerKey=:providerKey")
                    .setString("providerKey", providerKey).executeUpdate();
            logger.info("Deleted {} identifier mapping entries for external provider with key {}", deletedCount,
                    providerKey);
        } else {
            logger.info("No external provider entry found for key {}", providerKey);
        }
        session.getTransaction().commit();
    } catch (Exception e) {
        if (session != null) {
            session.getTransaction().rollback();
        }
        throw new RepositoryException(
                "Issue when removing external provider entry and identifier mappings for provider key "
                        + providerKey,
                e);
    } finally {
        if (session != null) {
            session.close();
        }
    }
}

From source file:org.jahia.services.content.impl.external.ExternalSessionImpl.java

License:Open Source License

public Node getNodeByUUID(String uuid) throws ItemNotFoundException, RepositoryException {
    if (!repository.getDataSource().isSupportsUuid() || uuid.startsWith("translation:")) {
        // Translate uuid to external mapping
        SessionFactory hibernateSession = repository.getStoreProvider().getHibernateSession();
        StatelessSession statelessSession = hibernateSession.openStatelessSession();
        try {//  w  w w. jav a2 s .c  o m
            Criteria criteria = statelessSession.createCriteria(UuidMapping.class);
            criteria.add(Restrictions.eq("internalUuid", uuid));
            List list = criteria.list();
            if (list.size() > 0) {
                uuid = ((UuidMapping) list.get(0)).getExternalId();
            } else {
                throw new ItemNotFoundException("Item " + uuid + " could not been found in this repository");
            }
        } finally {
            statelessSession.close();
        }
    }
    return getNodeByLocalIdentifier(uuid);
}

From source file:org.jboss.capedwarf.server.jee.persistence.HibernateStatelessAdapterFactory.java

License:Open Source License

protected StatelessAdapter doCreateStatelessAdapter(EntityManager em) {
    Object delegate = em.getDelegate();
    if (delegate instanceof Session == false)
        throw new IllegalArgumentException("Can only handle Hibernate Session: " + delegate);

    Session session = (Session) delegate;
    SessionFactory factory = session.getSessionFactory();
    return new HibernateStatelessAdapter(factory.openStatelessSession());
}

From source file:org.rhq.server.metrics.migrator.workers.AggregateDataMigratorTest.java

License:Open Source License

@Test
public void testEstimateTask() throws Exception {
    //tell the method story as it happens: mock or create dependencies and configure
    //those dependencies to get the method under test to completion
    DatabaseType databaseType = DatabaseType.Oracle;
    DataMigratorConfiguration mockConfig = mock(DataMigratorConfiguration.class);
    when(mockConfig.getDatabaseType()).thenReturn(databaseType);

    MetricsIndexMigrator mockMetricsIndexUpdateAccumulator = mock(MetricsIndexMigrator.class);
    PowerMockito.whenNew(MetricsIndexMigrator.class).withArguments(eq(MigrationTable.ONE_HOUR), eq(mockConfig))
            .thenReturn(mockMetricsIndexUpdateAccumulator);

    EntityManager mockEntityManager = mock(EntityManager.class);
    when(mockConfig.getEntityManager()).thenReturn(mockEntityManager);

    org.hibernate.Session mockHibernateSession = mock(org.hibernate.Session.class);
    when(mockEntityManager.getDelegate()).thenReturn(mockHibernateSession);
    SessionFactory mockSessionFactory = mock(SessionFactory.class);
    when(mockHibernateSession.getSessionFactory()).thenReturn(mockSessionFactory);
    StatelessSession mockStatelessSession = mock(StatelessSession.class);
    when(mockSessionFactory.openStatelessSession()).thenReturn(mockStatelessSession);

    org.hibernate.SQLQuery mockQuery = mock(org.hibernate.SQLQuery.class);
    when(mockStatelessSession.createSQLQuery(any(String.class))).thenReturn(mockQuery);

    when(mockQuery.uniqueResult()).thenReturn("1000");

    ScrollableDataSource mockDataSource = mock(ScrollableDataSource.class);
    PowerMockito.whenNew(ScrollableDataSource.class)
            .withArguments(eq(mockEntityManager), eq(databaseType), any(), anyInt()).thenReturn(mockDataSource);
    when(mockDataSource.getData(eq(0), anyInt())).thenReturn(new ArrayList<Object[]>());

    //create object to test and inject required dependencies
    AggregateDataMigrator objectUnderTest = new AggregateDataMigrator(MigrationTable.ONE_HOUR, mockConfig);

    //run code under test
    long estimateActual = objectUnderTest.estimate();

    //verify the results (assert and mock verification)
    PowerMockito.verifyNew(MetricsIndexMigrator.class).withArguments(eq(MigrationTable.ONE_HOUR),
            eq(mockConfig));/*from  w  w w  .  j  a v a  2s.  c om*/
    PowerMockito.verifyNew(ScrollableDataSource.class, times(1)).withArguments(eq(mockEntityManager),
            eq(databaseType), any(), anyInt());

    verify(mockStatelessSession, times(1)).createSQLQuery(any(String.class));
    verify(mockDataSource, times(1)).initialize();
    verify(mockDataSource, times(1)).getData(eq(0), anyInt());
    verify(mockDataSource, times(1)).close();
    verify(mockMetricsIndexUpdateAccumulator, times(1)).drain();

    verifyNoMoreInteractions(mockDataSource);
    verifyNoMoreInteractions(mockMetricsIndexUpdateAccumulator);

    Assert.assertNotEquals(estimateActual, 0);
}

From source file:org.rhq.server.metrics.migrator.workers.AggregateDataMigratorTest.java

License:Open Source License

@Test
public void testMigrateTask() throws Exception {
    //tell the method story as it happens: mock or create dependencies and configure
    //those dependencies to get the method under test to completion
    DatabaseType databaseType = DatabaseType.Oracle;
    DataMigratorConfiguration mockConfig = mock(DataMigratorConfiguration.class);
    when(mockConfig.getDatabaseType()).thenReturn(databaseType);

    Session mockCassandraSession = mock(Session.class);
    when(mockConfig.getSession()).thenReturn(mockCassandraSession);

    MetricsIndexMigrator mockMetricsIndexUpdateAccumulator = mock(MetricsIndexMigrator.class);
    PowerMockito.whenNew(MetricsIndexMigrator.class).withArguments(eq(MigrationTable.ONE_HOUR), eq(mockConfig))
            .thenReturn(mockMetricsIndexUpdateAccumulator);

    EntityManager mockEntityManager = mock(EntityManager.class);
    when(mockConfig.getEntityManager()).thenReturn(mockEntityManager);

    org.hibernate.Session mockHibernateSession = mock(org.hibernate.Session.class);
    when(mockEntityManager.getDelegate()).thenReturn(mockHibernateSession);
    SessionFactory mockSessionFactory = mock(SessionFactory.class);
    when(mockHibernateSession.getSessionFactory()).thenReturn(mockSessionFactory);
    StatelessSession mockStatelessSession = mock(StatelessSession.class);
    when(mockSessionFactory.openStatelessSession()).thenReturn(mockStatelessSession);

    org.hibernate.SQLQuery mockQuery = mock(org.hibernate.SQLQuery.class);
    when(mockStatelessSession.createSQLQuery(any(String.class))).thenReturn(mockQuery);

    when(mockQuery.uniqueResult()).thenReturn("1000");

    ScrollableDataSource mockDataSource = mock(ScrollableDataSource.class);
    PowerMockito.whenNew(ScrollableDataSource.class)
            .withArguments(eq(mockEntityManager), eq(databaseType), any()).thenReturn(mockDataSource);

    List<Object[]> resultList = new ArrayList<Object[]>();
    resultList.add(new Object[] { 100, 100, 100, 100, 100 });
    resultList.add(new Object[] { 100, System.currentTimeMillis() - 100l, 100, 100, 100 });

    for (int index = 0; index < 15; index++) {
        when(mockDataSource.getData(eq(0), anyInt())).thenReturn(resultList);
        when(mockDataSource.getData(eq(2), anyInt())).thenReturn(new ArrayList<Object[]>());
    }//w ww  .  ja v  a  2s. com

    ResultSetFuture mockResultSetFuture = mock(ResultSetFuture.class);
    when(mockCassandraSession.executeAsync(any(Query.class))).thenReturn(mockResultSetFuture);

    //create object to test and inject required dependencies
    AggregateDataMigrator objectUnderTest = new AggregateDataMigrator(MigrationTable.ONE_HOUR, mockConfig);

    //run code under test
    objectUnderTest.migrate();

    //verify the results (assert and mock verification)
    PowerMockito.verifyNew(MetricsIndexMigrator.class).withArguments(eq(MigrationTable.ONE_HOUR),
            eq(mockConfig));
    PowerMockito.verifyNew(ScrollableDataSource.class, times(1)).withArguments(eq(mockEntityManager),
            eq(databaseType), any());

    verify(mockDataSource, times(1)).initialize();
    verify(mockDataSource, times(1)).getData(eq(0), anyInt());
    verify(mockDataSource, times(1)).getData(eq(2), anyInt());
    verify(mockDataSource, times(1)).close();

    verify(mockMetricsIndexUpdateAccumulator, times(1)).add(eq(100), anyInt());
    verify(mockMetricsIndexUpdateAccumulator, times(1)).drain();

    verify(mockCassandraSession, times(1)).executeAsync(any(Query.class));
    verify(mockResultSetFuture, times(1)).get();

    verifyNoMoreInteractions(mockDataSource);
    verifyNoMoreInteractions(mockCassandraSession);
    verifyNoMoreInteractions(mockResultSetFuture);
    verifyNoMoreInteractions(mockMetricsIndexUpdateAccumulator);
}

From source file:org.rhq.server.metrics.migrator.workers.RawDataMigratorTest.java

License:Open Source License

@Test
public void testEstimateTask() throws Exception {
    //tell the method story as it happens: mock or create dependencies and configure
    //those dependencies to get the method under test to completion
    DatabaseType databaseType = DatabaseType.Oracle;
    DataMigratorConfiguration mockConfig = mock(DataMigratorConfiguration.class);
    when(mockConfig.getDatabaseType()).thenReturn(databaseType);

    MetricsIndexMigrator mockMetricsIndexUpdateAccumulator = mock(MetricsIndexMigrator.class);
    PowerMockito.whenNew(MetricsIndexMigrator.class).withArguments(eq(MigrationTable.RAW), eq(mockConfig))
            .thenReturn(mockMetricsIndexUpdateAccumulator);

    EntityManager mockEntityManager = mock(EntityManager.class);
    when(mockConfig.getEntityManager()).thenReturn(mockEntityManager);

    org.hibernate.Session mockHibernateSession = mock(org.hibernate.Session.class);
    when(mockEntityManager.getDelegate()).thenReturn(mockHibernateSession);
    SessionFactory mockSessionFactory = mock(SessionFactory.class);
    when(mockHibernateSession.getSessionFactory()).thenReturn(mockSessionFactory);
    StatelessSession mockStatelessSession = mock(StatelessSession.class);
    when(mockSessionFactory.openStatelessSession()).thenReturn(mockStatelessSession);

    org.hibernate.SQLQuery mockQuery = mock(org.hibernate.SQLQuery.class);
    when(mockStatelessSession.createSQLQuery(any(String.class))).thenReturn(mockQuery);

    when(mockQuery.uniqueResult()).thenReturn("1000");

    ScrollableDataSource mockDataSource = mock(ScrollableDataSource.class);
    PowerMockito.whenNew(ScrollableDataSource.class)
            .withArguments(eq(mockEntityManager), eq(databaseType), any(), anyInt()).thenReturn(mockDataSource);
    when(mockDataSource.getData(eq(0), anyInt())).thenReturn(new ArrayList<Object[]>());

    //create object to test and inject required dependencies
    RawDataMigrator objectUnderTest = new RawDataMigrator(mockConfig);

    //run code under test
    long estimateActual = objectUnderTest.estimate();

    //verify the results (assert and mock verification)
    PowerMockito.verifyNew(MetricsIndexMigrator.class).withArguments(eq(MigrationTable.RAW), eq(mockConfig));
    PowerMockito.verifyNew(ScrollableDataSource.class, times(15)).withArguments(eq(mockEntityManager),
            eq(databaseType), any(), anyInt());

    verify(mockStatelessSession, times(15)).createSQLQuery(any(String.class));
    verify(mockDataSource, times(15)).initialize();
    verify(mockDataSource, times(15)).getData(eq(0), anyInt());
    verify(mockDataSource, times(15)).close();
    verify(mockMetricsIndexUpdateAccumulator, times(1)).drain();

    verifyNoMoreInteractions(mockDataSource);
    verifyNoMoreInteractions(mockMetricsIndexUpdateAccumulator);

    Assert.assertNotEquals(estimateActual, 0);
}