This commit is contained in:
yumoqing 2025-10-28 21:04:05 +08:00
parent 84d740ad3d
commit dd5f88368b
2 changed files with 19 additions and 4 deletions

View File

@ -56,11 +56,24 @@ class SqlorPool:
self.sqlors.append(x) self.sqlors.append(x)
return x return x
async def _del_sqlor(self, sor):
try:
await sor.exit()
except:
pass
try:
await sor.close()
except:
pass
async def test_sqlor(self, sor): async def test_sqlor(self, sor):
try: try:
await sor.enter()
await sor.execute(sor.test_sqlstr, {}) await sor.execute(sor.test_sqlstr, {})
await sor.exit()
return True return True
except: except:
await sor.exit()
return False return False
@asynccontextmanager @asynccontextmanager
@ -70,10 +83,11 @@ class SqlorPool:
yielded_sqlor = None yielded_sqlor = None
for s in sqlors: for s in sqlors:
if not s.used: if not s.used:
flg = await self.test_sqlor(s) flg = await self.test_sqlor(s.sqlor)
if flg: if flg:
yielded_sqlor = s yielded_sqlor = s
else: else:
await self._del_sqlor(s.sqlor)
self.sqlors = [ x for x in self.sqlors if x != s ] self.sqlors = [ x for x in self.sqlors if x != s ]
if not yielded_sqlor: if not yielded_sqlor:
yielded_sqlor = await self._new_sqlor() yielded_sqlor = await self._new_sqlor()
@ -82,6 +96,7 @@ class SqlorPool:
yield yielded_sqlor.sqlor yield yielded_sqlor.sqlor
yielded_sqlor.used = False yielded_sqlor.used = False
@SingletonDecorator @SingletonDecorator
class DBPools: class DBPools:
def __init__(self,databases={},max_connect=100,loop=None): def __init__(self,databases={},max_connect=100,loop=None):
@ -119,9 +134,9 @@ class DBPools:
sqlor = None sqlor = None
try: try:
async with pool.context() as sqlor: async with pool.context() as sqlor:
sqlor.enter() await sqlor.enter()
yield sqlor yield sqlor
sqlor.exit() await sqlor.exit()
if sqlor and sqlor.dataChanged: if sqlor and sqlor.dataChanged:
await sqlor.commit() await sqlor.commit()
except Exception as e: except Exception as e:

View File

@ -164,7 +164,7 @@ WHERE
async def exit(self): async def exit(self):
try: try:
await self.cur.fetchall() await self.cur.fetchall()
self.cur.close() await self.cur.close()
except: except:
pass pass
self.cur = None self.cur = None