| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
"""Session Handling for SQLAlchemy backend. |
| 18 |
|
| 19 |
Recommended ways to use sessions within this framework: |
| 20 |
|
| 21 |
* Use the ``enginefacade`` system for connectivity, session and |
| 22 |
transaction management: |
| 23 |
|
| 24 |
.. code-block:: python |
| 25 |
|
| 26 |
from oslo_db.sqlalchemy import enginefacade |
| 27 |
|
| 28 |
@enginefacade.reader |
| 29 |
def get_foo(context, foo): |
| 30 |
return (model_query(models.Foo, context.session). |
| 31 |
filter_by(foo=foo). |
| 32 |
first()) |
| 33 |
|
| 34 |
@enginefacade.writer |
| 35 |
def update_foo(context, id, newfoo): |
| 36 |
(model_query(models.Foo, context.session). |
| 37 |
filter_by(id=id). |
| 38 |
update({'foo': newfoo})) |
| 39 |
|
| 40 |
@enginefacade.writer |
| 41 |
def create_foo(context, values): |
| 42 |
foo_ref = models.Foo() |
| 43 |
foo_ref.update(values) |
| 44 |
foo_ref.save(context.session) |
| 45 |
return foo_ref |
| 46 |
|
| 47 |
In the above system, transactions are committed automatically, and |
| 48 |
are shared among all dependent database methods. Ensure |
| 49 |
that methods which "write" data are enclosed within @writer blocks. |
| 50 |
|
| 51 |
.. note:: Statements in the session scope will not be automatically retried. |
| 52 |
|
| 53 |
* If you create models within the session, they need to be added, but you |
| 54 |
do not need to call `model.save()`: |
| 55 |
|
| 56 |
.. code-block:: python |
| 57 |
|
| 58 |
@enginefacade.writer |
| 59 |
def create_many_foo(context, foos): |
| 60 |
for foo in foos: |
| 61 |
foo_ref = models.Foo() |
| 62 |
foo_ref.update(foo) |
| 63 |
context.session.add(foo_ref) |
| 64 |
|
| 65 |
@enginefacade.writer |
| 66 |
def update_bar(context, foo_id, newbar): |
| 67 |
foo_ref = (model_query(models.Foo, context.session). |
| 68 |
filter_by(id=foo_id). |
| 69 |
first()) |
| 70 |
(model_query(models.Bar, context.session). |
| 71 |
filter_by(id=foo_ref['bar_id']). |
| 72 |
update({'bar': newbar})) |
| 73 |
|
| 74 |
The two queries in `update_bar` can alternatively be expressed using |
| 75 |
a single query, which may be more efficient depending on scenario: |
| 76 |
|
| 77 |
.. code-block:: python |
| 78 |
|
| 79 |
@enginefacade.writer |
| 80 |
def update_bar(context, foo_id, newbar): |
| 81 |
subq = (model_query(models.Foo.id, context.session). |
| 82 |
filter_by(id=foo_id). |
| 83 |
limit(1). |
| 84 |
subquery()) |
| 85 |
(model_query(models.Bar, context.session). |
| 86 |
filter_by(id=subq.as_scalar()). |
| 87 |
update({'bar': newbar})) |
| 88 |
|
| 89 |
For reference, this emits approximately the following SQL statement: |
| 90 |
|
| 91 |
.. code-block:: sql |
| 92 |
|
| 93 |
UPDATE bar SET bar = '${newbar}' |
| 94 |
WHERE id=(SELECT bar_id FROM foo WHERE id = '${foo_id}' LIMIT 1); |
| 95 |
|
| 96 |
.. note:: `create_duplicate_foo` is a trivially simple example of catching an |
| 97 |
exception while using a savepoint. Here we create two duplicate |
| 98 |
instances with same primary key, must catch the exception out of context |
| 99 |
managed by a single session: |
| 100 |
|
| 101 |
.. code-block:: python |
| 102 |
|
| 103 |
@enginefacade.writer |
| 104 |
def create_duplicate_foo(context): |
| 105 |
foo1 = models.Foo() |
| 106 |
foo2 = models.Foo() |
| 107 |
foo1.id = foo2.id = 1 |
| 108 |
try: |
| 109 |
with context.session.begin_nested(): |
| 110 |
session.add(foo1) |
| 111 |
session.add(foo2) |
| 112 |
except exception.DBDuplicateEntry as e: |
| 113 |
handle_error(e) |
| 114 |
|
| 115 |
* The enginefacade system eliminates the need to decide when sessions need |
| 116 |
to be passed between methods. All methods should instead share a common |
| 117 |
context object; the enginefacade system will maintain the transaction |
| 118 |
across method calls. |
| 119 |
|
| 120 |
.. code-block:: python |
| 121 |
|
| 122 |
@enginefacade.writer |
| 123 |
def myfunc(context, foo): |
| 124 |
# do some database things |
| 125 |
bar = _private_func(context, foo) |
| 126 |
return bar |
| 127 |
|
| 128 |
def _private_func(context, foo): |
| 129 |
with enginefacade.using_writer(context) as session: |
| 130 |
# do some other database things |
| 131 |
session.add(SomeObject()) |
| 132 |
return bar |
| 133 |
|
| 134 |
|
| 135 |
* Avoid ``with_lockmode('UPDATE')`` when possible. |
| 136 |
|
| 137 |
FOR UPDATE is not compatible with MySQL/Galera. Instead, an "opportunistic" |
| 138 |
approach should be used, such that if an UPDATE fails, the entire |
| 139 |
transaction should be retried. The @wrap_db_retry decorator is one |
| 140 |
such system that can be used to achieve this. |
| 141 |
|
| 142 |
Enabling soft deletes: |
| 143 |
|
| 144 |
* To use/enable soft-deletes, `SoftDeleteMixin` may be used. For example: |
| 145 |
|
| 146 |
.. code-block:: python |
| 147 |
|
| 148 |
class NovaBase(models.SoftDeleteMixin, models.ModelBase): |
| 149 |
pass |
| 150 |
|
| 151 |
|
| 152 |
Efficient use of soft deletes: |
| 153 |
|
| 154 |
* While there is a ``model.soft_delete()`` method, prefer |
| 155 |
``query.soft_delete()``. Some examples: |
| 156 |
|
| 157 |
.. code-block:: python |
| 158 |
|
| 159 |
@enginefacade.writer |
| 160 |
def soft_delete_bar(context): |
| 161 |
# synchronize_session=False will prevent the ORM from attempting |
| 162 |
# to search the Session for instances matching the DELETE; |
| 163 |
# this is typically not necessary for small operations. |
| 164 |
count = model_query(BarModel, context.session).\\ |
| 165 |
find(some_condition).soft_delete(synchronize_session=False) |
| 166 |
if count == 0: |
| 167 |
raise Exception("0 entries were soft deleted") |
| 168 |
|
| 169 |
@enginefacade.writer |
| 170 |
def complex_soft_delete_with_synchronization_bar(context): |
| 171 |
# use synchronize_session='evaluate' when you'd like to attempt |
| 172 |
# to update the state of the Session to match that of the DELETE. |
| 173 |
# This is potentially helpful if the operation is complex and |
| 174 |
# continues to work with instances that were loaded, though |
| 175 |
# not usually needed. |
| 176 |
count = (model_query(BarModel, context.session). |
| 177 |
find(some_condition). |
| 178 |
soft_delete(synchronize_session='evaulate')) |
| 179 |
if count == 0: |
| 180 |
raise Exception("0 entries were soft deleted") |
| 181 |
|
| 182 |
|
| 183 |
""" |
| 184 |
|
| 185 |
from oslo_db.sqlalchemy import engines |
| 186 |
from oslo_db.sqlalchemy import orm |
| 187 |
|
| 188 |
create_engine = engines.create_engine |
| 189 |
get_maker = orm.get_maker |
| 190 |
Query = orm.Query |
| 191 |
Session = orm.Session |
| 192 |
|
| 193 |
|
| 194 |
__all__ = ["create_engine", "get_maker", "Query", "Session"] |