【问题标题】:Hibernate search lucene consuming database connections休眠搜索 lucene 消耗数据库连接
【发布时间】:2019-02-26 00:13:33
【问题描述】:

我已经为 c3p0 池配置了 maxPoolSize 10。 minPoolSize 为 3。 每当我启动服务器并为 mysql 数据库执行 show processlist 时,都会建立三个连接。除非服务器有大量流量,否则这将保持为三个。这很好。

我已经实现了第一次开始索引的搜索功能。 当应用程序进行搜索并且当我检查我的数据库的显示进程列表时,所有数据库连接现在都打开了。我看到所有 10 个连接都是打开的。 这是公认的行为。理想情况下,我会说使用打开 2 或 3 个连接可能会很好。 为什么休眠 lucene 使用许多连接? 我可以在某处为 lucene 配置最大连接数吗?

我在下面发布我的代码。

fullTextEntityManager.createIndexer().startAndWait(); 

将只为应用程序执行一次

    @Transactional
public List<E> searchRecord(String searchText) {

    FullTextEntityManager fullTextEntityManager = org.hibernate.search.jpa.Search.getFullTextEntityManager(em);

    if (CREATE_INDEX.equals("START")) {
        try {
            log.info("creating indexes");
            fullTextEntityManager.createIndexer().startAndWait();
            CREATE_INDEX = "END";
        } catch (InterruptedException e) {
            log.info("error in creating indexes", e);
        }
    }
    log.info("lucene query builder");
    QueryBuilder qb = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(getEntityClass())
            .get();
    // org.apache.lucene.search.Query luceneQuery =
    // qb.keyword().onFields("name")
    // .matching(searchText).createQuery();
    if(searchText.contains(" ")){
        String[] splittedText = searchText.split(" ");
        searchText = splittedText[0];
    }
    org.apache.lucene.search.Query luceneQuery = qb.keyword().wildcard().onField(getSearchField())
            .matching(searchText.toLowerCase() + '*').createQuery();

    javax.persistence.Query jpaQuery = fullTextEntityManager.createFullTextQuery(luceneQuery, getEntityClass());

    List<E> result = jpaQuery.getResultList();
    return result;
}

【问题讨论】:

    标签: java spring-mvc lucene hibernate-search c3p0


    【解决方案1】:

    海量索引器的目的是处理整个数据库(或至少应由 Hibernate Search 索引的部分)以重建索引。正如您所料,这是一个高度资源密集型过程,需要大量数据库访问,并行执行这些访问可以显着加快该过程。这就是它使用许多连接的原因。默认情况下它使用 7 个连接。

    现在,如果您希望质量索引器使用更少的连接,您可以简单地要求质量索引器使用更少的线程:

    fullTextEntityManager.createIndexer()
        .threadsToLoadObjects(2)
        .startAndWait();
    

    上面将只使用 3 个线程:2 个从数据库加载实体,1 个将文档推送到索引。不过,显然重新索引会更慢。

    有关此主题的更多信息,请参阅this section of the documentation

    【讨论】:

    • 它不起作用,它还使用了所有可用的连接。在我的情况下,总共新创建了 6 个连接。我使用的是休眠搜索 4.1.1 版本,因为 typesToIndexInParallel 不可用。我必须尝试使用​​该选项。
    • 哦,好的。我的回答是关于最近的版本,例如 Search 5.10,并且可能早在 5.5(3 年前)就适用。除此之外,我真的不知道。
    猜你喜欢
    • 2021-07-28
    • 2012-08-17
    • 1970-01-01
    • 2014-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-01
    • 1970-01-01
    相关资源
    最近更新 更多