Skip to content

Instantly share code, notes, and snippets.

@chrisvest
Created November 16, 2013 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisvest/7504624 to your computer and use it in GitHub Desktop.
Save chrisvest/7504624 to your computer and use it in GitHub Desktop.
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));
}
}
}
@chrisvest
Copy link
Author

Please don't do stuff like this, people.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment