Skip to content

Commit 910e519

Browse files
committed
Examples with timeout values
1 parent 3c67963 commit 910e519

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

test/examples/autocommit_transaction_example.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818
# See the License for the specific language governing permissions and
1919
# limitations under the License.
2020

21+
2122
from test.examples.base_application import BaseApplication
2223

2324
# tag::autocommit-transaction-import[]
25+
from neo4j import Statement
2426
# end::autocommit-transaction-import[]
2527

2628

@@ -32,4 +34,9 @@ def __init__(self, uri, user, password):
3234
def add_person(self, name):
3335
with self._driver.session() as session:
3436
session.run("CREATE (a:Person {name: $name})", name=name)
37+
38+
# Alternative implementation, with timeout
39+
def add_person_within_half_a_second(self, name):
40+
with self._driver.session() as session:
41+
session.run(Statement("CREATE (a:Person {name: $name})", timeout=0.5), name=name)
3542
# end::autocommit-transaction[]

test/examples/transaction_function_example.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,36 @@
1818
# See the License for the specific language governing permissions and
1919
# limitations under the License.
2020

21+
2122
from test.examples.base_application import BaseApplication
2223

2324
# tag::transaction-function-import[]
25+
from neo4j import unit_of_work
2426
# end::transaction-function-import[]
2527

2628

29+
# tag::transaction-function[]
30+
def add_person(driver, name):
31+
with driver.session() as session:
32+
# Caller for transactional unit of work
33+
return session.write_transaction(create_person_node, name)
34+
35+
36+
# Simple implementation of the unit of work
37+
def create_person_node(tx, name):
38+
return tx.run("CREATE (a:Person {name: $name}) RETURN id(a)", name=name).single().value()
39+
40+
41+
# Alternative implementation, with timeout
42+
@unit_of_work(timeout=0.5)
43+
def create_person_node_within_half_a_second(tx, name):
44+
return tx.run("CREATE (a:Person {name: $name}) RETURN id(a)", name=name).single().value()
45+
# end::transaction-function[]
46+
47+
2748
class TransactionFunctionExample(BaseApplication):
2849
def __init__(self, uri, user, password):
2950
super(TransactionFunctionExample, self).__init__(uri, user, password)
3051

31-
# tag::transaction-function[]
3252
def add_person(self, name):
33-
with self._driver.session() as session:
34-
session.write_transaction(self.create_person_node, name)
35-
36-
@staticmethod
37-
def create_person_node(tx, name):
38-
tx.run("CREATE (a:Person {name: $name})", name=name)
39-
# end::transaction-function[]
53+
return add_person(self._driver, name)

0 commit comments

Comments
 (0)