NPE in SearchFactory while using different threads

Description

java.lang.NullPointerException
at org.hibernate.search.SearchFactory.getSearchFactory(SearchFactory.java:120)
at com.seanergie.persistence.SessionsManager.getSearchFactory(SessionsManager.java:374)
...

The contexts ThreadLocal is missing a get method. it is initialized by the first thread that uses it by the static statement, but for the next Thread it hasn't been initialized, and so it contexts.get() returns null.

Simple fix : add the get method as follows :

public class SearchFactory {
private static ThreadLocal<WeakHashMap<Configuration, SearchFactory>> contexts =
new ThreadLocal<WeakHashMap<Configuration, SearchFactory>>(){
@Override
public WeakHashMap<Configuration,SearchFactory> get() {
return new WeakHashMap<Configuration, SearchFactory>(2);
}
};
static {
Version.touch();
contexts.set( new WeakHashMap<Configuration, SearchFactory>(2) );
}

Environment

None

Status

Assignee

Emmanuel Bernard

Reporter

Sylvain Vieujot

Labels

None

Suitable for new contributors

None

Pull Request

None

Feedback Requested

None

Time tracking

3m

Components

Fix versions

Priority

Major