繁体   English   中英

如何在不杀死VM的情况下关闭并重新打开Spring Data Neo4J上下文

[英]How to close and re-open Spring Data Neo4J contexts without killing the VM

我正在运行一个spring数据neo-4j应用程序(不是基于web的),它在正常操作期间工作正常。

如果我关闭Spring Context'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 Context。 像这样:

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

查看我的上一条评论,您的回复已经编辑了我的完整答案。

下面的两个主要文件。

第一个使用WrappingNeoServerBootstrapper

Neo4j服务器的引导程序,它采用已经实例化的{@link org.neo4j.kernel.GraphDatabaseAPI}和可选配置,并使用该数据库启动服务器。 使用此命令从已使用{@link EmbeddedGraphDatabase}或{@link HighlyAvailableGraphDatabase}的应用程序中启动完整的Neo4j服务器。 这为您的应用程序提供了服务器REST API,Web管理界面和统计信息跟踪的全部优势。

package sandbox;

import org.neo4j.server.WrappingNeoServerBootstrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.neo4j.support.Neo4jTemplate;

public class GalaxyServiceTest {

    private final static Logger slf4jLogger = LoggerFactory.getLogger(GalaxyServiceTest.class);
    @Autowired
    private GalaxyService galaxyService;
    @Autowired
    private Neo4jTemplate template;

    public static void main(String args[]) throws InterruptedException {
        GalaxyServiceTest main = new GalaxyServiceTest();
        ApplicationContextLoader loader = new ApplicationContextLoader();
        loader.load(main, "/spring/helloWorldContext.xml");
        // The server starts with loading of above Context.xml
        WrappingNeoServerBootstrapper neoServer = loader.getApplicationContext().getBean("serverWrapper", WrappingNeoServerBootstrapper.class);
        //process something in repository 
        main.doSomething();
        // do a graceful stop
        int stop = neoServer.stop(0);
        slf4jLogger.info("stopping Server status code {} ", stop);
        //Restart the server 
        neoServer.start();
        slf4jLogger.info("Restarting Server ");
        // Process something in Repository
        main.doSomething();

    }

    public void doSomething() {
        galaxyService.makeSomeWorlds();
        Iterable<World> allWorlds = galaxyService.getAllWorlds();
        for (World world : allWorlds) {
            slf4jLogger.info("World Name is {}", world.toString());
        }
    }
}

应用程序上下文定义xml

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

    <bean id="transactionManager" 
          class="org.springframework.transaction.jta.JtaTransactionManager">
        <property name="transactionManager">
            <bean id="jotm" class="org.springframework.data.neo4j.transaction.JotmFactoryBean"/>
        </property>
    </bean>
    <neo4j:config graphDatabaseService="graphDatabaseService" />
    <bean id="serverWrapper" class="org.neo4j.server.WrappingNeoServerBootstrapper"
          init-method="start" destroy-method="stop">
        <constructor-arg ref="graphDatabaseService" />
    </bean>

    <bean id="graphDatabaseService" class="org.neo4j.kernel.EmbeddedGraphDatabase" 
          destroy-method="shutdown">
        <constructor-arg value="target/test-db"/>
    </bean>
    <tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>

    <neo4j:repositories base-package="sandbox"></neo4j:repositories>

</beans>

我希望这有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM