Jira8 : searchProvider.getHitCount throws IllegalStateException Incorrect usage of JIRA/lucene search API

Hi everyone,

I already searched quite a lot for a solution for this problem.
In my Jira8 app, when issue page is loaded, I am checking if my app should appear with searchProvider:

        import com.atlassian.jira.issue.search.SearchQuery;
        import com.atlassian.jira.issue.search.SearchProvider;
        ....

        SearchQuery searchQuery = SearchQuery.create(query, user);
        searchQuery.overrideSecurity(true);
        return searchProvider.getHitCount(searchQuery);

Problem is that in runtime, this is throwing an exception:

    java.lang.IllegalStateException: Incorrect usage of JIRA/lucene search API. You can only create/use: ManagedIndexSearcher inside a context (request or Jira-Thread-Local). Check: JiraThreadLocalUtils for details.
    at com.atlassian.jira.index.ManagedIndexSearcherFactory.createFrom(ManagedIndexSearcherFactory.java:15)
    at com.atlassian.jira.issue.index.ThreadLocalSearcherCache$Cache.retrieveEntitySearcher(ThreadLocalSearcherCache.java:116)
    at com.atlassian.jira.issue.index.ThreadLocalSearcherCache.getSearcher(ThreadLocalSearcherCache.java:39)
    at com.atlassian.jira.issue.index.DefaultIndexManager.getEntitySearcher(DefaultIndexManager.java:889)
    at com.atlassian.jira.issue.index.DefaultIndexManager.getIssueSearcher(DefaultIndexManager.java:866)
    ... 2 filtered
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.atlassian.jira.config.component.SwitchingInvocationHandler.invoke(SwitchingInvocationHandler.java:38)
    at com.sun.proxy.$Proxy166.getIssueSearcher(Unknown Source)
    at com.atlassian.jira.issue.search.SearchProviderFactoryImpl.getSearcher(SearchProviderFactoryImpl.java:17)
    at com.atlassian.jira.issue.search.providers.LuceneSearchProvider.getIssueSearcher(LuceneSearchProvider.java:129)
    at com.atlassian.jira.issue.search.providers.LuceneSearchProvider.getHitCount(LuceneSearchProvider.java:144)
    ... 2 filtered

I tried using jiraThreadLocalUtil

jiraThreadLocalUtil.preCall();
try {
        SearchQuery searchQuery = SearchQuery.create(query, user);
        searchQuery.overrideSecurity(true);
        return searchProvider.getHitCount(searchQuery);
} finally {
   jiraThreadLocalUtil.postCall(LOG, () -> LOG.error("The task 'getHitCount' has open transaction."))

}

but since my method is executing in issue view in a parallel thread, this is stoping / breaking other Jira modules in runtime (attachment, activity history,…) provoking a js console full of exceptions.

Can anyone help me with this?

Thank you

In order to avoid this exception (the case when in ThreadLocalSearcherCache the method inSearcherContext() returns false - searcherContextExists.get() is empty) I added

ThreadLocalSearcherCache.startSearcherContext();

before calling searchProvider (or searchService)

   searchProvider.getHitCount(searchQuery);

Looks like this solves this Lucene problem

1 Like

Full code:

     try {
        ThreadLocalSearcherCache.startSearcherContext();
        return searchProvider.getHitCount(searchQuery);
    } finally {
        ThreadLocalSearcherCache.stopAndCloseSearcherContext();
    }
1 Like

This answer to tell you that this thread provide me a solution i was looking for the last 7 hours…

Thanks a million.

Just another answer to say thank you! And to let the next person know that this does work and is a required change from 7.x to 8.x

Hello @dusan.spaic ,

Our app also performs a search and some processing in a background thread. We are working on a similar case as one of our customers face the same error in his environment but we cannot reproduce the problem in our own environment.

I have two questions for you:

First, were you getting this error constantly? What are the conditions to producing the error?

Second, as you mentioned, Atlassian recommends using JiraThreadLocalUtil in its documentation:
… and I quote: "…any plugin that creates its own threads for background processing must use this component…"
but you say: “…but since my method is executing in issue view in a parallel thread, this is stoping / breaking other Jira modules in runtime”.

I am confused. If there is a background thread involved, there will always be something else running in a parallel thread. Why couldn’t you use JiraThreadLocalUtil ? What are the side effects involved? How is it causing damage in other threads?

Thanks.