title: How to close and re-open Spring Data Neo4J contexts without killing the VM
body: <p>I'm running a spring data neo-4j application (not web based) which works fine during normal operation.</p>
  
  <p>If I close the Spring Context 'ctx.close()' the lock on the neo 4J database goes away.</p>
  
  <p>Then, from the same instance of the application, if I grab another Context I see the lock come back, but if I try and read / write from that database from that context I get an error:</p>
  
  <pre><code>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.
  </code></pre>
  
  <p>Is there a way to successfully close and then re-open the Application Context within a single instance of an application (ie without shutting down the virtual machine) ?</p>
  
  <p>I was initially calling shutdown() on the graph database, but changed this as Michael Hunger told me not to do it.</p>
  
  <p>our problem can be reproduced in our domain like this.</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>Here's the XML file we use to create the context.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;
  &lt;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"&gt;
  
  &lt;context:spring-configured/&gt;
      &lt;context:annotation-config/&gt;
      &lt;context:component-scan base-package="OurData" /&gt;
  
      &lt;neo4j:config storeDirectory="c:/app_data/data"/&gt;
      &lt;neo4j:repositories base-package="OurData"/&gt;
  
  &lt;/beans&gt;
  </code></pre>