I have these code to rebuild an index. This code is copied from this forum almost verbatim:
Code:
Transaction userTx = null;
try {
log.debug("deleting indexed documents");
FullTextHibernateSessionProxy proxy = (FullTextHibernateSessionProxy) entityManager.getDelegate();
// EntityManager em = entityManager;//(EntityManager) Component.getInstance("entityManager");
// Session session = (Session) em.getDelegate();
userTx = proxy.beginTransaction();
// userTx.begin();
// Delete all documents with "_hibernate_class" term of the selected entity
DirectoryProvider dirProvider = proxy.getSearchFactory().getDirectoryProviders(entityClass)[0];
IndexReader reader = IndexReader.open(dirProvider.getDirectory());
// TODO: This is using an internal term of HSearch
reader.deleteDocuments(new Term("_hibernate_class", entityClass.getName()));
reader.close();
// Optimize index
log.debug("optimizing index (merging segments)");
proxy.getSearchFactory().optimize(entityClass);
userTx.commit();
log.debug("indexing documents in batches of: " + batchSize);
// Now re-index with HSearch
proxy = (FullTextHibernateSessionProxy) entityManager.getDelegate();
userTx = proxy.beginTransaction();
// userTx.begin();
// Use HQL instead of Criteria to eager fetch lazy properties
ScrollableResults cursor = proxy
.createQuery("select o from " + entityClass.getName() + " o where status=:status")
.setParameter("status", Constants.Status.ACTIVE).scroll();
cursor.last();
int count = cursor.getRowNumber() + 1; // rowNumber will be -1 if there is no row
cursor.first(); // Reset to first result row
int i = 0;
while (true && count > 0) {
i++;
Object o = cursor.get(0);
log.debug("indexing: " + i);
proxy.index(o);
if (i % batchSize == 0) proxy.clear(); // Clear persistence context for each batch
if (cursor.isLast())
break;
else
cursor.next();
}
cursor.close();
userTx.commit();
log.debug("indexing complete of entity class: " + entityClass);
} catch (Exception ex) {
try {
if (userTx != null) userTx.rollback();
} catch (Exception rbEx) {
rbEx.printStackTrace();
}
throw new RuntimeException(ex);
}
I am using Hibernate search 3.0.0 and Seam 2.0. The code works fine when I index about 10,000 records, but throws this error when I am indexing 100,000 records:
Quote:
2008-05-02 12:39:45,765 WARN [org.hibernate.jdbc.AbstractBatcher] exception clearing maxRows/queryTimeout
java.sql.SQLException: The statement is closed.
at org.jboss.resource.adapter.jdbc.WrappedStatement.checkState(WrappedStatement.java:599)
...
com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException: No operations allowed after statement closed.
...
How do I avoid that? Thanks.