Spring Data JPA に変換したばかりのプロジェクトがあります。このプロジェクトは Hibernate Search を使用しており、データベース内の既存 (~1,500 万) のレコードにインデックスを付ける方法が必要です。
非常に多くのレコードを扱っているため、メモリ不足の問題が発生するため、Hibernate Search の MassIndexer を使用できません。
私が読んだことから (ここ: http://docs.jboss.org/hibernate/search/4.2/reference/en-US/html/manual-index-changes.html#search-batchindex )、推奨される方法これは次のようなものです。
fullTextSession.setFlushMode(FlushMode.MANUAL);
fullTextSession.setCacheMode(CacheMode.IGNORE);
transaction = fullTextSession.beginTransaction();
//Scrollable results will avoid loading too many objects in memory
ScrollableResults results = fullTextSession.createCriteria( Email.class )
.setFetchSize(BATCH_SIZE)
.scroll( ScrollMode.FORWARD_ONLY );
int index = 0;
while( results.next() ) {
index++;
fullTextSession.index( results.get(0) ); //index each element
if (index % BATCH_SIZE == 0) {
fullTextSession.flushToIndexes(); //apply changes to indexes
fullTextSession.clear(); //free memory since the queue is processed
}
}
transaction.commit();
ただし、Spring で構成したエンティティ マネージャーを注入したいと考えています。
エンティティ マネージャーのメソッドを使用して Hibernate セッションを取得できることを読みましたがgetDelegate()
、セッションにプロパティを設定しようとするとすぐに Hibernate セッションが閉じられるというエラーが発生します。
public void reindexListings() throws InterruptedException {
Session session = (Session) em.getDelegate();
FullTextSession fts = Search.getFullTextSession(session);
try {
fts.setFlushMode(FlushMode.MANUAL);
} catch (Exception e) {
// Throws stack trace here stating that the Hibernate session is closed.
e.printStackTrace();
}
fts.setCacheMode(CacheMode.IGNORE);
Transaction transaction = fts.beginTransaction();
// Scrollable results will avoid loading too many objects in memory
ScrollableResults results = fts.createCriteria(EListing.class)
.setFetchSize(25).scroll(ScrollMode.FORWARD_ONLY);
int index = 0;
while (results.next()) {
index++;
fts.index(results.get(0)); // index each element
if ((index % 25) == 0) {
fts.flushToIndexes(); // apply changes to indexes
fts.clear(); // free memory since the queue is processed
}
}
transaction.commit();
}
HibernateUtil
セッションを取得するために使用できることも読みました( http://www.17od.com/2006/11/06/using-managed-sessions-in-hibernate-to-ease-unit-testing/)、繰り返しますが、これは私のエンティティマネージャーを利用していません。
これまでのところ正しい軌道に乗っているのか、それともまったく別の方法でこれを行う必要があるのか はわかりませんが、これまでに見つかったものは何もないようです.