neo4j / neo4j-ogm

Java Object-Graph Mapping Library for Neo4j
https://neo4j.com/docs/ogm-manual/
Apache License 2.0
328 stars 163 forks source link

NPE when creating a SessionFactory in 3.0 snapshots when there's no ogm.properties file #340

Closed wilkinsona closed 7 years ago

wilkinsona commented 7 years ago

Expected Behavior

A NullPointerException is not thrown when calling new SessionFactory(Configuration, String...)

Current Behavior

A NullPointerException is thrown when calling new SessionFactory(Configuration, String...):

Caused by: java.lang.NullPointerException
    at java.util.Properties$LineReader.readLine(Properties.java:434)
    at java.util.Properties.load0(Properties.java:353)
    at java.util.Properties.load(Properties.java:341)
    at org.neo4j.ogm.config.ClasspathConfigurationSource.<init>(ClasspathConfigurationSource.java:19)
    at org.neo4j.ogm.config.Components.configure(Components.java:73)
    at org.neo4j.ogm.config.Components.autoConfigure(Components.java:106)
    at org.neo4j.ogm.config.Components.loadDriver(Components.java:114)
    at org.neo4j.ogm.config.Components.driver(Components.java:86)
    at org.neo4j.ogm.session.SessionFactory.<init>(SessionFactory.java:53)
    at org.neo4j.ogm.session.SessionFactory.<init>(SessionFactory.java:88)
    at org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration.sessionFactory(Neo4jDataAutoConfiguration.java:75)
    at org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration$$EnhancerBySpringCGLIB$$29c1a54f.CGLIB$sessionFactory$1(<generated>)
    at org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration$$EnhancerBySpringCGLIB$$29c1a54f$$FastClassBySpringCGLIB$$48c5ef2d.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:358)
    at org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration$$EnhancerBySpringCGLIB$$29c1a54f.sessionFactory(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
    ... 52 more

Possible Solution

The underlying problem appears to be that Components.driver() is still called, but Components has not been initialized with the Configuration instance. This initialisation was removed in https://github.com/neo4j/neo4j-ogm/commit/7b2b21e5cf82661f227733e7f0855b7c41dae47b which is entitled "Making Configuration the default mechanism for settings rather than Components". It looks like the move to a Configuration-based mechanism is incomplete.

Steps to Reproduce (for bugs)

The following code will reproduce the problem if there's no ogm.properties file on the classpath:

Configuration configuration = new Configuration();
configuration.setDriverClassName(HttpDriver.class.getName());
new SessionFactory(configuration, "com.example");

The problem can be worked around by calling Components.configure(Configuration) prior to creating the Session Factory:

Configuration configuration = new Configuration();
configuration.setDriverClassName(HttpDriver.class.getName());
Components.configure(configuration);
new SessionFactory(configuration, "com.example");

Context

I am trying to upgrade Spring Boot 2.0 snapshots to use OGM 3.0 snapshots as required by Spring Data Neo4J 5.0 snapshots.

Your Environment

mangrish commented 7 years ago

@wilkinsona: We are making changes to the Configuration in order to remove the static Components class. It appears we don't have a test for this situation. I will add it to the https://github.com/neo4j/neo4j-ogm/tree/refactor/remove-components before merging.

The results of this merge will mean users can either a) Not supply a properties file and simply call new Configuration.Builder() b) supply a file and load it through new Components.Builder(ConfigurationSource). Users can then override the initial values through the builder before calling build().

In this release we want to emphasis configuration via code and letting users supply their own configuration source. If that happens to be a ClasspathConfigurationSource then they can use that but we will be avoiding "autoconfiguration" as of 3.0.

HTH

mangrish commented 7 years ago

@wilkinsona This should work if you build from source. We are having issues with one of our driver tests that is taking a long time. You should be able to keep going if you mvn install for now.

mangrish commented 7 years ago

@wilkinsona Can you update and check if this is still occurring (from SDN snapshot)?

wilkinsona commented 7 years ago

@mangrish This seems to have been fixed as part of the removal of Components. Thanks.