Skip to content

examples: Consuming results in writeTransactions #1044

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
import org.neo4j.driver.GraphDatabase;
import org.neo4j.driver.Result;
import org.neo4j.driver.Session;
import org.neo4j.driver.Transaction;
import org.neo4j.driver.TransactionWork;

import static org.neo4j.driver.Values.parameters;
// end::hello-world-import[]
Expand All @@ -51,18 +49,14 @@ public void printGreeting( final String message )
{
try ( Session session = driver.session() )
{
String greeting = session.writeTransaction( new TransactionWork<String>()
{
@Override
public String execute( Transaction tx )
{
Result result = tx.run( "CREATE (a:Greeting) " +
"SET a.message = $message " +
"RETURN a.message + ', from node ' + id(a)",
parameters( "message", message ) );
return result.single().get( 0 ).asString();
}
} );
String greeting = session.writeTransaction( tx ->
{
Result result = tx.run( "CREATE (a:Greeting) " +
"SET a.message = $message " +
"RETURN a.message + ', from node ' + id(a)",
parameters( "message", message ) );
return result.single().get( 0 ).asString();
} );
System.out.println( greeting );
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,19 @@
package org.neo4j.docs.driver;

// tag::pass-bookmarks-import[]

import java.util.ArrayList;
import java.util.List;

import org.neo4j.driver.AccessMode;
import org.neo4j.driver.Bookmark;
import org.neo4j.driver.Record;
import org.neo4j.driver.Session;
import org.neo4j.driver.Result;
import org.neo4j.driver.Session;
import org.neo4j.driver.Transaction;
import org.neo4j.driver.Bookmark;

import static org.neo4j.driver.Values.parameters;
import static org.neo4j.driver.SessionConfig.builder;
import static org.neo4j.driver.Values.parameters;
// end::pass-bookmarks-import[]

public class PassBookmarkExample extends BaseApplication
Expand Down Expand Up @@ -93,27 +94,27 @@ public void addEmployAndMakeFriends()
// Create the first person and employment relationship.
try ( Session session1 = driver.session( builder().withDefaultAccessMode( AccessMode.WRITE ).build() ) )
{
session1.writeTransaction( tx -> addCompany( tx, "Wayne Enterprises" ) );
session1.writeTransaction( tx -> addPerson( tx, "Alice" ) );
session1.writeTransaction( tx -> employ( tx, "Alice", "Wayne Enterprises" ) );
session1.writeTransaction( tx -> addCompany( tx, "Wayne Enterprises" ).consume() );
session1.writeTransaction( tx -> addPerson( tx, "Alice" ).consume() );
session1.writeTransaction( tx -> employ( tx, "Alice", "Wayne Enterprises" ).consume() );

savedBookmarks.add( session1.lastBookmark() );
}

// Create the second person and employment relationship.
try ( Session session2 = driver.session( builder().withDefaultAccessMode( AccessMode.WRITE ).build() ) )
{
session2.writeTransaction( tx -> addCompany( tx, "LexCorp" ) );
session2.writeTransaction( tx -> addPerson( tx, "Bob" ) );
session2.writeTransaction( tx -> employ( tx, "Bob", "LexCorp" ) );
session2.writeTransaction( tx -> addCompany( tx, "LexCorp" ).consume() );
session2.writeTransaction( tx -> addPerson( tx, "Bob" ).consume() );
session2.writeTransaction( tx -> employ( tx, "Bob", "LexCorp" ).consume() );

savedBookmarks.add( session2.lastBookmark() );
}

// Create a friendship between the two people created above.
try ( Session session3 = driver.session( builder().withDefaultAccessMode( AccessMode.WRITE ).withBookmarks( savedBookmarks ).build() ) )
{
session3.writeTransaction( tx -> makeFriends( tx, "Alice", "Bob" ) );
session3.writeTransaction( tx -> makeFriends( tx, "Alice", "Bob" ).consume() );

session3.readTransaction( this::printFriends );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@

// tag::read-write-transaction-import[]

import org.neo4j.driver.Session;
import org.neo4j.driver.Result;
import org.neo4j.driver.Session;
import org.neo4j.driver.Transaction;
import org.neo4j.driver.TransactionWork;

import static org.neo4j.driver.Values.parameters;
// end::read-write-transaction-import[]
Expand All @@ -40,28 +39,14 @@ public long addPerson( final String name )
{
try ( Session session = driver.session() )
{
session.writeTransaction( new TransactionWork<Void>()
{
@Override
public Void execute( Transaction tx )
{
return createPersonNode( tx, name );
}
} );
return session.readTransaction( new TransactionWork<Long>()
{
@Override
public Long execute( Transaction tx )
{
return matchPersonNode( tx, name );
}
} );
session.writeTransaction( tx -> createPersonNode( tx, name ) );
return session.readTransaction( tx -> matchPersonNode( tx, name ) );
}
}

private static Void createPersonNode( Transaction tx, String name )
{
tx.run( "CREATE (a:Person {name: $name})", parameters( "name", name ) );
tx.run( "CREATE (a:Person {name: $name})", parameters( "name", name ) ).consume();
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.List;

import org.neo4j.driver.Record;
import org.neo4j.driver.Result;
import org.neo4j.driver.Session;
import org.neo4j.driver.Transaction;
import org.neo4j.driver.TransactionWork;
Expand Down Expand Up @@ -53,19 +54,16 @@ public List<Record> execute( Transaction tx )
} );
for ( final Record person : persons )
{
employees += session.writeTransaction( new TransactionWork<Integer>()
{
@Override
public Integer execute( Transaction tx )
{
tx.run( "MATCH (emp:Person {name: $person_name}) " +
"MERGE (com:Company {name: $company_name}) " +
"MERGE (emp)-[:WORKS_FOR]->(com)",
parameters( "person_name", person.get( "name" ).asString(), "company_name",
companyName ) );
return 1;
}
} );
employees += session.writeTransaction( tx ->
{
Result result = tx.run( "MATCH (emp:Person {name: $person_name}) " +
"MERGE (com:Company {name: $company_name}) " +
"MERGE (emp)-[:WORKS_FOR]->(com)",
parameters( "person_name", person.get( "name" ).asString(), "company_name",
companyName ) );
result.consume();
return 1;
} );
}
return employees;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import org.neo4j.driver.GraphDatabase;
import org.neo4j.driver.Logging;
import org.neo4j.driver.Session;
import org.neo4j.driver.Transaction;
import org.neo4j.driver.TransactionWork;
import org.neo4j.driver.exceptions.ServiceUnavailableException;

import static java.util.concurrent.TimeUnit.SECONDS;
Expand Down Expand Up @@ -58,15 +56,11 @@ public boolean addItem()
{
try ( Session session = driver.session() )
{
return session.writeTransaction( new TransactionWork<Boolean>()
{
@Override
public Boolean execute( Transaction tx )
{
tx.run( "CREATE (a:Item)" );
return true;
}
} );
return session.writeTransaction( tx ->
{
tx.run( "CREATE (a:Item)" ).consume();
return true;
} );
}
catch ( ServiceUnavailableException ex )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void addPerson( final String name )
try ( Session session = driver.session() )
{
session.writeTransaction( tx -> {
tx.run( "CREATE (a:Person {name: $name})", parameters( "name", name ) );
tx.run( "CREATE (a:Person {name: $name})", parameters( "name", name ) ).consume();
return 1;
} );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void addPerson( final String name )
.withMetadata( transactionMetadata ).build();
session.writeTransaction( tx ->
{
tx.run( "CREATE (a:Person {name: $name})", parameters( "name", name ) );
tx.run( "CREATE (a:Person {name: $name})", parameters( "name", name ) ).consume();
return 1;
}, txConfig );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private void write( final String query, final Value parameters )
{
session.writeTransaction( tx ->
{
tx.run( query, parameters );
tx.run( query, parameters ).consume();
return null;
} );
}
Expand Down