DATAGRAPH-255 Transaction Manager Compatibility with 1.7 AND 1.8

This commit is contained in:
Lasse Westh-Nielsen
2012-06-13 12:14:02 +01:00
parent 54e36a43f4
commit 3379bb09bf
9 changed files with 269 additions and 106 deletions

View File

@@ -162,6 +162,7 @@
<module>spring-data-neo4j-examples/cineasts-rest</module>
<module>spring-data-neo4j-examples/myrestaurants-social</module>
<module>spring-data-neo4j-examples/todos</module>
<module>spring-data-neo4j-examples/backwardscompatibility</module>
</modules>
</profile>
<profile>

View File

@@ -106,8 +106,8 @@
</property>
</bean-->
<bean id="transactionManager" class="org.springframework.data.neo4j.config.TestTransactionManagerFactoryBean">
<property name="graphDatabaseService" ref="graphDatabaseService"/>
<bean id="transactionManager" class="org.springframework.data.neo4j.config.JtaTransactionManagerFactoryBean">
<constructor-arg ref="graphDatabaseService"/>
</bean>
<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>

View File

@@ -21,6 +21,7 @@ import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.neo4j.aspects.config.Neo4jAspectConfiguration;
import org.springframework.data.neo4j.config.JtaTransactionManagerFactoryBean;
import org.springframework.data.neo4j.cross_store.support.node.CrossStoreNodeDelegatingFieldAccessorFactory;
import org.springframework.data.neo4j.cross_store.support.node.CrossStoreNodeEntityInstantiator;
import org.springframework.data.neo4j.cross_store.support.node.CrossStoreNodeEntityStateFactory;
@@ -68,14 +69,15 @@ public class CrossStoreNeo4jConfiguration extends Neo4jAspectConfiguration {
}
@Bean
public PlatformTransactionManager neo4jTransactionManager() {
public PlatformTransactionManager neo4jTransactionManager() throws Exception {
JtaTransactionManager jtaTm = new JtaTransactionManagerFactoryBean( getGraphDatabaseService() ).getObject();
if (isUsingCrossStorePersistence()) {
JpaTransactionManager jpaTm = new JpaTransactionManager(getEntityManagerFactory());
JtaTransactionManager jtaTm = createJtaTransactionManager();
return new ChainedTransactionManager(jpaTm, jtaTm);
}
else {
return createJtaTransactionManager();
return jtaTm;
}
}

View File

@@ -0,0 +1,73 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-data-neo4j-dist</artifactId>
<groupId>org.springframework.data</groupId>
<version>2.1.0.BUILD-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>backwardscompatibility</artifactId>
<properties>
<spring.version>3.0.7.RELEASE</spring.version>
<neo4j.version>1.7</neo4j.version>
</properties>
<dependencies>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j</artifactId>
<version>${neo4j.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-neo4j</artifactId>
<version>2.1.0.BUILD-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<repositories>
<repository>
<id>neo4j-snapshot-repository</id>
<name>Neo4j Maven 2 snapshot repository</name>
<url>http://m2.neo4j.org/snapshots</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>
<profiles>
<profile>
<id>1.7</id>
<properties>
<neo4j.version>1.7</neo4j.version>
</properties>
</profile>
<profile>
<id>1.8-SNAPSHOT</id>
<properties>
<neo4j.version>1.8-SNAPSHOT</neo4j.version>
</properties>
</profile>
</profiles>
</project>

View File

@@ -0,0 +1,41 @@
package backwardscompatibility;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.neo4j.support.Neo4jTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
@ContextConfiguration(locations = "classpath:/backwardscompatibility.xml")
@RunWith(SpringJUnit4ClassRunner.class)
@Transactional
public class BackwardsCompatibilityTest
{
@Autowired
private Neo4jTemplate template;
@Test
public void shouldBeBackwardsCompatible() throws Exception
{
Transaction transaction = template.getGraphDatabase().beginTx();
try
{
Node node = template.createNode();
node.setProperty( "foo", "bar" );
transaction.success();
}
catch ( Exception e )
{
transaction.failure();
}
finally
{
transaction.finish();
}
}
}

View File

@@ -0,0 +1,21 @@
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/data/neo4j
http://www.springframework.org/schema/data/neo4j/spring-neo4j-2.0.xsd">
<context:spring-configured/>
<context:annotation-config/>
<context:component-scan base-package="backwardscompatibility" />
<neo4j:config storeDirectory="target/neo4j-db"/>
<neo4j:repositories base-package="backwardscompatibility"/>
</beans>

View File

@@ -0,0 +1,123 @@
/**
* Copyright 2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.neo4j.config;
import javax.transaction.TransactionManager;
import javax.transaction.UserTransaction;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.kernel.GraphDatabaseAPI;
import org.neo4j.kernel.impl.transaction.SpringTransactionManager;
import org.neo4j.kernel.impl.transaction.UserTransactionImpl;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.transaction.jta.JtaTransactionManager;
import org.springframework.transaction.jta.UserTransactionAdapter;
public class JtaTransactionManagerFactoryBean implements FactoryBean<JtaTransactionManager>
{
private final JtaTransactionManager jtaTransactionManager;
public JtaTransactionManagerFactoryBean( GraphDatabaseService gds ) throws Exception
{
jtaTransactionManager = create( gds );
}
@Override
public JtaTransactionManager getObject() throws Exception
{
return jtaTransactionManager;
}
@Override
public Class<JtaTransactionManager> getObjectType()
{
return JtaTransactionManager.class;
}
@Override
public boolean isSingleton()
{
return true;
}
private JtaTransactionManager create( GraphDatabaseService gds ) throws Exception
{
if ( !(gds instanceof GraphDatabaseAPI) )
{
return createNullJtaTransactionManager();
}
try
{
return createJtaTransactionManager( gds );
}
catch ( NoSuchMethodException e )
{
return createJtaTransactionManagerForOnePointSeven( gds );
}
}
private JtaTransactionManager createNullJtaTransactionManager()
{
TransactionManager transactionManager = new NullTransactionManager();
UserTransaction userTransaction = new UserTransactionAdapter( transactionManager );
return new JtaTransactionManager( userTransaction, transactionManager );
}
private JtaTransactionManager createJtaTransactionManagerForOnePointSeven( GraphDatabaseService gds ) throws Exception
{
TransactionManager transactionManager = createTransactionManagerForOnePointSeven( gds );
UserTransaction userTransaction = createUserTransactionForOnePointSeven( gds );
return new JtaTransactionManager( userTransaction, transactionManager );
}
private JtaTransactionManager createJtaTransactionManager( GraphDatabaseService gds ) throws Exception
{
TransactionManager transactionManager = createTransactionManagerForOnePointEight( gds );
UserTransaction userTransaction = createUserTransactionForOnePointEight( gds );
return new JtaTransactionManager( userTransaction, transactionManager );
}
private TransactionManager createTransactionManagerForOnePointSeven( GraphDatabaseService gds ) throws Exception
{
return createDynamically( SpringTransactionManager.class, GraphDatabaseService.class, gds );
}
private UserTransaction createUserTransactionForOnePointSeven( GraphDatabaseService gds ) throws Exception
{
TransactionManager txManager = ((GraphDatabaseAPI) gds).getTxManager();
return createDynamically( UserTransactionImpl.class, TransactionManager.class, txManager );
}
private TransactionManager createTransactionManagerForOnePointEight( GraphDatabaseService gds ) throws Exception
{
return createDynamically( SpringTransactionManager.class, GraphDatabaseAPI.class, gds );
}
private UserTransaction createUserTransactionForOnePointEight( GraphDatabaseService gds ) throws Exception
{
return createDynamically( UserTransactionImpl.class, GraphDatabaseAPI.class, gds );
}
private <T> T createDynamically( Class<T> requiredClass, Class<?> argumentClass, Object gds ) throws Exception
{
return requiredClass.getDeclaredConstructor( argumentClass ).newInstance( gds );
}
}

View File

@@ -19,9 +19,6 @@ package org.springframework.data.neo4j.config;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.kernel.GraphDatabaseAPI;
import org.neo4j.kernel.impl.transaction.SpringTransactionManager;
import org.neo4j.kernel.impl.transaction.UserTransactionImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
@@ -59,10 +56,6 @@ import org.springframework.data.neo4j.support.relationship.RelationshipEntitySta
import org.springframework.data.neo4j.support.typerepresentation.ClassValueTypeInformationMapper;
import org.springframework.data.neo4j.support.typerepresentation.TypeRepresentationStrategyFactory;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.jta.JtaTransactionManager;
import org.springframework.transaction.jta.UserTransactionAdapter;
import javax.transaction.TransactionManager;
import javax.validation.Validator;
import static java.util.Arrays.asList;
@@ -158,7 +151,7 @@ public abstract class Neo4jConfiguration {
@Bean
public TypeMapper<Relationship> relationshipTypeMapper() throws Exception {
return new DefaultTypeMapper<Relationship>(new TRSTypeAliasAccessor<Relationship>(relationshipTypeRepresentationStrategy()),asList(new ClassValueTypeInformationMapper()));
return new DefaultTypeMapper<Relationship>(new TRSTypeAliasAccessor<Relationship>(relationshipTypeRepresentationStrategy()),asList( new ClassValueTypeInformationMapper() ));
}
@Bean
@@ -228,8 +221,8 @@ public abstract class Neo4jConfiguration {
@Bean(name = {"neo4jTransactionManager","transactionManager"})
@Qualifier("neo4jTransactionManager")
public PlatformTransactionManager neo4jTransactionManager() {
return createJtaTransactionManager();
public PlatformTransactionManager neo4jTransactionManager() throws Exception {
return new JtaTransactionManagerFactoryBean(getGraphDatabaseService()).getObject();
}
@Bean
@@ -237,21 +230,6 @@ public abstract class Neo4jConfiguration {
return new IndexCreationMappingEventListener(neo4jTemplate());
}
protected JtaTransactionManager createJtaTransactionManager() {
JtaTransactionManager jtaTm = new JtaTransactionManager();
final GraphDatabaseService gds = getGraphDatabaseService();
if (gds instanceof GraphDatabaseAPI) {
final TransactionManager txManager = ((GraphDatabaseAPI) gds).getTxManager();
jtaTm.setTransactionManager(new SpringTransactionManager(gds));
jtaTm.setUserTransaction(new UserTransactionImpl(txManager));
} else {
final NullTransactionManager tm = new NullTransactionManager();
jtaTm.setTransactionManager(tm);
jtaTm.setUserTransaction(new UserTransactionAdapter(tm));
}
return jtaTm;
}
@Bean
@Autowired
@DependsOn("graphDatabaseService")

View File

@@ -1,76 +0,0 @@
/**
* Copyright 2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.neo4j.config;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.kernel.AbstractGraphDatabase;
import org.neo4j.kernel.GraphDatabaseAPI;
import org.neo4j.kernel.impl.transaction.SpringTransactionManager;
import org.neo4j.kernel.impl.transaction.UserTransactionImpl;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.transaction.jta.JtaTransactionManager;
import org.springframework.transaction.jta.UserTransactionAdapter;
import javax.transaction.TransactionManager;
/**
* @author mh
* @since 09.04.11
*/
public class TestTransactionManagerFactoryBean implements FactoryBean<JtaTransactionManager> {
private GraphDatabaseService graphDatabaseService;
private JtaTransactionManager transactionManager;
@Override
public JtaTransactionManager getObject() throws Exception {
return transactionManager;
}
@Override
public Class<?> getObjectType() {
return JtaTransactionManager.class;
}
@Override
public boolean isSingleton() {
return true;
}
private JtaTransactionManager createJtaTransactionManager(GraphDatabaseService gds) {
JtaTransactionManager jtaTm = new JtaTransactionManager();
if (gds instanceof GraphDatabaseAPI) {
final TransactionManager txManager = ((GraphDatabaseAPI) gds).getTxManager();
jtaTm.setTransactionManager(new SpringTransactionManager(gds));
jtaTm.setUserTransaction(new UserTransactionImpl(txManager));
} else {
final NullTransactionManager tm = new NullTransactionManager();
jtaTm.setTransactionManager(tm);
jtaTm.setUserTransaction(new UserTransactionAdapter(tm));
}
return jtaTm;
}
public GraphDatabaseService getGraphDatabaseService() {
return graphDatabaseService;
}
public void setGraphDatabaseService(GraphDatabaseService graphDatabaseService) {
this.graphDatabaseService = graphDatabaseService;
transactionManager = createJtaTransactionManager(graphDatabaseService);
}
}