ramenhood
ramenhood

Reputation: 48

Autowiring Neo4j OGM Session for SDN 4.2 Embedded

In the SDN 4.1 reference, the configuration includes extending Neo4jConfiguration and setting up a Session object explicitly as a @Bean. In 4.2, the guidance is to not extend Neo4jConfiguration and follow the config below. Note that explicitly setting up a standalone Session object is absent:

@Bean
public org.neo4j.ogm.config.Configuration configuration() {
    org.neo4j.ogm.config.Configuration config = new org.neo4j.ogm.config.Configuration();
    config
            .driverConfiguration()
            .setDriverClassName("org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver")
            .setURI(dbLocation);
    return config;

@Bean
public SessionFactory sessionFactory() {
    SessionFactory sessionFactory = new SessionFactory(configuration(), "org.my.package" );
    sessionFactory.register(new MyPreSaveListener());
    return sessionFactory;
}

I've seen this config used while @Autowiring the Session object itself (not the factory) in repository classes. Does this mean that there will then be only one Session instance across the application? If so, doesn't that go against the idea that a Session lifespan should be limited to an application's "unit of work?"

Note that my repositories are custom and do not extend the neo4j repos, as I am currently migrating away from using the Neo4jTemplate object.

Upvotes: 1

Views: 636

Answers (1)

digx1
digx1

Reputation: 1088

No, there is not one session per application. So long as your Session is called from within a @Transactional annotation or TransactionTemplate it will call a proxy Session (which is generated by the Spring Data Neo4j framework during startup). This will effectively create a session for the lifetime of the transaction (where it is demarcated) and once out of scope, allow it to be garbage collected.

Upvotes: 2

Related Questions