Running a Neo4j Graph Database alongside a traditional JPA relational database within a SpringBoot micro-service can help solve a lot of problems. This is a great way to leverage the power of a graph database within an enterprise environment where you cannot escape the RDBMS entirely.
Working for a large Enterprise comes with extra constraints when introducing new software, and no more so than database technology. That’s because database technology has, until recent years, been very stable and mature in large organisations. Getting permission to use anything but the ‘strategic’ (cough!) managed RDBMS service can be an exhausting undertaking.
We wanted Neo4j to work alongside our RDBMS to serve higher level brain functions.
Since most of the micro-services in a large enterprise have an RDBMS it was interesting to attach a Neo4j database to an existing RDBMS connected service. We wanted Neo4j to work alongside our RDBMS to serve higher level brain functions. Whether using Neo4j as an embedded ‘hydrated’ store for connecting and querying data that would otherwise mean painful SQL, or relying on Neo4j as the primary store and RDBMS for other warehouse like functions (e.g. audit), the two make a great pair.
Turned out getting the Spring magic beans (annotations) to work correctly was a little tricky. The principles are simple enough:
- Create a ChainedTransactionManager, chaining the Jpa and Neo4j transaction managers
@Autowired LocalContainerEntityManagerFactoryBean entityManagerFactoryBean;
new ChainedTransactionManager( new JpaTransactionManager(entityManagerFactoryBean.getObject()), super.transactionManager() );
- Make sure you enable transaction management, and the Jpa and Neo4j repositories (in that order)
@EnableTransactionManagement @EnableJpaRepositories("....jpa.repository") @EnableNeo4jRepositories("....neo4j.repository")
Take a look at the sample code on GitHub. It’s a multi-module project to show JPA and Neo4j in isolation before combining them in the final module.