Skip to content

Commit ffef58a

Browse files
gjmwoodsrobsdedude
andauthored
Add examples for configuring transactions (#550) (#552)
* Add examples for configuring transactions Co-authored-by: Robsdedude <[email protected]>
1 parent cb86c29 commit ffef58a

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/env python
2+
# -*- encoding: utf-8 -*-
3+
4+
# Copyright (c) "Neo4j"
5+
# Neo4j Sweden AB [http://neo4j.com]
6+
#
7+
# This file is part of Neo4j.
8+
#
9+
# Licensed under the Apache License, Version 2.0 (the "License");
10+
# you may not use this file except in compliance with the License.
11+
# You may obtain a copy of the License at
12+
#
13+
# http://www.apache.org/licenses/LICENSE-2.0
14+
#
15+
# Unless required by applicable law or agreed to in writing, software
16+
# distributed under the License is distributed on an "AS IS" BASIS,
17+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
# See the License for the specific language governing permissions and
19+
# limitations under the License.
20+
21+
from neo4j import unit_of_work, Query
22+
23+
24+
# python -m pytest tests/integration/examples/test_transaction_metadata_config_example.py -s -v
25+
26+
# tag::transaction-metadata-config[]
27+
@unit_of_work(timeout=5, metadata={"applicationId": "123"})
28+
def create_person(tx, name):
29+
return tx.run(
30+
"CREATE (a:Person {name: $name}) RETURN id(a)", name=name
31+
).single().value()
32+
33+
34+
def add_person(driver, name):
35+
with driver.session() as session:
36+
return session.write_transaction(create_person, name)
37+
# end::transaction-metadata-config[]
38+
39+
40+
class TransactionMetadataConfigExample:
41+
42+
def __init__(self, driver):
43+
self.driver = driver
44+
45+
def add_person(self, name):
46+
return add_person(self.driver, name)
47+
48+
49+
def test_example(bolt_driver):
50+
eg = TransactionMetadataConfigExample(bolt_driver)
51+
with eg.driver.session() as session:
52+
session.run("MATCH (_) DETACH DELETE _")
53+
eg.add_person("Alice")
54+
n = session.run("MATCH (a:Person) RETURN count(a)").single().value()
55+
assert n == 1
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/env python
2+
# -*- encoding: utf-8 -*-
3+
4+
# Copyright (c) "Neo4j"
5+
# Neo4j Sweden AB [http://neo4j.com]
6+
#
7+
# This file is part of Neo4j.
8+
#
9+
# Licensed under the Apache License, Version 2.0 (the "License");
10+
# you may not use this file except in compliance with the License.
11+
# You may obtain a copy of the License at
12+
#
13+
# http://www.apache.org/licenses/LICENSE-2.0
14+
#
15+
# Unless required by applicable law or agreed to in writing, software
16+
# distributed under the License is distributed on an "AS IS" BASIS,
17+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
# See the License for the specific language governing permissions and
19+
# limitations under the License.
20+
21+
from neo4j import unit_of_work, Query
22+
23+
24+
# python -m pytest tests/integration/examples/test_transaction_timeout_config_example.py -s -v
25+
26+
# tag::transaction-timeout-config[]
27+
@unit_of_work(timeout=5)
28+
def create_person(tx, name):
29+
return tx.run(
30+
"CREATE (a:Person {name: $name}) RETURN id(a)", name=name
31+
).single().value()
32+
33+
34+
def add_person(driver, name):
35+
with driver.session() as session:
36+
return session.write_transaction(create_person, name)
37+
# end::transaction-timeout-config[]
38+
39+
40+
class TransactionTimeoutConfigExample:
41+
42+
def __init__(self, driver):
43+
self.driver = driver
44+
45+
def add_person(self, name):
46+
return add_person(self.driver, name)
47+
48+
49+
def test_example(bolt_driver):
50+
eg = TransactionTimeoutConfigExample(bolt_driver)
51+
with eg.driver.session() as session:
52+
session.run("MATCH (_) DETACH DELETE _")
53+
eg.add_person("Alice")
54+
n = session.run("MATCH (a:Person) RETURN count(a)").single().value()
55+
assert n == 1

0 commit comments

Comments
 (0)