Created
March 27, 2015 09:41
-
-
Save smallufo/11b3db0d93f032b39cd8 to your computer and use it in GitHub Desktop.
rxjava scroll database example by hibernate. (pity , JPA doesn't support scroll)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Shouldn't you check for (results.next() && !subscriber.isUnsubscribed()) ?