|
| 1 | +# |
| 2 | +# Copyright 2021 GridGain Systems, Inc. and Contributors. |
| 3 | +# |
| 4 | +# Licensed under the GridGain Community Edition License (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# https://www.gridgain.com/products/software/community-edition/gridgain-community-edition-license |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | +import asyncio |
| 17 | +import sys |
| 18 | +import time |
| 19 | + |
| 20 | +from pygridgain import AioClient, Client |
| 21 | +from pygridgain.datatypes import TransactionIsolation, TransactionConcurrency |
| 22 | +from pygridgain.datatypes.prop_codes import PROP_CACHE_ATOMICITY_MODE, PROP_NAME |
| 23 | +from pygridgain.datatypes.cache_config import CacheAtomicityMode |
| 24 | +from pygridgain.exceptions import CacheError |
| 25 | + |
| 26 | + |
| 27 | +async def async_example(): |
| 28 | + client = AioClient() |
| 29 | + async with client.connect('127.0.0.1', 10800): |
| 30 | + cache = await client.get_or_create_cache({ |
| 31 | + PROP_NAME: 'tx_cache', |
| 32 | + PROP_CACHE_ATOMICITY_MODE: CacheAtomicityMode.TRANSACTIONAL |
| 33 | + }) |
| 34 | + |
| 35 | + # starting transaction |
| 36 | + key = 1 |
| 37 | + async with client.tx_start( |
| 38 | + isolation=TransactionIsolation.REPEATABLE_READ, concurrency=TransactionConcurrency.PESSIMISTIC |
| 39 | + ) as tx: |
| 40 | + await cache.put(key, 'success') |
| 41 | + await tx.commit() |
| 42 | + |
| 43 | + # key=1 value=success |
| 44 | + val = await cache.get(key) |
| 45 | + print(f"key=1 value={val}") |
| 46 | + |
| 47 | + # rollback transaction. |
| 48 | + try: |
| 49 | + async with client.tx_start( |
| 50 | + isolation=TransactionIsolation.REPEATABLE_READ, concurrency=TransactionConcurrency.PESSIMISTIC |
| 51 | + ): |
| 52 | + await cache.put(key, 'fail') |
| 53 | + raise RuntimeError('test') |
| 54 | + except RuntimeError: |
| 55 | + pass |
| 56 | + |
| 57 | + # key=1 value=success |
| 58 | + val = await cache.get(key) |
| 59 | + print(f"key=1 value={val}") |
| 60 | + |
| 61 | + # rollback transaction on timeout. |
| 62 | + try: |
| 63 | + async with client.tx_start(timeout=1.0, label='long-tx') as tx: |
| 64 | + await cache.put(key, 'fail') |
| 65 | + await asyncio.sleep(2.0) |
| 66 | + await tx.commit() |
| 67 | + except CacheError as e: |
| 68 | + # Cache transaction timed out: GridNearTxLocal[...timeout=1000, ... label=long-tx] |
| 69 | + print(e) |
| 70 | + |
| 71 | + # key=1 value=success |
| 72 | + val = await cache.get(key) |
| 73 | + print(f"key=1 value={val}") |
| 74 | + |
| 75 | + # destroy cache |
| 76 | + await cache.destroy() |
| 77 | + |
| 78 | + |
| 79 | +def sync_example(): |
| 80 | + client = Client() |
| 81 | + with client.connect('127.0.0.1', 10800): |
| 82 | + cache = client.get_or_create_cache({ |
| 83 | + PROP_NAME: 'tx_cache', |
| 84 | + PROP_CACHE_ATOMICITY_MODE: CacheAtomicityMode.TRANSACTIONAL |
| 85 | + }) |
| 86 | + |
| 87 | + # starting transaction |
| 88 | + with client.tx_start( |
| 89 | + isolation=TransactionIsolation.REPEATABLE_READ, concurrency=TransactionConcurrency.PESSIMISTIC |
| 90 | + ) as tx: |
| 91 | + cache.put(1, 'success') |
| 92 | + tx.commit() |
| 93 | + |
| 94 | + # key=1 value=success |
| 95 | + print(f"key=1 value={cache.get(1)}") |
| 96 | + |
| 97 | + # rollback transaction. |
| 98 | + try: |
| 99 | + with client.tx_start( |
| 100 | + isolation=TransactionIsolation.REPEATABLE_READ, concurrency=TransactionConcurrency.PESSIMISTIC |
| 101 | + ): |
| 102 | + cache.put(1, 'fail') |
| 103 | + raise RuntimeError('test') |
| 104 | + except RuntimeError: |
| 105 | + pass |
| 106 | + |
| 107 | + # key=1 value=success |
| 108 | + print(f"key=1 value={cache.get(1)}") |
| 109 | + |
| 110 | + # rollback transaction on timeout. |
| 111 | + try: |
| 112 | + with client.tx_start(timeout=1.0, label='long-tx') as tx: |
| 113 | + cache.put(1, 'fail') |
| 114 | + time.sleep(2.0) |
| 115 | + tx.commit() |
| 116 | + except CacheError as e: |
| 117 | + # Cache transaction timed out: GridNearTxLocal[...timeout=1000, ... label=long-tx] |
| 118 | + print(e) |
| 119 | + |
| 120 | + # key=1 value=success |
| 121 | + print(f"key=1 value={cache.get(1)}") |
| 122 | + |
| 123 | + # destroy cache |
| 124 | + cache.destroy() |
| 125 | + |
| 126 | + |
| 127 | +if __name__ == '__main__': |
| 128 | + print("Starting sync example") |
| 129 | + sync_example() |
| 130 | + |
| 131 | + if sys.version_info >= (3, 7): |
| 132 | + print("Starting async example") |
| 133 | + loop = asyncio.get_event_loop() |
| 134 | + loop.run_until_complete(async_example()) |
0 commit comments