|
| 1 | +============== |
| 2 | +Driver Objects |
| 3 | +============== |
| 4 | + |
| 5 | +Every Neo4j-backed application will require a :class:`.Driver` object. |
| 6 | +This object holds the details required to establish connections with a Neo4j database, including server URIs, credentials and other configuration. |
| 7 | +:class:`.Driver` objects hold a connection pool from which :class:`.Session` objects can borrow connections. |
| 8 | +Closing a driver will immediately shut down all connections in the pool. |
| 9 | + |
| 10 | +Construction |
| 11 | +============ |
| 12 | + |
| 13 | +:class:`.Driver` construction can either be carried out directly or via a `classmethod` on the :class:`.GraphDatabase` class. |
| 14 | + |
| 15 | +.. autoclass:: neo4j.v1.GraphDatabase |
| 16 | + :members: |
| 17 | + |
| 18 | +.. autoclass:: neo4j.v1.Driver(uri, **config) |
| 19 | + :members: |
| 20 | + |
| 21 | + |
| 22 | +URI |
| 23 | +=== |
| 24 | + |
| 25 | +On construction, the scheme of the URI determines the type of :class:`.Driver` object created. |
| 26 | +Each supported scheme maps to a particular :class:`.Driver` subclass that implements a specific behaviour. |
| 27 | +The remainder of the URI should be considered subclass-specific. |
| 28 | + |
| 29 | +The alternative behaviours are described in the subsections below. |
| 30 | + |
| 31 | + |
| 32 | +Bolt Direct |
| 33 | +----------- |
| 34 | + |
| 35 | +URI scheme: |
| 36 | + ``bolt`` |
| 37 | +Driver subclass: |
| 38 | + :class:`.DirectDriver` |
| 39 | + |
| 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 | + |
| 45 | +.. autoclass:: neo4j.v1.DirectDriver |
| 46 | + :members: |
| 47 | + :inherited-members: |
| 48 | + |
| 49 | + |
| 50 | +Bolt Routing |
| 51 | +------------ |
| 52 | + |
| 53 | +URI scheme: |
| 54 | + ``bolt+routing`` |
| 55 | +Driver subclass: |
| 56 | + :class:`.RoutingDriver` |
| 57 | + |
| 58 | +.. autoclass:: neo4j.v1.RoutingDriver |
| 59 | + :members: |
| 60 | + :inherited-members: |
| 61 | + |
| 62 | + |
| 63 | +Configuration |
| 64 | +============= |
| 65 | + |
| 66 | +Additional configuration, including authentication details, can be provided via the :class:`.Driver` constructor. |
| 67 | + |
| 68 | +``auth`` |
| 69 | +-------- |
| 70 | + |
| 71 | +An authentication token for the server. |
| 72 | +For basic auth, this can be a simple tuple, for example ``("neo4j", "password")``. |
| 73 | +Alternatively, one of the auth token functions can be used. |
| 74 | + |
| 75 | +.. autofunction:: neo4j.v1.basic_auth |
| 76 | + |
| 77 | +.. autofunction:: neo4j.v1.custom_auth |
| 78 | + |
| 79 | +``encrypted`` |
| 80 | +------------- |
| 81 | + |
| 82 | +A boolean indicating whether or not TLS should be used for connections. |
| 83 | +Defaults to :py:const:`True` if TLS is available. |
| 84 | + |
| 85 | +``trust`` |
| 86 | +--------- |
| 87 | + |
| 88 | +The trust level for certificates received from the server during TLS negotiation. |
| 89 | +This setting does not have any effect if ``encrypted`` is set to :py:const:`False`. |
| 90 | + |
| 91 | +.. py:attribute:: neo4j.v1.TRUST_ALL_CERTIFICATES |
| 92 | +
|
| 93 | + Trust any server certificate (default). This ensures that communication |
| 94 | + is encrypted but does not verify the server certificate against a |
| 95 | + certificate authority. This option is primarily intended for use with |
| 96 | + the default auto-generated server certificate. |
| 97 | + |
| 98 | +.. py:attribute:: neo4j.v1.TRUST_SYSTEM_CA_SIGNED_CERTIFICATES |
| 99 | +
|
| 100 | + Trust server certificates that can be verified against the system |
| 101 | + certificate authority. This option is primarily intended for use with |
| 102 | + full certificates. |
| 103 | + |
| 104 | +``der_encoded_server_certificate`` |
| 105 | +---------------------------------- |
| 106 | + |
| 107 | +The server certificate in DER format, if required. |
| 108 | + |
| 109 | +``user_agent`` |
| 110 | +-------------- |
| 111 | + |
| 112 | +A custom user agent string, if required. |
| 113 | +The driver will generate a user agent if none is supplied. |
| 114 | + |
| 115 | +``max_connection_lifetime`` |
| 116 | +--------------------------- |
| 117 | + |
| 118 | +The maximum time for which a connection can exist before being closed on release, instead of returned to the pool. |
| 119 | + |
| 120 | +``max_connection_pool_size`` |
| 121 | +---------------------------- |
| 122 | + |
| 123 | +The maximum number of connections managed by the connection pool |
| 124 | + |
| 125 | +``connection_acquisition_timeout`` |
| 126 | +---------------------------------- |
| 127 | + |
| 128 | +The maximum time to wait for a connection to be acquired from the pool. |
| 129 | + |
| 130 | +``connection_timeout`` |
| 131 | +---------------------- |
| 132 | + |
| 133 | +The maximum time to wait for a new connection to be established. |
| 134 | + |
| 135 | +``keep_alive`` |
| 136 | +-------------- |
| 137 | + |
| 138 | +Flag to indicate whether or not the TCP `KEEP_ALIVE` setting should be used. |
| 139 | + |
| 140 | +``max_retry_time`` |
| 141 | +------------------ |
| 142 | + |
| 143 | +The maximum time to allow for retries to be attempted when using transaction functions. |
| 144 | +After this time, no more retries will be attempted. |
| 145 | +This setting does not terminate running queries. |
| 146 | + |
| 147 | + |
| 148 | + |
| 149 | +Object Lifetime |
| 150 | +=============== |
| 151 | + |
| 152 | +For general applications, it is recommended to create one top-level :class:`.Driver` object that lives for the lifetime of the application. |
| 153 | +For example: |
| 154 | + |
| 155 | +.. code-block:: python |
| 156 | +
|
| 157 | + from neo4j.v1 import GraphDatabase |
| 158 | +
|
| 159 | + class Application(object): |
| 160 | +
|
| 161 | + def __init__(self, uri, user, password) |
| 162 | + self.driver = GraphDatabase.driver(uri, auth=(user, password)) |
| 163 | +
|
| 164 | + def close(self): |
| 165 | + self.driver.close() |
| 166 | +
|
| 167 | +Connection details held by the :class:`.Driver` are immutable. |
| 168 | +Therefore if, for example, a password is changed, a replacement :class:`.Driver` object must be created. |
| 169 | +More than one :class:`.Driver` may be required if connections to multiple databases, or connections as multiple users, are required. |
| 170 | + |
| 171 | +:class:`.Driver` objects are thread-safe but cannot be shared across processes. |
| 172 | +Therefore, ``multithreading`` should generally be preferred over ``multiprocessing`` for parallel database access. |
| 173 | +If using ``multiprocessing`` however, each process will require its own :class:`.Driver` object. |
0 commit comments