gpt4 book ai didi

java - 如何在不终止 VM 的情况下关闭和重新打开 Spring Data Neo4J 上下文

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:32:42 25 4
gpt4 key购买 nike

我正在运行一个 spring data neo-4j 应用程序(不是基于 Web 的),它在正常操作期间运行良好。

如果我关闭 Spring 上下文“ctx.close()”,neo 4J 数据库上的锁就会消失。

然后,从应用程序的同一个实例中,如果我获取另一个上下文,我会看到锁返回,但是如果我尝试从该上下文读取/写入该数据库,我会收到错误消息:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.data.neo4j.config.Neo4jConfiguration#0': Unsatisfied dependency expressed through bean property 'conversionService': : Error creating bean with name 'mappingInfrastructure' defined in class org.springframework.data.neo4j.config.Neo4jConfiguration: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public final org.springframework.data.neo4j.support.MappingInfrastructureFactoryBean org.springframework.data.neo4j.config.Neo4jConfiguration$$EnhancerByCGLIB$$64cefd6f.mappingInfrastructure() throws java.lang.Exception] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'typeRepresentationStrategyFactory' defined in class org.springframework.data.neo4j.config.Neo4jConfiguration: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public final org.springframework.data.neo4j.support.typerepresentation.TypeRepresentationStrategyFactory org.springframework.data.neo4j.config.Neo4jConfiguration$$EnhancerByCGLIB$$64cefd6f.typeRepresentationStrategyFactory() throws java.lang.Exception] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'graphDatabaseService': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.neo4j.kernel.EmbeddedGraphDatabase]: Constructor threw exception; nested exception is java.lang.IllegalStateException: Unable to lock store [C:\app_data\gelato\data\neostore], this is usually a result of some other Neo4j kernel running using the same store.; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mappingInfrastructure' defined in class org.springframework.data.neo4j.config.Neo4jConfiguration: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public final org.springframework.data.neo4j.support.MappingInfrastructureFactoryBean org.springframework.data.neo4j.config.Neo4jConfiguration$$EnhancerByCGLIB$$64cefd6f.mappingInfrastructure() throws java.lang.Exception] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'typeRepresentationStrategyFactory' defined in class org.springframework.data.neo4j.config.Neo4jConfiguration: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public final org.springframework.data.neo4j.support.typerepresentation.TypeRepresentationStrategyFactory org.springframework.data.neo4j.config.Neo4jConfiguration$$EnhancerByCGLIB$$64cefd6f.typeRepresentationStrategyFactory() throws java.lang.Exception] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'graphDatabaseService': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.neo4j.kernel.EmbeddedGraphDatabase]: Constructor threw exception; nested exception is java.lang.IllegalStateException: Unable to lock store [C:\app_data\gelato\data\neostore], this is usually a result of some other Neo4j kernel running using the same store.

有没有办法在应用程序的单个实例中成功关闭然后重新打开应用程序上下文(即不关闭虚拟机)?

我最初是在图形数据库上调用 shutdown(),但由于 Michael Hunger 告诉我不要这样做而更改了它。

我们的问题可以像这样在我们的域中重现。

AbstractApplicationContext ctx = new FileSystemXmlApplicationContext("neo4jconfig.xml");
OurDomainService domainService = (OurDomainService) ctx.getBean(OurDomainServiceImpl.class);
// This works
domainService.save(data);
// this releases the lock
ctx.close();
// this re-creates the lock and the context looks actvive
ctx = new FileSystemXmlApplicationContext("neo4jconfig.xml");
domainService = (OurDomainService) ctx.getBean(OurDomainServiceImpl.class);
// this errors out
domainService.save(data);

这是我们用来创建上下文的 XML 文件。

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:neo4j="http://www.springframework.org/schema/data/neo4j"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/data/neo4j
http://www.springframework.org/schema/data/neo4j/spring-neo4j.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">

<context:spring-configured/>
<context:annotation-config/>
<context:component-scan base-package="OurData" />

<neo4j:config storeDirectory="c:/app_data/data"/>
<neo4j:repositories base-package="OurData"/>

</beans>

最佳答案

你不应该这样做,在你的情况下,spring-context 应该处理生命周期。

在您的情况下重新启动时会发生什么?

你关闭了应用上下文

ctx.close()

您可能应该使用 WebApplicationContext(Utils) 通过 web.xml 获取已配置的 Spring 上下文。像这样:

WebApplicationContext springContext = 
WebApplicationContextUtils.getWebApplicationContext(getServletContext());

关于java - 如何在不终止 VM 的情况下关闭和重新打开 Spring Data Neo4J 上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14706012/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com