@@ -65,106 +65,68 @@ def print_error(self, arg, comment=''):
6565 if self .show_errors :
6666 print ('Node' , self .node_id , 'got error' , arg , comment )
6767
68- def check_total (self ):
69- conn , cur = self .connect ()
70- i = 0
68+ def exec_tx (self , name , tx_block ):
69+ conn = psycopg2 .connect (self . connstr )
70+ cur = conn . cursor ()
7171
7272 while self .run .value :
73- i += 1
74-
75- event_id = self .history .register_start ('total' )
73+ event_id = self .history .register_start (name )
7674
77- if not conn .closed :
75+ if conn .closed :
76+ self .history .register_finish (event_id , 'ReConnect' )
7877 try :
7978 conn = psycopg2 .connect (self .connstr )
8079 cur = conn .cursor ()
8180 except :
82- self .history .register_finish (event_id , 'CantConnect' )
83- next
84-
85- amount = 1
86- from_uid = random .randrange (1 , self .accounts + 1 )
87- to_uid = random .randrange (1 , self .accounts + 1 )
81+ continue
82+ else :
83+ continue
8884
8985 try :
90- cur .execute ('select sum(amount) from bank_test' )
91- res = cur .fetchone ()
92- if res [0 ] != 0 :
93- print ("Isolation error, total = %d" % (res [0 ],))
94- raise BaseException
95- except BaseException :
96- raise BaseException
86+ tx_block (conn , cur )
9787 except psycopg2 .InterfaceError :
9888 self .history .register_finish (event_id , 'InterfaceError' )
89+ except psycopg2 .Error :
90+ self .history .register_finish (event_id , 'PsycopgError' )
9991 except :
92+ print (sys .exc_info ())
10093 self .history .register_finish (event_id , 'OtherError' )
10194 else :
102- self .history .register_finish (event_id , 'commit ' )
95+ self .history .register_finish (event_id , 'Commit ' )
10396
10497 cur .close ()
10598 conn .close ()
10699
107- def transfer_money (self ):
108- conn , cur = self .connect ()
109-
110- i = 0
100+ def check_total (self ):
111101
112- while self .run .value :
113- i += 1
102+ def tx (conn , cur ):
103+ cur .execute ('select sum(amount) from bank_test' )
104+ res = cur .fetchone ()
105+ if res [0 ] != 0 :
106+ print ("Isolation error, total = %d" % (res [0 ],))
107+ raise BaseException
114108
115- event_id = self .history . register_start ( 'transfer' )
109+ self .exec_tx ( 'total' , tx )
116110
117- if not conn .closed :
118- try :
119- conn = psycopg2 .connect (self .connstr )
120- cur = conn .cursor ()
121- except :
122- self .history .register_finish (event_id , 'CantConnect' )
123- next
111+ def transfer_money (self ):
124112
113+ def tx (conn , cur ):
125114 amount = 1
126- from_uid = random .randrange (1 , self .accounts + 1 )
127- to_uid = random .randrange (1 , self .accounts + 1 )
128-
129- try :
130- cur .execute ('''update bank_test
131- set amount = amount - %s
132- where uid = %s''' ,
133- (amount , from_uid ))
134- cur .execute ('''update bank_test
135- set amount = amount + %s
136- where uid = %s''' ,
137- (amount , to_uid ))
138- conn .commit ()
139-
140- except psycopg2 .InterfaceError :
141- self .history .register_finish (event_id , 'InterfaceError' )
142- except :
143- self .history .register_finish (event_id , 'OtherError' )
144- else :
145- self .history .register_finish (event_id , 'commit' )
146-
147- cur .close ()
148- conn .close ()
149-
150- def connect (self , reconnect = False ):
151-
152- while True :
153- try :
154- conn = psycopg2 .connect (self .connstr )
155- cur = conn .cursor ()
156- return conn , cur
157- except :
158- self .print_error (sys .exc_info (),'2' )
159- if not reconnect :
160- raise
161- if not self .run .value :
162- raise
163-
164- # def watchdog(self):
165- # while self.run.value:
166- # time.sleep(1)
167- # print('watchdog: ', self.history.aggregate())
115+ from_uid = random .randrange (1 , self .accounts - 10 )
116+ to_uid = from_uid + 1 #random.randrange(1, self.accounts + 1)
117+
118+ conn .commit ()
119+ cur .execute ('''update bank_test
120+ set amount = amount - %s
121+ where uid = %s''' ,
122+ (amount , from_uid ))
123+ cur .execute ('''update bank_test
124+ set amount = amount + %s
125+ where uid = %s''' ,
126+ (amount , to_uid ))
127+ conn .commit ()
128+
129+ self .exec_tx ('transfer' , tx )
168130
169131 def start (self ):
170132 self .transfer_process = Process (target = self .transfer_money , args = ())
@@ -173,9 +135,6 @@ def start(self):
173135 self .total_process = Process (target = self .check_total , args = ())
174136 self .total_process .start ()
175137
176- #self.total_process = Process(target=self.watchdog, args=())
177- #self.total_process.start()
178-
179138 return
180139
181140 def stop (self ):
0 commit comments