Reputation: 1672
After updating Spring Data Neo4j to 4.2.0.BUILD-SNAPSHOT
several difficulties appeared. In particular, I need to get current Session
instance to perform some custom query. I've been doing this using
private Neo4jOperations getNeo4jOperations() {
return applicationContext.getBean(Neo4jOperations.class);
}
but Neo4jOperations
is now deprecated, so I tried
private Session Neo4jSession() {
return applicationContext.getBean(SessionFactory.class).openSession();
}
but got an exception:
org.neo4j.ogm.exception.TransactionManagerException: Transaction is not current for this thread
org.neo4j.ogm.session.transaction.DefaultTransactionManager.rollback(DefaultTransactionManager.java:93)
org.neo4j.ogm.transaction.AbstractTransaction.rollback(AbstractTransaction.java:67)
org.neo4j.ogm.drivers.bolt.transaction.BoltTransaction.rollback(BoltTransaction.java:64)
org.neo4j.ogm.transaction.AbstractTransaction.close(AbstractTransaction.java:141)
org.springframework.data.neo4j.transaction.Neo4jTransactionManager.doCleanupAfterCompletion(Neo4jTransactionManager.java:311)
org.springframework.transaction.support.AbstractPlatformTransactionManager.cleanupAfterCompletion(AbstractPlatformTransactionManager.java:1016)
org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:811)
org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:730)
org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:487)
org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:291)
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
com.sun.proxy.$Proxy110.delete(Unknown Source)
So how can I get a current session instance?
Upvotes: 2
Views: 1081
Reputation: 7212
Could you try this?
Note that this config is using spring-data-neo4j 4.2.0.M1
version, but I think it should work for 4.2.0.BUILD-SNAPSHOT
as well
@Bean
public SessionFactory getSessionFactory() {
return new SessionFactory(getConfiguration(), "com.yourbasepackage") {
};
}
@Bean
@Scope(value = "prototype", proxyMode = ScopedProxyMode.TARGET_CLASS)
public Session getSession() throws Exception {
return getSessionFactory().openSession();
}
Also annotate your config class with:
@EnableTransactionManagement
Then you can inject the session
bean and use it whatever you want:
@Inject
Session session;
You can see the upgrade to SDN 4.2 complete guide here.
NOTE: Due to another issue, I've recently created a sample project on github configured correctly with this version of
spring-data-neo4j
. I think it could help you as a configuration sample.
Hope it helps
Upvotes: 4