from sqlalchemy import URL from sqlmodel import create_engine from app.core.settings import ( TIDB_HOST, TIDB_USER, TIDB_PASSWORD, TIDB_PORT, TIDB_DATABASE, ) def get_db_url(): return URL( drivername="mysql+pymysql", username=TIDB_USER, password=TIDB_PASSWORD, host=TIDB_HOST, port=TIDB_PORT, database=TIDB_DATABASE, query={"ssl_verify_cert": True, "ssl_verify_identity": True}, ) # TiDB Serverless clusters have a limitation: if there are no active connections for 5 minutes, # they will shut down, which closes all connections, so we need to recycle the connections engine = create_engine(get_db_url(), pool_recycle=300)