|
18 | 18 | # See the License for the specific language governing permissions and
|
19 | 19 | # limitations under the License.
|
20 | 20 |
|
| 21 | + |
21 | 22 | from test.examples.base_application import BaseApplication
|
22 | 23 |
|
23 | 24 | # tag::transaction-function-import[]
|
| 25 | +from neo4j import unit_of_work |
24 | 26 | # end::transaction-function-import[]
|
25 | 27 |
|
26 | 28 |
|
| 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 | + |
27 | 48 | class TransactionFunctionExample(BaseApplication):
|
28 | 49 | def __init__(self, uri, user, password):
|
29 | 50 | super(TransactionFunctionExample, self).__init__(uri, user, password)
|
30 | 51 |
|
31 |
| - # tag::transaction-function[] |
32 | 52 | 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