Skip to content

Instantly share code, notes, and snippets.

@smallufo
Created March 27, 2015 09:41
Show Gist options
  • Save smallufo/11b3db0d93f032b39cd8 to your computer and use it in GitHub Desktop.
Save smallufo/11b3db0d93f032b39cd8 to your computer and use it in GitHub Desktop.
rxjava scroll database example by hibernate. (pity , JPA doesn't support scroll)
public Observable<Data> toObservable(int batch) {
return Observable.create(new Observable.OnSubscribe<Data>() {
@Override
public void call(Subscriber<? super Data> subscriber) {
Session session = ((Session) emp.get().getDelegate()).getSessionFactory().openSession();
Query query = session.createQuery("select d from Data d order by id asc");
query.setFetchSize(batch);
query.setReadOnly(true);
ScrollableResults results = query.scroll(ScrollMode.FORWARD_ONLY);
while (results.next()) {
Data data = (Data) results.get(0);
subscriber.onNext(data);
}
results.close();
session.close();
if (!subscriber.isUnsubscribed()) {
subscriber.onCompleted();
}
}
});
}
@trevershick
Copy link

Shouldn't you check for (results.next() && !subscriber.isUnsubscribed()) ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment