|
| 1 | +.. _transactions: |
| 2 | + |
| 3 | +Transactions |
| 4 | +============ |
| 5 | +Transactions in |owm| are managed through the `transaction`_ library. The |
| 6 | +default RDF store is transactional. You can execute code within a transaction |
| 7 | +using a transaction manager. |owm| connections come with a transaction manager |
| 8 | +which you can access via the `transaction_manager` attribute. It's recommended |
| 9 | +to use a context manager to start and commit transactions like this:: |
| 10 | + |
| 11 | + >>> from rdflib.term import URIRef |
| 12 | + >>> from owmeta_core import connect |
| 13 | + >>> with connect() as conn, conn.transaction_manager: |
| 14 | + ... conn.rdf.add(( |
| 15 | + ... URIRef('http://example.org/bob'), |
| 16 | + ... URIRef('http://example.org/likes'), |
| 17 | + ... URIRef('http://example.org/alice'))) |
| 18 | + |
| 19 | +Because this is a common pattern, there's a |
| 20 | +:meth:`~owmeta_core.Connection.transaction` method that does something |
| 21 | +equivalent which is provided for convenience:: |
| 22 | + |
| 23 | + >>> with connect().transaction() as conn: |
| 24 | + ... conn.rdf.add(( |
| 25 | + ... URIRef('http://example.org/bob'), |
| 26 | + ... URIRef('http://example.org/likes'), |
| 27 | + ... URIRef('http://example.org/alice'))) |
| 28 | + |
| 29 | +Similar usage is possible with project connections through the high-level |
| 30 | +`~owmeta_core.command.OWM` interface:: |
| 31 | + |
| 32 | + >>> from owmeta_core.command import OWM |
| 33 | + >>> owm = OWM(non_interactive=True) |
| 34 | + >>> owm.init(default_context_id=URIRef("http://example.org/context")) |
| 35 | + Initialized owmeta-core project at .../.owm |
| 36 | + |
| 37 | + >>> with owm.connect().transaction() as conn: |
| 38 | + ... conn.rdf.add(( |
| 39 | + ... URIRef('http://example.org/bob'), |
| 40 | + ... URIRef('http://example.org/likes'), |
| 41 | + ... URIRef('http://example.org/alice'))) |
| 42 | + |
| 43 | +However, the methods of `~owmeta_core.command.OWM` and its "sub-commands" will |
| 44 | +typically manage the transactions themselves, so it wouldn't be necessary to |
| 45 | +start a transaction explicitly before calling these methods--in fact, doing so |
| 46 | +would typically cause an exception. For example, in this code:: |
| 47 | + |
| 48 | + >>> owm.say('http://example.org/bob', |
| 49 | + ... 'http://example.org/likes', |
| 50 | + ... 'http://example.org/eve') |
| 51 | + |
| 52 | +we don't have to declare a transaction since the `~owmeta_core.command.OWM.say` |
| 53 | +method handles that for us. |
| 54 | + |
| 55 | +For read-only operations, it is not strictly necessary to read from the RDF |
| 56 | +store within the context of a transaction, but it is recommended if you're in a |
| 57 | +multithreaded context to avoid getting an inconsistent picture of the data if |
| 58 | +there's an update part way through your operation. |
| 59 | + |
| 60 | +.. _transaction: https://transaction.readthedocs.io/en/latest/ |
0 commit comments