feat: DB再起動、時間経過後のコネクション再確立の仕込み
This commit is contained in:
parent
f194f206e4
commit
e87da068bb
@ -1,10 +1,28 @@
|
|||||||
from sqlalchemy import (Connection, CursorResult, Engine, QueuePool,
|
from sqlalchemy import (Connection, CursorResult, Engine, QueuePool,
|
||||||
create_engine, text)
|
create_engine, event, exc, text)
|
||||||
from sqlalchemy.engine.url import URL
|
from sqlalchemy.engine.url import URL
|
||||||
|
from sqlalchemy.pool import Pool
|
||||||
|
|
||||||
from src.error.exceptions import DBException
|
from src.error.exceptions import DBException
|
||||||
|
from src.logging.get_logger import get_logger
|
||||||
from src.system_var import environment
|
from src.system_var import environment
|
||||||
|
|
||||||
|
logger = get_logger('DB接続')
|
||||||
|
|
||||||
|
|
||||||
|
@event.listens_for(Pool, 'checkout')
|
||||||
|
def ping_connection(dbapi_connection, connection_record, connection_proxy):
|
||||||
|
"""コネクションが切れた場合、再接続"""
|
||||||
|
cursor = dbapi_connection.cursor()
|
||||||
|
try:
|
||||||
|
cursor.execute("SELECT 1")
|
||||||
|
except Exception as e:
|
||||||
|
logger.info(f'DB接続に失敗したため、リトライします: {e}')
|
||||||
|
# raise DisconnectionError - pool will try
|
||||||
|
# connecting again up to three times before raising.
|
||||||
|
raise exc.DisconnectionError()
|
||||||
|
cursor.close()
|
||||||
|
|
||||||
|
|
||||||
class Database:
|
class Database:
|
||||||
"""データベース操作クラス"""
|
"""データベース操作クラス"""
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user