use of org.hibernate.ScrollableResults in project querydsl by querydsl.
the class IntegrationBase method scroll.
@Test
public void scroll() {
session.save(new Cat("Bob", 10));
session.save(new Cat("Steve", 11));
QCat cat = QCat.cat;
HibernateQuery<?> query = new HibernateQuery<Void>(session);
ScrollableResults results = query.from(cat).select(cat).scroll(ScrollMode.SCROLL_INSENSITIVE);
while (results.next()) {
assertNotNull(results.get(0));
}
results.close();
}
use of org.hibernate.ScrollableResults in project hibernate-orm by hibernate.
the class HQLTest method test_hql_api_scroll_projection_example.
@Test
public void test_hql_api_scroll_projection_example() {
doInJPA(this::entityManagerFactory, entityManager -> {
Session session = entityManager.unwrap(Session.class);
try (ScrollableResults scrollableResults = session.createQuery("select p " + "from Person p " + "where p.name like :name").setParameter("name", "J%").scroll()) {
while (scrollableResults.next()) {
Person person = (Person) scrollableResults.get()[0];
process(person);
}
}
});
}
use of org.hibernate.ScrollableResults in project hibernate-orm by hibernate.
the class BatchTest method withScroll.
private void withScroll() {
withBatch();
//tag::batch-session-scroll-example[]
EntityManager entityManager = null;
EntityTransaction txn = null;
ScrollableResults scrollableResults = null;
try {
entityManager = entityManagerFactory().createEntityManager();
txn = entityManager.getTransaction();
txn.begin();
int batchSize = 25;
Session session = entityManager.unwrap(Session.class);
scrollableResults = session.createQuery("select p from Person p").setCacheMode(CacheMode.IGNORE).scroll(ScrollMode.FORWARD_ONLY);
int count = 0;
while (scrollableResults.next()) {
Person Person = (Person) scrollableResults.get(0);
processPerson(Person);
if (++count % batchSize == 0) {
//flush a batch of updates and release memory:
entityManager.flush();
entityManager.clear();
}
}
txn.commit();
} catch (RuntimeException e) {
if (txn != null && txn.isActive())
txn.rollback();
throw e;
} finally {
if (scrollableResults != null) {
scrollableResults.close();
}
if (entityManager != null) {
entityManager.close();
}
}
//end::batch-session-scroll-example[]
}
use of org.hibernate.ScrollableResults in project hibernate-orm by hibernate.
the class BatchTest method doBatchInsertUpdate.
public void doBatchInsertUpdate(int nEntities, int nBeforeFlush) {
Session s = openSession();
s.setCacheMode(CacheMode.IGNORE);
Transaction t = s.beginTransaction();
for (int i = 0; i < nEntities; i++) {
DataPoint dp = new DataPoint();
dp.setX(new BigDecimal(i * 0.1d).setScale(19, BigDecimal.ROUND_DOWN));
dp.setY(new BigDecimal(Math.cos(dp.getX().doubleValue())).setScale(19, BigDecimal.ROUND_DOWN));
s.save(dp);
if (i + 1 % nBeforeFlush == 0) {
s.flush();
s.clear();
}
}
t.commit();
s.close();
s = openSession();
s.setCacheMode(CacheMode.IGNORE);
t = s.beginTransaction();
int i = 0;
ScrollableResults sr = s.createQuery("from DataPoint dp order by dp.x asc").scroll(ScrollMode.FORWARD_ONLY);
while (sr.next()) {
DataPoint dp = (DataPoint) sr.get(0);
dp.setDescription("done!");
if (++i % nBeforeFlush == 0) {
s.flush();
s.clear();
}
}
t.commit();
s.close();
s = openSession();
s.setCacheMode(CacheMode.IGNORE);
t = s.beginTransaction();
i = 0;
sr = s.createQuery("from DataPoint dp order by dp.x asc").scroll(ScrollMode.FORWARD_ONLY);
while (sr.next()) {
DataPoint dp = (DataPoint) sr.get(0);
s.delete(dp);
if (++i % nBeforeFlush == 0) {
s.flush();
s.clear();
}
}
t.commit();
s.close();
}
use of org.hibernate.ScrollableResults in project hibernate-orm by hibernate.
the class AggressiveReleaseTest method testSerializationFailsOnAfterStatementAggressiveReleaseWithOpenResources.
@Test
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 batching to hold on
// to resources, which should make aggressive-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
}
// getting the first row only because SybaseASE15Dialect throws NullPointerException
// if data is not read beforeQuery closing the ResultSet
sr.next();
// Closing the ScrollableResults does currently force batching to
// aggressively release the connection
sr.close();
SerializationHelper.serialize(s);
s.delete(silly);
s.flush();
release(s);
done();
}
Aggregations