diff --git a/spring-data-neo4j/src/main/java/org/springframework/data/graph/neo4j/config/ConfigurationCheck.java b/spring-data-neo4j/src/main/java/org/springframework/data/graph/neo4j/config/ConfigurationCheck.java new file mode 100644 index 000000000..c7d0be8d3 --- /dev/null +++ b/spring-data-neo4j/src/main/java/org/springframework/data/graph/neo4j/config/ConfigurationCheck.java @@ -0,0 +1,67 @@ +package org.springframework.data.graph.neo4j.config; + +import org.neo4j.graphdb.Transaction; +import org.springframework.data.graph.neo4j.support.GraphDatabaseContext; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.TransactionStatus; + +import javax.annotation.PostConstruct; + +/** + * Validates correct configuration of Neo4j and Spring, especially transaction-managers + */ +public class ConfigurationCheck { + GraphDatabaseContext graphDatabaseContext; + PlatformTransactionManager transactionManager; + + public ConfigurationCheck(GraphDatabaseContext graphDatabaseContext, PlatformTransactionManager transactionManager) { + this.graphDatabaseContext = graphDatabaseContext; + this.transactionManager = transactionManager; + } + + @PostConstruct + private void checkConfiguration() { + checkInjection(); + checkSpringTransactionManager(); + checkNeo4jTransactionManager(); + } + + private void checkInjection() { + assert graphDatabaseContext.getGraphDatabaseService()!=null : "graphDatabaseService not correctly configured, please refer to the manual, setup section"; + } + + private void checkSpringTransactionManager() { + try { + TransactionStatus transaction = transactionManager.getTransaction(null); + updateStartTime(); + transactionManager.commit(transaction); + } catch(Exception e) { + AssertionError error = new AssertionError("transactionManager not correctly configured, please refer to the manual, setup section"); + error.initCause(e); + throw error; + } + } + + private void checkNeo4jTransactionManager() { + Transaction tx = null; + try { + tx = graphDatabaseContext.beginTx(); + updateStartTime(); + tx.success(); + } catch (Exception e) { + AssertionError error = new AssertionError("transactionManager not correctly configured, please refer to the manual, setup section"); + error.initCause(e); + throw error; + } finally { + try { + if (tx != null) tx.finish(); + } catch(Exception e) { + // ignore + } + } + } + + private void updateStartTime() { + graphDatabaseContext.getReferenceNode().setProperty("startTime", System.currentTimeMillis()); + } +} diff --git a/spring-data-neo4j/src/main/java/org/springframework/data/graph/neo4j/config/Neo4jConfiguration.java b/spring-data-neo4j/src/main/java/org/springframework/data/graph/neo4j/config/Neo4jConfiguration.java index 1adffa251..0a7e33cb4 100644 --- a/spring-data-neo4j/src/main/java/org/springframework/data/graph/neo4j/config/Neo4jConfiguration.java +++ b/spring-data-neo4j/src/main/java/org/springframework/data/graph/neo4j/config/Neo4jConfiguration.java @@ -154,4 +154,9 @@ public class Neo4jConfiguration { return transactionManager; } } + + @Bean + public ConfigurationCheck configurationCheck() throws Exception { + return new ConfigurationCheck(graphDatabaseContext(),transactionManager()); + } } diff --git a/spring-data-neo4j/src/test/java/org/springframework/data/graph/neo4j/config/ConfigurationConfirmationTest.java b/spring-data-neo4j/src/test/java/org/springframework/data/graph/neo4j/config/ConfigurationConfirmationTest.java new file mode 100644 index 000000000..99bc799f5 --- /dev/null +++ b/spring-data-neo4j/src/test/java/org/springframework/data/graph/neo4j/config/ConfigurationConfirmationTest.java @@ -0,0 +1,16 @@ +package org.springframework.data.graph.neo4j.config; + +import org.junit.Test; +import org.springframework.beans.factory.BeanCreationException; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +/** + * @author mh + * @since 23.03.11 + */ +public class ConfigurationConfirmationTest { + @Test(expected = BeanCreationException.class) + public void testInvalidTransactionManagerFails() { + ClassPathXmlApplicationContext appCtx = new ClassPathXmlApplicationContext("classpath:org/springframework/data/graph/neo4j/config/ConfigurationCofirmationTest-context.xml"); + } +} diff --git a/spring-data-neo4j/src/test/resources/org/springframework/data/graph/neo4j/config/ConfigurationCofirmationTest-context.xml b/spring-data-neo4j/src/test/resources/org/springframework/data/graph/neo4j/config/ConfigurationCofirmationTest-context.xml new file mode 100644 index 000000000..149f0f9c0 --- /dev/null +++ b/spring-data-neo4j/src/test/resources/org/springframework/data/graph/neo4j/config/ConfigurationCofirmationTest-context.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file