Quantcast
Channel: Spring Community Forums - NoSQL
Viewing all articles
Browse latest Browse all 128

Neo4j template transaction problem

$
0
0
Hi,
i have a problem with transaction using spring-data and neo4j.
I need to create some node with duplicated relationship, so i create node using graph repo and i create relationship with Neo4jTemplate method createRelationshipBetween ( the only method that i found who allows duplicates, graph repository removes duplicated by default).
I get an error when i try to create the relationship between nodes, and for what i understand the problem is that neo4jtemplate is not in the same transaction used by graph repository.
There is some possibility to configure neo4jtemplate to use the same transaction used by graph repository and by populateDB method ?

This is the code :

ApplicationContext.xml
Code:

        <context:annotation-config/>
        <context:spring-configured/>
        <bean id="graphDatabaseService" class="org.neo4j.kernel.EmbeddedGraphDatabase"
                destroy-method="shutdown">
            <constructor-arg index="0" value="target/config-test"/>
            <constructor-arg index="1">
                <map>
                        <entry key="enable_remote_shell" value="true"/>
                    <!--  <entry key="allow_store_upgrade" value="true"/> -->
                </map>
            </constructor-arg>       
        </bean>       
        <!-- 
        <bean id="graphDatabaseService" class="org.springframework.data.neo4j.rest.SpringRestGraphDatabase" scope="singleton">
          <constructor-arg index="0" value="http://localhost:7474/db/data" />
        </bean> -->
        <!--
        <bean id="neo4jTransactionManager" class="org.springframework.data.neo4j.config.JtaTransactionManagerFactoryBean">
                <constructor-arg ref="graphDatabaseService"/>
        </bean>
        -->
        <bean id="neo4jTransactionManagerService"
            class="org.neo4j.kernel.impl.transaction.SpringTransactionManager">
            <constructor-arg ref="graphDatabaseService" />
        </bean>
        <bean id="neo4jUserTransactionService" class="org.neo4j.kernel.impl.transaction.UserTransactionImpl">
            <constructor-arg ref="graphDatabaseService" />
        </bean>
       
        <bean id="neo4jTransactionManager"
            class="org.springframework.transaction.jta.JtaTransactionManager">
            <property name="transactionManager" ref="neo4jTransactionManagerService" />
            <property name="userTransaction" ref="neo4jUserTransactionService" />
        </bean>       
       
        <neo4j:repositories base-package="Repository"/>
        <bean id="TestNeoAttributoPojo" class="TestNeoAttributoPojo"></bean>
        <bean id="ServizioAttributoPOJO" class="ServizioAttributoPOJO"></bean>
        <bean id="ServizioCondizionePOJO" class="ServizioCondizionePOJO"></bean>
        <bean id="ServizioPopolaDbPOJO" class="ServizioPopolaDbPOJO"></bean>
        <tx:annotation-driven mode="aspectj" transaction-manager="neo4jTransactionManager"/>
        <neo4j:config graphDatabaseService="graphDatabaseService"/>

Every graph repository have the same code structure:

Code:

        @Neo4jTransactional
        public interface CondizioneRepository extends GraphRepository<CondizioneNode>
        {

        }

Main code :

Code:

        @Autowired AttributoRepository repoAtt;
        @Autowired CondizioneRepository repoCond;
        @Autowired EntitaRepository repoEnt;
        @Autowired ServizioRepository repoSer;
        @Autowired ValoreRepository repoVal;
        @Autowired Neo4jTemplate template;       
       
        @Neo4jTransactional
        public void populateDB()
        {
                ILog logj = (ILog)jc.getBean(FrameworkConstants.LOGGER_BACKEND);
                logj.Write(TypeLog.TRACE, "Entered in inserisci Attributo");
       
                try
                {
                        EntitaNode ent;
                        RelEnteInDeroga relDeroga;
                        RelEntIsComposed relEnteComposed;
                        CondizioneNode cond;
                        AttributoCondizionatoNode relCondComposed;
                        AttributoNode att;
                        RelHasValue relValue;
                        ValoreNode val;
                        RelValueIsValid relValidity;
                       
                        // Filiale 100
                        ent= new EntitaNode();
                        ent.setNome("E100");
                        ent.setTipo("Filiale");
                        repoEnt.save(ent);
                        // Banca  1030
                        ent= new EntitaNode();
                        ent.setNome("E1030");
                        ent.setTipo("Banca");
                        repoEnt.save(ent);
//                        int i=5/0;
                        // Condizione  DARE
                        cond= new CondizioneNode();
                        cond.setNome("DARE");
                        repoCond.save(cond);                       
                        // Condizione  AVERE
                        cond= new CondizioneNode();
                        cond.setNome("AVERE");
                        repoCond.save(cond);
                       
                        //Relazioni
                        //Relazioni Enti
                        cond = repoCond.findByPropertyValue("nome", "AVERE");
                        ent = repoEnt.findByPropertyValue("nome", "E1030");
                        relEnteComposed = template.createRelationshipBetween(ent, cond, RelEntIsComposed.class, "ENTISCOMPOSEDBY", true);                       
                        relEnteComposed.setCondizione(cond);
                        relEnteComposed.setEntita(ent);
                        template.save(relEnteComposed); <----- Error HERE

Error :

Code:

org.neo4j.graphdb.NotInTransactionException
        at org.neo4j.kernel.impl.persistence.PersistenceManager.getResource(PersistenceManager.java:252)
        at org.neo4j.kernel.impl.persistence.PersistenceManager.relationshipCreate(PersistenceManager.java:161)
        at org.neo4j.kernel.impl.core.NodeManager.createRelationship(NodeManager.java:252)
        at org.neo4j.kernel.impl.core.NodeImpl.createRelationshipTo(NodeImpl.java:578)
        at org.neo4j.kernel.impl.core.NodeProxy.createRelationshipTo(NodeProxy.java:207)
        at org.springframework.data.neo4j.support.mapping.EntityStateHandler.createRelationshipBetween(EntityStateHandler.java:225)
        at org.springframework.data.neo4j.support.Neo4jTemplate.createRelationshipBetween(Neo4jTemplate.java:356)
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.neo4j.kernel.EmbeddedGraphDatabase]: Constructor threw exception; nested exception is java.lang.IllegalStateException: Database locked.
        at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:162)
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:121)
        at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:280)
        ... 82 more
Caused by: java.lang.IllegalStateException: Database locked.
        at org.neo4j.kernel.InternalAbstractGraphDatabase.create(InternalAbstractGraphDatabase.java:289)
        at org.neo4j.kernel.InternalAbstractGraphDatabase.run(InternalAbstractGraphDatabase.java:227)
        at org.neo4j.kernel.EmbeddedGraphDatabase.<init>(EmbeddedGraphDatabase.java:79)
        at org.neo4j.kernel.EmbeddedGraphDatabase.<init>(EmbeddedGraphDatabase.java:70)

Regards,
Sampietri Nicola

PS : the code work perfectly if i use a REST neo4j configuration.

Viewing all articles
Browse latest Browse all 128

Trending Articles