Skip to content

Commit 64e2452

Browse files
committed
Session and transaction updates
1 parent 6dcbc18 commit 64e2452

File tree

8 files changed

+217
-90
lines changed

8 files changed

+217
-90
lines changed
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#!/usr/bin/env bash
22

3-
HOME=$(dirname $0)
3+
ROOT=$(dirname "$0")/..
44

5-
pip install --upgrade sphinx
6-
make -C ${HOME}/docs html
5+
pip install --quiet --upgrade sphinx
6+
make -C ${ROOT}/docs html
77

88
echo ""
9-
INDEX_FILE="${HOME}/docs/build/html/index.html"
9+
INDEX_FILE="${ROOT}/docs/build/html/index.html"
1010
echo "Documentation index file can be found at file://$(cd "$(dirname "${INDEX_FILE}")"; pwd)/$(basename "${INDEX_FILE}")"

docs/source/conf.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
'sphinx.ext.todo',
3838
'sphinx.ext.coverage',
3939
'sphinx.ext.ifconfig',
40-
'sphinx.ext.viewcode',
4140
'sphinx.ext.intersphinx',
4241
]
4342

docs/source/driver.rst

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ Construction
1313
:class:`.Driver` construction can either be carried out directly or via a `classmethod` on the :class:`.GraphDatabase` class.
1414

1515
.. autoclass:: neo4j.GraphDatabase
16-
:members:
16+
:members: driver
1717

1818
.. autoclass:: neo4j.Driver(uri, **config)
19-
:members:
19+
:members: session, close, closed
2020

2121

2222
URI
@@ -37,14 +37,7 @@ URI scheme:
3737
Driver subclass:
3838
:class:`.DirectDriver`
3939

40-
A Bolt :class:`.DirectDriver` is used to target a single machine.
41-
This may be a standalone server or could be a specific member of a cluster.
42-
43-
Connections established by a :class:`.DirectDriver` are always made to the exact host and port detailed in the URI.
44-
4540
.. autoclass:: neo4j.DirectDriver
46-
:members:
47-
:inherited-members:
4841

4942

5043
Bolt Routing
@@ -56,8 +49,6 @@ Driver subclass:
5649
:class:`.RoutingDriver`
5750

5851
.. autoclass:: neo4j.RoutingDriver
59-
:members:
60-
:inherited-members:
6152

6253

6354
Configuration

docs/source/errors.rst

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,42 @@
22
Errors
33
******
44

5-
.. autoclass:: neo4j.exceptions.CypherError
6-
:members:
75

8-
.. autoclass:: neo4j.exceptions.ProtocolError
9-
:members:
6+
Connectivity errors
7+
===================
108

11-
.. autoclass:: neo4j.exceptions.SecurityError
12-
:members:
9+
.. class:: neo4j.exceptions.ServiceUnavailable
1310

14-
.. autoclass:: neo4j.exceptions.ServiceUnavailable
15-
:members:
11+
Raised when a database server or service is not available.
12+
This may be due to incorrect configuration or could indicate a runtime failure of a database service that the driver is unable to route around.
13+
14+
.. class:: neo4j.exceptions.SecurityError
15+
16+
Raised when a security issue occurs, generally around TLS or authentication.
17+
18+
19+
Cypher execution errors
20+
=======================
21+
22+
.. class:: neo4j.exceptions.CypherError
23+
24+
Raised when the Cypher engine returns an error to the client.
25+
There are many possible types of Cypher error, each identified by a unique `status code <https://neo4j.com/docs/developer-manual/current/reference/status-codes/>`_.
26+
27+
The three classifications of status code are supported by the three subclasses of :class:`.CypherError`, listed below:
1628

1729
.. autoclass:: neo4j.exceptions.ClientError
18-
:show-inheritance:
19-
:members:
2030

2131
.. autoclass:: neo4j.exceptions.DatabaseError
22-
:show-inheritance:
23-
:members:
2432

2533
.. autoclass:: neo4j.exceptions.TransientError
26-
:show-inheritance:
27-
:members:
34+
35+
36+
Low-level errors
37+
================
38+
39+
.. class:: neo4j.exceptions.ProtocolError
40+
41+
Raised when an unexpected or unsupported protocol event occurs.
42+
This error generally indicates a fault with the driver or server software.
43+
If you receive this error, please raise a GitHub issue or a support ticket.

docs/source/index.rst

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
Neo4j Bolt Driver |version| for Python
33
**************************************
44

5-
The Official Neo4j Driver for Python supports Neo4j 3.1 and above and requires Python version 2.7, 3.4, 3.5 or 3.6.
5+
The Official Neo4j Driver for Python supports Neo4j 3.2 and above and requires Python version 2.7 or 3.4+.
6+
Note that support for Python 2.7 will be removed in the 2.0 driver.
67

78

89
Quick Example
@@ -25,20 +26,25 @@ Quick Example
2526
session.read_transaction(print_friends_of, "Alice")
2627
2728
29+
.. note::
30+
31+
While imports from ``neo4j.v1`` still work, these will be removed in the 2.0 driver.
32+
It is therefore recommended to change all imports from ``neo4j.v1`` to ``neo4j``.
33+
34+
2835
Installation
2936
============
3037

31-
To install the latest stable version, use:
38+
To install the latest stable driver release, use:
3239

3340
.. code:: bash
3441
3542
pip install neo4j
3643
37-
For the most up-to-date version (possibly unstable), use:
38-
39-
.. code:: bash
44+
.. note::
4045

41-
pip install git+https://github.com/neo4j/neo4j-python-driver.git#egg=neo4j
46+
The driver is currently released under two package names on `PyPI <https://pypi.org/>`_: ``neo4j`` and ``neo4j-driver``.
47+
Installing from ``neo4j`` is recommended since ``neo4j-driver`` will be removed in a future release.
4248

4349

4450
API Documentation

docs/source/transactions.rst

Lines changed: 105 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,65 @@
22
Sessions & Transactions
33
***********************
44

5+
All database activity is co-ordinated through two mechanisms: the :class:`.Session` and the :class:`.Transaction`.
56
A :class:`.Transaction` is a unit of work that is either committed in its entirety or is rolled back on failure.
6-
A :class:`.Session` is a logical container for one or more transactional units of work.
7-
Sessions automatically provide guarantees of causal consistency within a clustered environment.
7+
A :class:`.Session` is a logical container for any number of causally-related transactional units of work.
8+
Sessions automatically provide guarantees of causal consistency within a clustered environment but multiple sessions can also be causally chained if required.
89

9-
A session can be given a default `access mode` on construction.
10-
This applies only in clustered environments and determines whether transactions carried out within that session should be routed to a `read` or `write` server.
11-
Transaction functions within that session can override this access mode.
1210

13-
.. note::
14-
The driver does not parse Cypher statements and cannot determine whether a statement tagged as `read` or `write` is tagged correctly.
15-
Since the access mode is not passed to the server, this can allow a `write` statement to be executed in a `read` call on a single instance.
16-
Clustered environments are not susceptible to this loophole as cluster roles prevent it.
11+
Sessions
12+
========
13+
14+
Sessions provide the top-level of containment for database activity.
15+
Session creation is a lightweight operation and sessions are `not` thread safe.
16+
17+
Connections are drawn from the :class:`.Driver` connection pool as required; an idle session will not hold onto a connection.
18+
19+
Sessions will often be created and destroyed using a `with` block context.
20+
For example::
21+
22+
with driver.session() as session:
23+
result = session.run("MATCH (a:Person) RETURN a.name")
24+
# do something with the result...
25+
26+
To construct a :class:`.Session` use the :meth:`.Driver.session` method.
27+
28+
.. class:: neo4j.Session
29+
30+
.. automethod:: close
31+
32+
.. automethod:: closed
33+
34+
.. automethod:: run
35+
36+
.. automethod:: sync
37+
38+
.. automethod:: detach
39+
40+
.. automethod:: next_bookmarks
41+
42+
.. automethod:: last_bookmark
43+
44+
.. automethod:: has_transaction
45+
46+
.. automethod:: begin_transaction
47+
48+
.. automethod:: read_transaction
49+
50+
.. automethod:: write_transaction
51+
52+
53+
Transactions
54+
============
1755

1856
Neo4j supports three kinds of transaction: `auto-commit transactions`, `explicit transactions` and `transaction functions`.
1957
Each has pros and cons but if in doubt, use a transaction function.
2058

2159
Auto-commit Transactions
22-
========================
23-
Auto-commit transactions are the simplest form, available via :meth:`.Session.run`.
24-
These are fast and easy to use but support only one statement per transaction and are not automatically retried on failure.
25-
Auto-commit transactions are also the only way to run ``USING PERIODIC COMMIT`` statements.
60+
------------------------
61+
Auto-commit transactions are the simplest form of transaction, available via :meth:`.Session.run`.
62+
These are easy to use but support only one statement per transaction and are not automatically retried on failure.
63+
Auto-commit transactions are also the only way to run ``PERIODIC COMMIT`` statements, since this Cypher clause manages its own transactions internally.
2664

2765
.. code-block:: python
2866
@@ -32,8 +70,43 @@ Auto-commit transactions are also the only way to run ``USING PERIODIC COMMIT``
3270
"RETURN id(a)", name=name).single().value()
3371
3472
Explicit Transactions
35-
=====================
73+
---------------------
3674
Explicit transactions support multiple statements and must be created with an explicit :meth:`.Session.begin_transaction` call.
75+
This creates a new :class:`.Transaction` object that can be used to run Cypher.
76+
It also gives applications the ability to directly control `commit` and `rollback` activity.
77+
78+
.. class:: neo4j.Transaction
79+
80+
.. automethod:: run
81+
82+
.. automethod:: sync
83+
84+
.. attribute:: success
85+
86+
This attribute can be used to determine the outcome of a transaction on closure.
87+
Specifically, this will be either a COMMIT or a ROLLBACK.
88+
A value can be set for this attribute multiple times in user code before a transaction completes, with only the final value taking effect.
89+
90+
On closure, the outcome is evaluated according to the following rules:
91+
92+
================ ==================== =========================== ============== =============== =================
93+
:attr:`.success` ``__exit__`` cleanly ``__exit__`` with exception ``tx.close()`` ``tx.commit()`` ``tx.rollback()``
94+
================ ==================== =========================== ============== =============== =================
95+
:const:`None` COMMIT ROLLBACK ROLLBACK COMMIT ROLLBACK
96+
:const:`True` COMMIT COMMIT [1]_ COMMIT COMMIT ROLLBACK
97+
:const:`False` ROLLBACK ROLLBACK ROLLBACK COMMIT ROLLBACK
98+
================ ==================== =========================== ============== =============== =================
99+
100+
.. [1] While a COMMIT will be attempted in this scenario, it will likely fail if the exception originated from Cypher execution within that transaction.
101+
102+
.. automethod:: close
103+
104+
.. automethod:: closed
105+
106+
.. automethod:: commit
107+
108+
.. automethod:: rollback
109+
37110
Closing an explicit transaction can either happen automatically at the end of a ``with`` block, using the :attr:`.Transaction.success` attribute to determine success,
38111
or can be explicitly controlled through the :meth:`.Transaction.commit` and :meth:`.Transaction.rollback` methods.
39112
Explicit transactions are most useful for applications that need to distribute Cypher execution across multiple functions for the same transaction.
@@ -56,7 +129,7 @@ Explicit transactions are most useful for applications that need to distribute C
56129
"SET a.name = $name", id=node_id, name=name)
57130
58131
Transaction Functions
59-
=====================
132+
---------------------
60133
Transaction functions are the most powerful form of transaction, providing access mode override and retry capabilities.
61134
These allow a function object representing the transactional unit of work to be passed as a parameter.
62135
This function is called one or more times, within a configurable time limit, until it succeeds.
@@ -72,11 +145,22 @@ Returning a live result object would prevent the driver from correctly managing
72145
with driver.session() as session:
73146
node_id = session.write_transaction(create_person, "Alice")
74147
148+
To exert more control over how a transaction function is carried out, the :func:`.unit_of_work` decorator can be used.
149+
150+
.. autofunction:: neo4j.unit_of_work
75151

76-
API
77-
===
78-
.. autoclass:: neo4j.Session
79-
:members:
80152

81-
.. autoclass:: neo4j.Transaction
82-
:members:
153+
Access modes
154+
============
155+
156+
A session can be given a default `access mode` on construction.
157+
This applies only in clustered environments and determines whether transactions carried out within that session should be routed to a `read` or `write` server by default.
158+
159+
Note that this mode is simply a default and not a constraint.
160+
This means that transaction functions within a session can override the access mode passed to that session on construction.
161+
162+
.. note::
163+
The driver does not parse Cypher statements and cannot determine whether a statement tagged as `read` or `write` is tagged correctly.
164+
Since the access mode is not passed to the server, this can allow a `write` statement to be executed in a `read` call on a single instance.
165+
Clustered environments are not susceptible to this loophole as cluster roles prevent it.
166+
This behaviour should not be relied upon as the loophole may be closed in a future release.

0 commit comments

Comments
 (0)