-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcursores.py
executable file
·62 lines (46 loc) · 2.09 KB
/
cursores.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/python
# See the following example too: https://gist.github.com/moylop260/83978677cb072d048569c9dc9a1b7fbe
"""
Case of use to known the ISOLATION_LEVEL_REPEATABLE_READ database psycopg way
The output of this script was:
cursor 1 - select to add cache with ISOLATION_LEVEL_REPEATABLE_READ
[(1, 'Sales Journal - (test)')]
cursor 2 - select to add cache with ISOLATION_LEVEL_REPEATABLE_READ
[(1, 'Sales Journal - (test)')]
cursor 1 - update row and commit
cursor 1 - read again (updated cache) [(1, 'Moy')]
cursor 2 - read again (outdated cache) [(1, 'Sales Journal - (test)')]
FYI odoo use ISOLATION_LEVEL_REPEATABLE_READ more info here:
https://github.com/odoo/odoo/blob/b08642c21cb345aa0778bdc9754425eb9ac9faa7/openerp/sql_db.py#L69-L128
I fixed this issue using the following changes:
https://github.com/Vauxoo/odoo-mexico-v2/pull/340/files
"""
from __future__ import print_function
import psycopg2
from psycopg2.extensions import ISOLATION_LEVEL_REPEATABLE_READ
# ISOLATION_LEVEL_REPEATABLE_READ:
# use cache to avoid read the database by each time
def create_connection(dbname, isolation_level=ISOLATION_LEVEL_REPEATABLE_READ):
conn = psycopg2.connect("dbname='%s'" % dbname)
conn.set_isolation_level(isolation_level)
cur = conn.cursor()
return conn, cur
dbname = 'openerp_test29'
conn1, cur1 = create_connection(dbname)
conn2, cur2 = create_connection(dbname)
SELECT_QUERY = "SELECT id, name FROM account_journal WHERE id = %s"
UPDATE_QUERY = "UPDATE account_journal SET name = 'Moy' WHERE id = %s"
row_id = 1
cur1.execute(SELECT_QUERY, (row_id,))
print("cursor 1 - select to add cache with ISOLATION_LEVEL_REPEATABLE_READ")
print(cur1.fetchall())
cur2.execute(SELECT_QUERY, (row_id,))
print("cursor 2 - select to add cache with ISOLATION_LEVEL_REPEATABLE_READ")
print(cur2.fetchall())
cur1.execute(UPDATE_QUERY, (row_id,))
conn1.commit()
print("cursor 1 - update row and commit")
cur1.execute(SELECT_QUERY, (row_id,))
print("cursor 1 - read again (updated cache)", cur1.fetchall())
cur2.execute(SELECT_QUERY, (row_id,))
print("cursor 2 - read again (outdated cache)", cur2.fetchall())