Ticket #4984: dont-add-failing-connections-to-the-pool.diff
| File dont-add-failing-connections-to-the-pool.diff, 2.8 KB (added by tlk@…, 3 years ago) |
|---|
-
trac/db/pool.py
41 41 self._pool = pool 42 42 self._key = key 43 43 self._tid = tid 44 self._reusable = True 44 45 45 46 def close(self): 46 47 if self.cnx: 47 self._pool._return_cnx(self.cnx, self._key, self._tid )48 self._pool._return_cnx(self.cnx, self._key, self._tid, self._reusable) 48 49 self.cnx = None 49 50 self.log = None 50 51 51 52 def __del__(self): 52 53 self.close() 53 54 55 def cursor(self): 56 try: 57 return ConnectionWrapper.cursor(self) 58 except Exception: 59 self._reusable = False 60 self.close() 61 raise 54 62 55 63 def try_rollback(cnx): 56 64 """Resets the Connection in a safe way, returning True when it succeeds. … … 89 97 while True: 90 98 # First choice: Return the same cnx already used by the thread 91 99 if (tid, key) in self._active: 92 cnx, num = self._active[(tid, key)]100 cnx, num, reuse = self._active[(tid, key)] 93 101 num += 1 94 102 # Second best option: Reuse a live pooled connection 95 103 elif key in self._pool_key: … … 115 123 self._pool_time.pop(0) 116 124 cnx = connector.get_connection(**kwargs) 117 125 if cnx: 118 self._active[(tid, key)] = (cnx, num )126 self._active[(tid, key)] = (cnx, num, True) 119 127 return PooledConnection(self, cnx, key, tid, log) 120 128 # Worst option: wait until a connection pool slot is available 121 129 if timeout and (time.time() - start) > timeout: … … 129 137 finally: 130 138 self._available.release() 131 139 132 def _return_cnx(self, cnx, key, tid ):140 def _return_cnx(self, cnx, key, tid, reusable): 133 141 self._available.acquire() 134 142 try: 135 143 assert (tid, key) in self._active 136 cnx, num = self._active[(tid, key)] 144 cnx, num, reuse = self._active[(tid, key)] 145 reuse = reuse and reusable 137 146 if num == 1: 138 147 del self._active[(tid, key)] 139 148 self._available.notify() 140 if cnx.poolable and try_rollback(cnx) :149 if cnx.poolable and try_rollback(cnx) and reuse: 141 150 self._pool.append(cnx) 142 151 self._pool_key.append(key) 143 152 self._pool_time.append(time.time()) 144 153 else: 145 self._active[(tid, key)] = (cnx, num - 1 )154 self._active[(tid, key)] = (cnx, num - 1, reuse) 146 155 finally: 147 156 self._available.release() 148 157
