Created
November 16, 2013 20:01
-
-
Save chrisvest/7504624 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.Test; | |
import org.neo4j.graphdb.*; | |
import org.neo4j.kernel.GraphDatabaseAPI; | |
import org.neo4j.test.TestGraphDatabaseFactory; | |
import javax.transaction.TransactionManager; | |
import static org.hamcrest.Matchers.is; | |
import static org.junit.Assert.assertThat; | |
import static org.junit.Assert.assertTrue; | |
public class TxHandOver { | |
@Test | |
public void testTransactionHandover() throws Exception { | |
TestGraphDatabaseFactory factory = new TestGraphDatabaseFactory(); | |
final GraphDatabaseAPI db = (GraphDatabaseAPI) factory.newImpermanentDatabase(); | |
DependencyResolver resolver = db.getDependencyResolver(); | |
final TransactionManager txManager = resolver.resolveDependency( TransactionManager.class ); | |
final DynamicRelationshipType relationshipType = DynamicRelationshipType.withName("REL"); | |
final Transaction neoTx = db.beginTx(); | |
final Node n1 = db.createNode(); | |
n1.setProperty("a", "b"); | |
final javax.transaction.Transaction transaction = txManager.suspend(); | |
Runnable committer = new Runnable() { | |
@Override | |
public void run() { | |
try { | |
txManager.resume(transaction); | |
Node n2 = db.createNode(); | |
n2.setProperty("x", "y"); | |
n1.createRelationshipTo(n2, relationshipType); | |
neoTx.success(); | |
neoTx.close(); | |
} catch (Exception e) { | |
throw new RuntimeException(e); | |
} | |
} | |
}; | |
Thread thread = new Thread(committer); | |
thread.start(); | |
thread.join(); | |
try ( Transaction ignore = db.beginTx()) | |
{ | |
Node nx = db.getNodeById(n1.getId()); | |
Object value1 = "b"; | |
assertThat(nx.getProperty("a"), is(value1)); | |
assertTrue(nx.hasRelationship(relationshipType, Direction.OUTGOING)); | |
Relationship relationship = nx.getSingleRelationship(relationshipType, Direction.OUTGOING); | |
Node ny = relationship.getEndNode(); | |
Object value2 = "y"; | |
assertThat(ny.getProperty("x"), is(value2)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please don't do stuff like this, people.