Java Code Examples for org.hibernate.ScrollableResults#close()
The following examples show how to use
org.hibernate.ScrollableResults#close() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CriteriaQueryTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 6 votes |
public void testScrollCriteria() { Session session = openSession(); Transaction t = session.beginTransaction(); Course course = new Course(); course.setCourseCode("HIB"); course.setDescription("Hibernate Training"); session.persist(course); session.flush(); session.clear(); ScrollableResults sr = session.createCriteria(Course.class).scroll(); assertTrue( sr.next() ); course = (Course) sr.get(0); assertNotNull(course); sr.close(); session.delete(course); t.commit(); session.close(); }
Example 2
Source File: AbstractHibernateDAO.java From Knowage-Server with GNU Affero General Public License v3.0 | 5 votes |
protected int getTotalNumber(Criteria criteria) { ScrollableResults results = criteria.scroll(); results.last(); int total = results.getRowNumber() + 1; results.close(); return total; }
Example 3
Source File: AggressiveReleaseTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 5 votes |
public void testSerializationFailsOnAfterStatementAggressiveReleaseWithOpenResources() throws Throwable { prepare(); Session s = getSessionUnderTest(); Silly silly = new Silly( "silly" ); s.save( silly ); // this should cause the CM to obtain a connection, and then release it s.flush(); // both scroll() and iterate() cause the batcher to hold on // to resources, which should make aggresive-release not release // the connection (and thus cause serialization to fail) ScrollableResults sr = s.createQuery( "from Silly" ).scroll(); try { SerializationHelper.serialize( s ); fail( "Serialization allowed on connected session; or aggressive release released connection with open resources" ); } catch( IllegalStateException e ) { // expected behavior } // Closing the ScrollableResults does currently force the batcher to // aggressively release the connection sr.close(); SerializationHelper.serialize( s ); s.delete( silly ); s.flush(); release( s ); done(); }
Example 4
Source File: AggressiveReleaseTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 5 votes |
public void testQueryScrolling() throws Throwable { prepare(); Session s = getSessionUnderTest(); Silly silly = new Silly( "silly" ); s.save( silly ); s.flush(); ScrollableResults sr = s.createQuery( "from Silly" ).scroll(); assertTrue( sr.next() ); Silly silly2 = ( Silly ) sr.get( 0 ); assertEquals( silly, silly2 ); sr.close(); sr = s.createQuery( "from Silly" ).scroll(); ScrollableResults sr2 = s.createQuery( "from Silly where name = 'silly'" ).scroll(); assertTrue( sr.next() ); assertEquals( silly, sr.get( 0 ) ); assertTrue( sr2.next() ); assertEquals( silly, sr2.get( 0 ) ); sr.close(); sr2.close(); s.delete( silly ); s.flush(); release( s ); done(); }
Example 5
Source File: IndexHelper.java From document-management-system with GNU General Public License v2.0 | 4 votes |
protected int doRebuildIndex() throws Exception { FullTextSession fullTextSession = (FullTextSession) entityManager.getDelegate(); fullTextSession.setFlushMode(org.hibernate.FlushMode.MANUAL); fullTextSession.setCacheMode(org.hibernate.CacheMode.IGNORE); fullTextSession.purgeAll(NodeDocumentVersion.class); fullTextSession.getSearchFactory().optimize(NodeDocumentVersion.class); String query = "select ndv from NodeDocumentVersion ndv"; ScrollableResults cursor = fullTextSession.createQuery(query).scroll(); cursor.last(); int count = cursor.getRowNumber() + 1; log.warn("Re-building Wine index for " + count + " objects."); if (count > 0) { int batchSize = 300; cursor.first(); // Reset to first result row int i = 0; while (true) { fullTextSession.index(cursor.get(0)); if (++i % batchSize == 0) { fullTextSession.flushToIndexes(); fullTextSession.clear(); // Clear persistence context for each batch log.info("Flushed index update " + i + " from Thread " + Thread.currentThread().getName()); } if (cursor.isLast()) { break; } cursor.next(); } } cursor.close(); fullTextSession.flushToIndexes(); fullTextSession.clear(); // Clear persistence context for each batch fullTextSession.getSearchFactory().optimize(NodeDocumentVersion.class); return count; }
Example 6
Source File: CMTTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 4 votes |
public void testCurrentSessionWithScroll() throws Exception { DummyTransactionManager.INSTANCE.begin(); Session s = getSessions().getCurrentSession(); Map item1 = new HashMap(); item1.put( "name", "Item - 1" ); item1.put( "description", "The first item" ); s.persist( "Item", item1 ); Map item2 = new HashMap(); item2.put( "name", "Item - 2" ); item2.put( "description", "The second item" ); s.persist( "Item", item2 ); DummyTransactionManager.INSTANCE.getTransaction().commit(); // First, test partially scrolling the result with out closing DummyTransactionManager.INSTANCE.begin(); s = getSessions().getCurrentSession(); ScrollableResults results = s.createQuery( "from Item" ).scroll(); results.next(); DummyTransactionManager.INSTANCE.getTransaction().commit(); // Next, test partially scrolling the result with closing DummyTransactionManager.INSTANCE.begin(); s = getSessions().getCurrentSession(); results = s.createQuery( "from Item" ).scroll(); results.next(); results.close(); DummyTransactionManager.INSTANCE.getTransaction().commit(); // Next, scroll the entire result (w/o closing) DummyTransactionManager.INSTANCE.begin(); s = getSessions().getCurrentSession(); results = s.createQuery( "from Item" ).scroll(); while ( !results.isLast() ) { results.next(); } DummyTransactionManager.INSTANCE.getTransaction().commit(); // Next, scroll the entire result (closing) DummyTransactionManager.INSTANCE.begin(); s = getSessions().getCurrentSession(); results = s.createQuery( "from Item" ).scroll(); while ( !results.isLast() ) { results.next(); } results.close(); DummyTransactionManager.INSTANCE.getTransaction().commit(); DummyTransactionManager.INSTANCE.begin(); s = getSessions().getCurrentSession(); s.createQuery( "delete from Item" ).executeUpdate(); DummyTransactionManager.INSTANCE.getTransaction().commit(); }
Example 7
Source File: StatsTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 4 votes |
public void testQueryStatGathering() { Statistics stats = getSessions().getStatistics(); stats.clear(); Session s = openSession(); Transaction tx = s.beginTransaction(); fillDb(s); tx.commit(); s.close(); s = openSession(); tx = s.beginTransaction(); final String continents = "from Continent"; int results = s.createQuery( continents ).list().size(); QueryStatistics continentStats = stats.getQueryStatistics( continents ); assertNotNull( "stats were null", continentStats ); assertEquals( "unexpected execution count", 1, continentStats.getExecutionCount() ); assertEquals( "unexpected row count", results, continentStats.getExecutionRowCount() ); long maxTime = continentStats.getExecutionMaxTime(); assertEquals( maxTime, stats.getQueryExecutionMaxTime() ); // assertEquals( continents, stats.getQueryExecutionMaxTimeQueryString() ); Iterator itr = s.createQuery( continents ).iterate(); // iterate() should increment the execution count assertEquals( "unexpected execution count", 2, continentStats.getExecutionCount() ); // but should not effect the cumulative row count assertEquals( "unexpected row count", results, continentStats.getExecutionRowCount() ); Hibernate.close( itr ); ScrollableResults scrollableResults = s.createQuery( continents ).scroll(); // same deal with scroll()... assertEquals( "unexpected execution count", 3, continentStats.getExecutionCount() ); assertEquals( "unexpected row count", results, continentStats.getExecutionRowCount() ); scrollableResults.close(); tx.commit(); s.close(); // explicitly check that statistics for "split queries" get collected // under the original query stats.clear(); s = openSession(); tx = s.beginTransaction(); final String localities = "from Locality"; results = s.createQuery( localities ).list().size(); QueryStatistics localityStats = stats.getQueryStatistics( localities ); assertNotNull( "stats were null", localityStats ); // ...one for each split query assertEquals( "unexpected execution count", 2, localityStats.getExecutionCount() ); assertEquals( "unexpected row count", results, localityStats.getExecutionRowCount() ); maxTime = localityStats.getExecutionMaxTime(); assertEquals( maxTime, stats.getQueryExecutionMaxTime() ); // assertEquals( localities, stats.getQueryExecutionMaxTimeQueryString() ); tx.commit(); s.close(); assertFalse( s.isOpen() ); // native sql queries stats.clear(); s = openSession(); tx = s.beginTransaction(); final String sql = "select id, name from Country"; results = s.createSQLQuery( sql ).addEntity( Country.class ).list().size(); QueryStatistics sqlStats = stats.getQueryStatistics( sql ); assertNotNull( "sql stats were null", sqlStats ); assertEquals( "unexpected execution count", 1, sqlStats.getExecutionCount() ); assertEquals( "unexpected row count", results, sqlStats.getExecutionRowCount() ); maxTime = sqlStats.getExecutionMaxTime(); assertEquals( maxTime, stats.getQueryExecutionMaxTime() ); // assertEquals( sql, stats.getQueryExecutionMaxTimeQueryString() ); tx.commit(); s.close(); s = openSession(); tx = s.beginTransaction(); cleanDb( s ); tx.commit(); s.close(); }
Example 8
Source File: StatelessSessionTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 4 votes |
public void testCreateUpdateReadDelete() { StatelessSession ss = getSessions().openStatelessSession(); Transaction tx = ss.beginTransaction(); Document doc = new Document("blah blah blah", "Blahs"); ss.insert(doc); assertNotNull( doc.getName() ); Date initVersion = doc.getLastModified(); assertNotNull( initVersion ); tx.commit(); tx = ss.beginTransaction(); doc.setText("blah blah blah .... blah"); ss.update(doc); assertNotNull( doc.getLastModified() ); assertNotSame( doc.getLastModified(), initVersion ); tx.commit(); tx = ss.beginTransaction(); doc.setText("blah blah blah .... blah blay"); ss.update(doc); tx.commit(); Document doc2 = (Document) ss.get(Document.class.getName(), "Blahs"); assertEquals("Blahs", doc2.getName()); assertEquals(doc.getText(), doc2.getText()); doc2 = (Document) ss.createQuery("from Document where text is not null").uniqueResult(); assertEquals("Blahs", doc2.getName()); assertEquals(doc.getText(), doc2.getText()); ScrollableResults sr = ss.createQuery("from Document where text is not null") .scroll(ScrollMode.FORWARD_ONLY); sr.next(); doc2 = (Document) sr.get(0); sr.close(); assertEquals("Blahs", doc2.getName()); assertEquals(doc.getText(), doc2.getText()); doc2 = (Document) ss.createSQLQuery("select * from Document") .addEntity(Document.class) .uniqueResult(); assertEquals("Blahs", doc2.getName()); assertEquals(doc.getText(), doc2.getText()); doc2 = (Document) ss.createCriteria(Document.class).uniqueResult(); assertEquals("Blahs", doc2.getName()); assertEquals(doc.getText(), doc2.getText()); sr = ss.createCriteria(Document.class).scroll(ScrollMode.FORWARD_ONLY); sr.next(); doc2 = (Document) sr.get(0); sr.close(); assertEquals("Blahs", doc2.getName()); assertEquals(doc.getText(), doc2.getText()); tx = ss.beginTransaction(); ss.delete(doc); tx.commit(); ss.close(); }
Example 9
Source File: ASTParserLoadingTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 4 votes |
public void testDynamicInstantiationQueries() throws Exception { createTestBaseData(); Session session = openSession(); List results = session.createQuery( "select new Animal(an.description, an.bodyWeight) from Animal an" ).list(); assertEquals( "Incorrect result size", 2, results.size() ); assertClassAssignability( results.get( 0 ).getClass(), Animal.class ); Iterator iter = session.createQuery( "select new Animal(an.description, an.bodyWeight) from Animal an" ).iterate(); assertTrue( "Incorrect result size", iter.hasNext() ); assertTrue( "Incorrect return type", iter.next() instanceof Animal ); results = session.createQuery( "select new list(an.description, an.bodyWeight) from Animal an" ).list(); assertEquals( "Incorrect result size", 2, results.size() ); assertTrue( "Incorrect return type", results.get( 0 ) instanceof List ); assertEquals( "Incorrect return type", ( (List) results.get( 0 ) ).size(), 2 ); results = session.createQuery( "select new list(an.description, an.bodyWeight) from Animal an" ).list(); assertEquals( "Incorrect result size", 2, results.size() ); assertTrue( "Incorrect return type", results.get( 0 ) instanceof List ); assertEquals( "Incorrect return type", ( (List) results.get( 0 ) ).size(), 2 ); iter = session.createQuery( "select new list(an.description, an.bodyWeight) from Animal an" ).iterate(); assertTrue( "Incorrect result size", iter.hasNext() ); Object obj = iter.next(); assertTrue( "Incorrect return type", obj instanceof List ); assertEquals( "Incorrect return type", ( (List) obj ).size(), 2 ); iter = ((org.hibernate.classic.Session)session).iterate( "select new list(an.description, an.bodyWeight) from Animal an" ); assertTrue( "Incorrect result size", iter.hasNext() ); obj = iter.next(); assertTrue( "Incorrect return type", obj instanceof List ); assertEquals( "Incorrect return type", ( (List) obj ).size(), 2 ); results = session.createQuery( "select new map(an.description, an.bodyWeight) from Animal an" ).list(); assertEquals( "Incorrect result size", 2, results.size() ); assertTrue( "Incorrect return type", results.get( 0 ) instanceof Map ); assertEquals( "Incorrect return type", ( (Map) results.get( 0 ) ).size(), 2 ); assertTrue( ( (Map) results.get( 0 ) ).containsKey("0") ); assertTrue( ( (Map) results.get( 0 ) ).containsKey("1") ); results = session.createQuery( "select new map(an.description as descr, an.bodyWeight as bw) from Animal an" ).list(); assertEquals( "Incorrect result size", 2, results.size() ); assertTrue( "Incorrect return type", results.get( 0 ) instanceof Map ); assertEquals( "Incorrect return type", ( (Map) results.get( 0 ) ).size(), 2 ); assertTrue( ( (Map) results.get( 0 ) ).containsKey("descr") ); assertTrue( ( (Map) results.get( 0 ) ).containsKey("bw") ); iter = session.createQuery( "select new map(an.description, an.bodyWeight) from Animal an" ).iterate(); assertTrue( "Incorrect result size", iter.hasNext() ); obj = iter.next(); assertTrue( "Incorrect return type", obj instanceof Map ); assertEquals( "Incorrect return type", ( (Map) obj ).size(), 2 ); ScrollableResults sr = session.createQuery( "select new map(an.description, an.bodyWeight) from Animal an" ).scroll(); assertTrue( "Incorrect result size", sr.next() ); obj = sr.get(0); assertTrue( "Incorrect return type", obj instanceof Map ); assertEquals( "Incorrect return type", ( (Map) obj ).size(), 2 ); sr.close(); sr = session.createQuery( "select new Animal(an.description, an.bodyWeight) from Animal an" ).scroll(); assertTrue( "Incorrect result size", sr.next() ); assertTrue( "Incorrect return type", sr.get(0) instanceof Animal ); sr.close(); // caching... QueryStatistics stats = getSessions().getStatistics().getQueryStatistics( "select new Animal(an.description, an.bodyWeight) from Animal an" ); results = session.createQuery( "select new Animal(an.description, an.bodyWeight) from Animal an" ) .setCacheable( true ) .list(); assertEquals( "incorrect result size", 2, results.size() ); assertClassAssignability( Animal.class, results.get( 0 ).getClass() ); long initCacheHits = stats.getCacheHitCount(); results = session.createQuery( "select new Animal(an.description, an.bodyWeight) from Animal an" ) .setCacheable( true ) .list(); assertEquals( "dynamic intantiation query not served from cache", initCacheHits + 1, stats.getCacheHitCount() ); assertEquals( "incorrect result size", 2, results.size() ); assertClassAssignability( Animal.class, results.get( 0 ).getClass() ); session.close(); destroyTestBaseData(); }
Example 10
Source File: ASTParserLoadingTest.java From cacheonix-core with GNU Lesser General Public License v2.1 | 4 votes |
public void testResultTransformerScalarQueries() throws Exception { createTestBaseData(); String query = "select an.description as description, an.bodyWeight as bodyWeight from Animal an order by bodyWeight desc"; Session session = openSession(); List results = session.createQuery( query ) .setResultTransformer(Transformers.aliasToBean(Animal.class)).list(); assertEquals( "Incorrect result size", results.size(), 2 ); assertTrue( "Incorrect return type", results.get(0) instanceof Animal ); Animal firstAnimal = (Animal) results.get(0); Animal secondAnimal = (Animal) results.get(1); assertEquals("Mammal #1", firstAnimal.getDescription()); assertEquals("Mammal #2", secondAnimal.getDescription()); assertFalse(session.contains(firstAnimal)); session.close(); session = openSession(); Iterator iter = session.createQuery( query ) .setResultTransformer(Transformers.aliasToBean(Animal.class)).iterate(); assertTrue( "Incorrect result size", iter.hasNext() ); assertTrue( "Incorrect return type", iter.next() instanceof Animal ); session.close(); session = openSession(); ScrollableResults sr = session.createQuery( query ) .setResultTransformer(Transformers.aliasToBean(Animal.class)).scroll(); assertTrue( "Incorrect result size", sr.next() ); assertTrue( "Incorrect return type", sr.get(0) instanceof Animal ); assertFalse(session.contains(sr.get(0))); sr.close(); session.close(); session = openSession(); results = session.createQuery( "select a from Animal a, Animal b order by a.id" ) .setResultTransformer(new DistinctRootEntityResultTransformer()) .list(); assertEquals( "Incorrect result size", 2, results.size()); assertTrue( "Incorrect return type", results.get(0) instanceof Animal ); firstAnimal = (Animal) results.get(0); secondAnimal = (Animal) results.get(1); assertEquals("Mammal #1", firstAnimal.getDescription()); assertEquals("Mammal #2", secondAnimal.getDescription()); session.close(); destroyTestBaseData(); }