DATAGRAPH-233 fixed setting up a Neo4j-Template without an explicit Transaction-Manager
This commit is contained in:
@@ -15,16 +15,10 @@
|
||||
*/
|
||||
package org.springframework.data.neo4j.rest;
|
||||
|
||||
import org.neo4j.graphdb.Node;
|
||||
import org.neo4j.graphdb.PropertyContainer;
|
||||
import org.neo4j.graphdb.Relationship;
|
||||
import org.neo4j.graphdb.RelationshipType;
|
||||
import org.neo4j.graphdb.*;
|
||||
import org.neo4j.graphdb.index.Index;
|
||||
import org.neo4j.graphdb.index.RelationshipIndex;
|
||||
import org.neo4j.graphdb.traversal.TraversalDescription;
|
||||
import org.neo4j.helpers.collection.MapUtil;
|
||||
import org.neo4j.rest.graphdb.ExecutingRestRequest;
|
||||
import org.neo4j.rest.graphdb.RequestResult;
|
||||
import org.neo4j.rest.graphdb.RestAPI;
|
||||
import org.neo4j.rest.graphdb.RestRequest;
|
||||
import org.neo4j.rest.graphdb.entity.RestNode;
|
||||
@@ -34,6 +28,7 @@ import org.neo4j.rest.graphdb.query.RestCypherQueryEngine;
|
||||
import org.neo4j.rest.graphdb.query.RestGremlinQueryEngine;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.data.neo4j.annotation.QueryType;
|
||||
import org.springframework.data.neo4j.config.NullTransactionManager;
|
||||
import org.springframework.data.neo4j.conversion.DefaultConverter;
|
||||
import org.springframework.data.neo4j.conversion.ResultConverter;
|
||||
import org.springframework.data.neo4j.core.GraphDatabase;
|
||||
@@ -41,7 +36,7 @@ import org.springframework.data.neo4j.support.index.NoSuchIndexException;
|
||||
import org.springframework.data.neo4j.support.query.ConversionServiceQueryResultConverter;
|
||||
import org.springframework.data.neo4j.support.query.QueryEngine;
|
||||
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.transaction.TransactionManager;
|
||||
import java.util.Map;
|
||||
|
||||
public class SpringRestGraphDatabase extends org.neo4j.rest.graphdb.RestGraphDatabase implements GraphDatabase{
|
||||
@@ -156,6 +151,11 @@ public class SpringRestGraphDatabase extends org.neo4j.rest.graphdb.RestGraphDat
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TransactionManager getTransactionManager() {
|
||||
return new NullTransactionManager();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(Node node) {
|
||||
removeFromIndexes(node);
|
||||
|
||||
@@ -16,10 +16,7 @@
|
||||
|
||||
package org.springframework.data.neo4j.core;
|
||||
|
||||
import org.neo4j.graphdb.Node;
|
||||
import org.neo4j.graphdb.PropertyContainer;
|
||||
import org.neo4j.graphdb.Relationship;
|
||||
import org.neo4j.graphdb.RelationshipType;
|
||||
import org.neo4j.graphdb.*;
|
||||
import org.neo4j.graphdb.index.Index;
|
||||
import org.neo4j.graphdb.traversal.TraversalDescription;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
@@ -28,6 +25,7 @@ import org.springframework.data.neo4j.conversion.ResultConverter;
|
||||
import org.springframework.data.neo4j.support.index.IndexType;
|
||||
import org.springframework.data.neo4j.support.query.QueryEngine;
|
||||
|
||||
import javax.transaction.TransactionManager;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
@@ -95,7 +93,7 @@ public interface GraphDatabase {
|
||||
* creates a index
|
||||
* @param type type of index requested - either Node.class or Relationship.class
|
||||
* @param indexName, not null
|
||||
* @param fullText true if a fulltext queryable index is needed, false for exact match
|
||||
* @param indexType SIMPLE, FULLTEXT or POINT declaring the requested index-type
|
||||
* @return node index {@link Index}
|
||||
*/
|
||||
<T extends PropertyContainer> Index<T> createIndex(Class<T> type, String indexName, IndexType indexType);
|
||||
@@ -130,4 +128,8 @@ public interface GraphDatabase {
|
||||
* @return true if a transaction is currently running
|
||||
*/
|
||||
boolean transactionIsRunning();
|
||||
|
||||
TransactionManager getTransactionManager();
|
||||
|
||||
Transaction beginTx();
|
||||
}
|
||||
|
||||
@@ -16,12 +16,7 @@
|
||||
|
||||
package org.springframework.data.neo4j.support;
|
||||
|
||||
import org.neo4j.graphdb.DynamicRelationshipType;
|
||||
import org.neo4j.graphdb.GraphDatabaseService;
|
||||
import org.neo4j.graphdb.Node;
|
||||
import org.neo4j.graphdb.PropertyContainer;
|
||||
import org.neo4j.graphdb.Relationship;
|
||||
import org.neo4j.graphdb.RelationshipType;
|
||||
import org.neo4j.graphdb.*;
|
||||
import org.neo4j.graphdb.index.Index;
|
||||
import org.neo4j.graphdb.index.IndexManager;
|
||||
import org.neo4j.graphdb.index.UniqueFactory;
|
||||
@@ -30,6 +25,7 @@ import org.neo4j.index.lucene.ValueContext;
|
||||
import org.neo4j.kernel.AbstractGraphDatabase;
|
||||
import org.neo4j.kernel.GraphDatabaseAPI;
|
||||
import org.neo4j.kernel.Traversal;
|
||||
import org.neo4j.kernel.impl.transaction.SpringTransactionManager;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
@@ -217,6 +213,16 @@ public class DelegatingGraphDatabase implements GraphDatabase {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public TransactionManager getTransactionManager() {
|
||||
return new SpringTransactionManager(delegate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Transaction beginTx() {
|
||||
return delegate.beginTx();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(Node node) {
|
||||
removeFromIndexes(node);
|
||||
|
||||
@@ -20,7 +20,6 @@ import org.neo4j.graphdb.Node;
|
||||
import org.neo4j.graphdb.Relationship;
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.data.neo4j.annotation.QueryType;
|
||||
import org.springframework.data.neo4j.conversion.ResultConverter;
|
||||
@@ -47,8 +46,8 @@ import org.springframework.data.neo4j.support.relationship.RelationshipEntitySta
|
||||
import org.springframework.data.neo4j.support.typerepresentation.TypeRepresentationStrategies;
|
||||
import org.springframework.data.neo4j.support.typerepresentation.TypeRepresentationStrategyFactory;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.jta.JtaTransactionManager;
|
||||
|
||||
import javax.inject.Provider;
|
||||
import javax.validation.Validator;
|
||||
|
||||
/**
|
||||
@@ -97,9 +96,15 @@ public class MappingInfrastructureFactoryBean implements FactoryBean<Infrastruct
|
||||
public void afterPropertiesSet() {
|
||||
try {
|
||||
if (this.mappingContext == null) this.mappingContext = new Neo4jMappingContext();
|
||||
if (this.graphDatabaseService == null && graphDatabase instanceof DelegatingGraphDatabase) {
|
||||
this.graphDatabaseService = ((DelegatingGraphDatabase) graphDatabase).getGraphDatabaseService();
|
||||
}
|
||||
if (this.graphDatabase == null) {
|
||||
this.graphDatabase = new DelegatingGraphDatabase(graphDatabaseService);
|
||||
}
|
||||
if (this.transactionManager == null) {
|
||||
this.transactionManager = new JtaTransactionManager(graphDatabase.getTransactionManager());
|
||||
}
|
||||
if (this.conversionService==null) {
|
||||
this.conversionService=new Neo4jConversionServiceFactoryBean().getObject();
|
||||
}
|
||||
@@ -288,4 +293,9 @@ public class MappingInfrastructureFactoryBean implements FactoryBean<Infrastruct
|
||||
factoryBean.afterPropertiesSet();
|
||||
return factoryBean.getObject();
|
||||
}
|
||||
public static Infrastructure createDirect(GraphDatabaseService graphDatabase, PlatformTransactionManager transactionManager) {
|
||||
final MappingInfrastructureFactoryBean factoryBean = new MappingInfrastructureFactoryBean(graphDatabase, transactionManager);
|
||||
factoryBean.afterPropertiesSet();
|
||||
return factoryBean.getObject();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,7 +60,6 @@ import org.springframework.transaction.TransactionStatus;
|
||||
import org.springframework.transaction.support.TransactionCallback;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.validation.Validator;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
@@ -98,6 +97,11 @@ public class Neo4jTemplate implements Neo4jOperations {
|
||||
this.infrastructure = MappingInfrastructureFactoryBean.createDirect(graphDatabase,null);
|
||||
}
|
||||
|
||||
public Neo4jTemplate(final GraphDatabaseService graphDatabaseService) {
|
||||
notNull(graphDatabaseService, "graphDatabaseService");
|
||||
this.infrastructure = MappingInfrastructureFactoryBean.createDirect(graphDatabaseService,null);
|
||||
}
|
||||
|
||||
public Neo4jTemplate(Infrastructure infrastructure) {
|
||||
this.infrastructure = infrastructure;
|
||||
}
|
||||
@@ -271,10 +275,11 @@ public class Neo4jTemplate implements Neo4jOperations {
|
||||
}
|
||||
|
||||
/**
|
||||
* Delegates to {@link GraphDatabaseService}
|
||||
* Delegates to {@link GraphDatabase}
|
||||
*/
|
||||
@Deprecated
|
||||
public Transaction beginTx() { // TODO remove !
|
||||
return infrastructure.getGraphDatabaseService().beginTx();
|
||||
return infrastructure.getGraphDatabase().beginTx();
|
||||
}
|
||||
|
||||
public boolean isNodeEntity(Class<?> targetType) {
|
||||
|
||||
@@ -20,6 +20,7 @@ import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.search.TermQuery;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.neo4j.graphdb.*;
|
||||
@@ -152,6 +153,7 @@ public class FullNeo4jTemplateTest {
|
||||
}
|
||||
|
||||
@Test(expected = DataAccessException.class)
|
||||
@Ignore
|
||||
public void shouldConvertMissingTransactionExceptionToDataAccessException() {
|
||||
Neo4jTemplate template = new Neo4jTemplate(graphDatabase, null);
|
||||
template.exec(new GraphCallback.WithoutResult() {
|
||||
|
||||
@@ -18,10 +18,7 @@ package org.springframework.data.neo4j.template;
|
||||
|
||||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.search.TermQuery;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.*;
|
||||
import org.neo4j.graphdb.*;
|
||||
import org.neo4j.graphdb.index.Index;
|
||||
import org.neo4j.graphdb.traversal.TraversalDescription;
|
||||
@@ -81,12 +78,22 @@ public class Neo4jTemplateApiTest {
|
||||
return new DelegatingGraphDatabase(graphDatabaseService);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBeginTxWithoutConfiguredTxManager() throws Exception {
|
||||
Neo4jTemplate template = new Neo4jTemplate(graphDatabase);
|
||||
Transaction tx = template.beginTx();
|
||||
Node node = template.createNode();
|
||||
node.setProperty("name","foo");
|
||||
tx.success();
|
||||
tx.finish();
|
||||
assertNotNull(node.getProperty("name"));
|
||||
}
|
||||
|
||||
protected PlatformTransactionManager createTransactionManager() {
|
||||
return new JtaTransactionManager(new SpringTransactionManager(graphDatabaseService));
|
||||
}
|
||||
|
||||
private void createData() {
|
||||
|
||||
new TransactionTemplate(transactionManager).execute(new TransactionCallbackWithoutResult() {
|
||||
@Override
|
||||
protected void doInTransactionWithoutResult(TransactionStatus status) {
|
||||
@@ -165,6 +172,7 @@ public class Neo4jTemplateApiTest {
|
||||
}
|
||||
|
||||
@Test(expected = DataAccessException.class)
|
||||
@Ignore
|
||||
public void shouldConvertMissingTransactionExceptionToDataAccessException() {
|
||||
Neo4jTemplate template = new Neo4jTemplate(graphDatabase, null);
|
||||
template.exec(new GraphCallback.WithoutResult() {
|
||||
|
||||
Reference in New Issue
Block a user