2
2
import random
3
3
from multiprocessing import Process , Value , Queue
4
4
import time
5
+ import sys
5
6
from event_history import *
6
7
7
8
class ClientCollection (object ):
@@ -59,26 +60,36 @@ def history(self):
59
60
return self ._history
60
61
61
62
def check_total (self ):
62
- conn = psycopg2 .connect (self . connstr )
63
- cur = conn . cursor ();
63
+ conn , cur = self .connect ()
64
+
64
65
while self .run .value :
65
66
event_id = self .history .register_start ('total' )
66
- cur .execute ('select sum(amount) from bank_test' )
67
- self .history .register_finish (event_id , 'commit' )
68
67
69
- res = cur .fetchone ()
70
- if res [0 ] != 0 :
71
- print ("Isolation error, total = %d" % (res [0 ],))
72
- raise BaseException
68
+ try :
69
+ cur .execute ('select sum(amount) from bank_test' )
70
+ res = cur .fetchone ()
71
+ if res [0 ] != 0 :
72
+ print ("Isolation error, total = %d" % (res [0 ],))
73
+ raise BaseException
74
+ except psycopg2 .InterfaceError :
75
+ print ("Got error: " , sys .exc_info ())
76
+ print ("Reconnecting" )
77
+ conn , cur = self .connect (reconnect = True )
78
+ except :
79
+ print ("Got error: " , sys .exc_info ())
80
+ self .history .register_finish (event_id , 'rollback' )
81
+ else :
82
+ self .history .register_finish (event_id , 'commit' )
83
+
73
84
74
85
cur .close ()
75
86
conn .close ()
76
87
77
88
def transfer_money (self ):
78
- print (self .connstr )
79
- conn = psycopg2 . connect ( self . connstr )
80
- cur = conn . cursor ()
81
-
89
+ #conn = psycopg2.connect (self.connstr)
90
+ #cur = conn.cursor( )
91
+ conn , cur = self . connect ()
92
+
82
93
i = 0
83
94
while self .run .value :
84
95
i += 1
@@ -88,39 +99,59 @@ def transfer_money(self):
88
99
89
100
event_id = self .history .register_start ('transfer' )
90
101
91
- cur .execute ('''update bank_test
102
+ try :
103
+ cur .execute ('''update bank_test
92
104
set amount = amount - %s
93
105
where uid = %s''' ,
94
106
(amount , from_uid ))
95
- cur .execute ('''update bank_test
107
+ cur .execute ('''update bank_test
96
108
set amount = amount + %s
97
109
where uid = %s''' ,
98
110
(amount , to_uid ))
99
111
100
- try :
101
112
conn .commit ()
113
+ except psycopg2 .DatabaseError :
114
+ print ("Got error: " , sys .exc_info ())
115
+ print ("Reconnecting" )
116
+
117
+ self .history .register_finish (event_id , 'rollback' )
118
+ conn , cur = self .connect (reconnect = True )
102
119
except :
120
+ print ("Got error: " , sys .exc_info ())
103
121
self .history .register_finish (event_id , 'rollback' )
104
122
else :
105
123
self .history .register_finish (event_id , 'commit' )
106
-
124
+
107
125
cur .close ()
108
126
conn .close ()
109
127
110
- def watchdog (self ):
128
+ def connect (self , reconnect = False ):
129
+
111
130
while self .run .value :
112
- time .sleep (1 )
113
- print ('watchdog: ' , self .history .aggregate ())
131
+ try :
132
+ conn = psycopg2 .connect (self .connstr )
133
+ cur = conn .cursor ()
134
+ except :
135
+ print ("Got error: " , sys .exc_info ())
136
+ if not reconnect :
137
+ raise
138
+ else :
139
+ return conn , cur
140
+
141
+ # def watchdog(self):
142
+ # while self.run.value:
143
+ # time.sleep(1)
144
+ # print('watchdog: ', self.history.aggregate())
114
145
115
146
def start (self ):
116
147
self .transfer_process = Process (target = self .transfer_money , args = ())
117
148
self .transfer_process .start ()
118
-
149
+
119
150
self .total_process = Process (target = self .check_total , args = ())
120
151
self .total_process .start ()
121
152
122
- self .total_process = Process (target = self .watchdog , args = ())
123
- self .total_process .start ()
153
+ # self.total_process = Process(target=self.watchdog, args=())
154
+ # self.total_process.start()
124
155
125
156
return
126
157
@@ -136,5 +167,3 @@ def cleanup(self):
136
167
cur .close ()
137
168
conn .close ()
138
169
139
-
140
-
0 commit comments