List of usage examples for org.hibernate StatelessSession createSQLQuery
@Override NativeQuery createSQLQuery(String queryString);
From source file:org.rhq.server.metrics.migrator.workers.DeleteAllData.java
License:Open Source License
public void migrate() { org.hibernate.Query nativeQuery; StatelessSession session = getSQLSession(config); if (config.isRun1HAggregateDataMigration()) { session.getTransaction().begin(); nativeQuery = session.createSQLQuery(MigrationQuery.DELETE_1H_DATA.toString()); nativeQuery.executeUpdate();// ww w . j a v a2 s. com session.getTransaction().commit(); log.info("- RHQ_MEASUREMENT_DATA_NUM_1H - Cleaned -"); } if (config.isRun6HAggregateDataMigration()) { session.getTransaction().begin(); nativeQuery = session.createSQLQuery(MigrationQuery.DELETE_6H_DATA.toString()); nativeQuery.executeUpdate(); session.getTransaction().commit(); log.info("- RHQ_MEASUREMENT_DATA_NUM_6H - Cleaned -"); } if (config.isRun1DAggregateDataMigration()) { session.getTransaction().begin(); nativeQuery = session.createSQLQuery(MigrationQuery.DELETE_1D_DATA.toString()); nativeQuery.executeUpdate(); session.getTransaction().commit(); log.info("- RHQ_MEASUREMENT_DATA_NUM_1D - Cleaned -"); } if (config.isRunRawDataMigration()) { for (String table : getRawDataTables()) { session.getTransaction().begin(); String deleteAllData = String.format(MigrationQuery.DELETE_RAW_ALL_DATA.toString(), table); nativeQuery = session.createSQLQuery(deleteAllData); nativeQuery.executeUpdate(); session.getTransaction().commit(); log.info("- " + table + " - Cleaned -"); } } closeSQLSession(session); }
From source file:org.rhq.server.metrics.migrator.workers.MetricsIndexMigrator.java
License:Open Source License
private long getLastAggregationTime(MigrationTable migratedTable) { StatelessSession session = getSQLSession(migratorConfiguration); long aggregationSlice = Integer.MAX_VALUE; Duration duration = null;/*from w w w.j av a 2 s. com*/ String queryString = null; if (MigrationTable.RAW.equals(migratedTable)) { duration = metricsConfiguration.getRawTimeSliceDuration(); queryString = MigrationQuery.MAX_TIMESTAMP_1H_DATA.toString(); } else if (MigrationTable.ONE_HOUR.equals(migratedTable)) { duration = metricsConfiguration.getOneHourTimeSliceDuration(); queryString = MigrationQuery.MAX_TIMESTAMP_6H_DATA.toString(); } else if (MigrationTable.SIX_HOUR.equals(migratedTable)) { duration = metricsConfiguration.getSixHourTimeSliceDuration(); queryString = MigrationQuery.MAX_TIMESTAMP_1D_DATA.toString(); } if (duration != null && queryString != null) { Query query = session.createSQLQuery(queryString); Long timeStamp; Object result = query.uniqueResult(); if (result != null) { String queryResult = query.uniqueResult().toString(); Long timestamp = Long.parseLong(queryResult); aggregationSlice = dateTimeService.getTimeSlice(new DateTime(timestamp), duration).getMillis(); } } closeSQLSession(session); return aggregationSlice; }
From source file:org.rhq.server.metrics.migrator.workers.MetricsIndexUpdateAccumulator.java
License:Open Source License
private long getLastAggregationTime(MetricsTable migratedTable) { StatelessSession session = getSQLSession(config); long aggregationSlice = Integer.MAX_VALUE; Duration duration = null;/*w ww . ja va 2s. c om*/ String queryString = null; if (MetricsTable.RAW.equals(migratedTable)) { duration = configuration.getRawTimeSliceDuration(); queryString = MigrationQuery.MAX_TIMESTAMP_1H_DATA.toString(); } else if (MetricsTable.ONE_HOUR.equals(migratedTable)) { duration = configuration.getOneHourTimeSliceDuration(); queryString = MigrationQuery.MAX_TIMESTAMP_6H_DATA.toString(); } else if (MetricsTable.SIX_HOUR.equals(migratedTable)) { duration = configuration.getSixHourTimeSliceDuration(); queryString = MigrationQuery.MAX_TIMESTAMP_1D_DATA.toString(); } if (duration != null && queryString != null) { Query query = session.createSQLQuery(queryString); Long timeStamp; Object result = query.uniqueResult(); if (result != null) { String queryResult = query.uniqueResult().toString(); Long timestamp = Long.parseLong(queryResult); aggregationSlice = dateTimeService.getTimeSlice(new DateTime(timestamp), duration).getMillis(); } } closeSQLSession(session); return aggregationSlice; }
From source file:org.rhq.server.metrics.migrator.workers.RawDataMigrator.java
License:Open Source License
private long getRowCount(String countQuery) { StatelessSession session = getSQLSession(config); org.hibernate.Query query = session.createSQLQuery(countQuery); query.setReadOnly(true);//from ww w . jav a 2s . c o m query.setTimeout(DataMigrator.SQL_TIMEOUT); long count = Long.parseLong(query.uniqueResult().toString()); closeSQLSession(session); return count; }
From source file:org.rhq.server.metrics.migrator.workers.RawDataMigrator.java
License:Open Source License
private void deleteTableData(String table) throws Exception { String deleteQuery = String.format(MigrationQuery.DELETE_RAW_ENTRY.toString(), table); int failureCount = 0; while (failureCount < MAX_NUMBER_OF_FAILURES) { try {/*from www . j a v a2 s . c o m*/ StatelessSession session = getSQLSession(config); session.getTransaction().begin(); org.hibernate.Query nativeQuery = session.createSQLQuery(deleteQuery); nativeQuery.executeUpdate(); session.getTransaction().commit(); closeSQLSession(session); log.info("- " + table + " - Cleaned -"); } catch (Exception e) { log.error("Failed to delete " + table + " data. Attempting to delete data one more time..."); failureCount++; if (failureCount == MAX_NUMBER_OF_FAILURES) { throw e; } } } }
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); }
From source file:org.rhq.server.metrics.migrator.workers.RawDataMigratorTest.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.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()).thenReturn(mockDataSource); List<Object[]> resultList = new ArrayList<Object[]>(); resultList.add(new Object[] { 100, 100, 100 }); resultList.add(new Object[] { 100, System.currentTimeMillis() - 100l, 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[]>()); }//from ww w . ja va 2 s .c o m ResultSetFuture mockResultSetFuture = mock(ResultSetFuture.class); when(mockCassandraSession.executeAsync(any(Query.class))).thenReturn(mockResultSetFuture); //create object to test and inject required dependencies RawDataMigrator objectUnderTest = new RawDataMigrator(mockConfig); //run code under test objectUnderTest.migrate(); //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()); verify(mockDataSource, times(15)).initialize(); verify(mockDataSource, times(15)).getData(eq(0), anyInt()); verify(mockDataSource, times(15)).getData(eq(2), anyInt()); verify(mockDataSource, times(15)).close(); verify(mockMetricsIndexUpdateAccumulator, times(15)).add(eq(100), anyInt()); verify(mockMetricsIndexUpdateAccumulator, times(1)).drain(); verify(mockCassandraSession, times(15)).executeAsync(any(Query.class)); verify(mockResultSetFuture, times(15)).get(); verifyNoMoreInteractions(mockDataSource); verifyNoMoreInteractions(mockCassandraSession); verifyNoMoreInteractions(mockResultSetFuture); verifyNoMoreInteractions(mockMetricsIndexUpdateAccumulator); }
From source file:org.springframework.batch.item.database.support.HibernateNativeQueryProviderTests.java
License:Apache License
@Test public void testCreateQueryWithStatelessSession() { String sqlQuery = "select * from T_FOOS"; hibernateQueryProvider.setSqlQuery(sqlQuery); StatelessSession session = mock(StatelessSession.class); SQLQuery query = mock(SQLQuery.class); when(session.createSQLQuery(sqlQuery)).thenReturn(query); when(query.addEntity(Foo.class)).thenReturn(query); hibernateQueryProvider.setStatelessSession(session); Assert.notNull(hibernateQueryProvider.createQuery()); }
From source file:org.squashtest.tm.service.internal.api.repository.HibernateSqlQueryRunner.java
License:Open Source License
private <T> T executeQuery(String selectQuery, QueryExecution<Query> execution) { SessionFactory sessionFactory = entityManagerFactory.unwrap(SessionFactory.class); StatelessSession s = sessionFactory.openStatelessSession(); Transaction tx = s.beginTransaction(); T res = null;//from w ww .j av a 2 s .c o m try { SQLQuery q = s.createSQLQuery(selectQuery); res = execution.<T>executeQuery(q); tx.commit(); } catch (HibernateException e) { tx.rollback(); throw e; } finally { s.close(); } return res; }
From source file:org.squashtest.tm.service.internal.campaign.coercers.TestSuiteToIterationCoercerForUniqueId.java
License:Open Source License
@Override public Serializable coerce(Object id) { StatelessSession s = hibernateStatelessSessionHelper.openStatelessSession(); Transaction tx = s.beginTransaction(); try {/* w w w . java 2 s.co m*/ Query q = s.createSQLQuery( "SELECT DISTINCT iteration_id FROM iteration_test_suite WHERE test_suite_id = :suiteId"); q.setParameter("suiteId", id); return (Serializable) q.uniqueResult(); } finally { tx.commit(); s.close(); } }