为了账号安全,请及时绑定邮箱和手机立即绑定

如果MySQL数据库连接池在很长一段时间内未使用,或者使用它的应用程序崩溃了,那么它是否会过期?

如果MySQL数据库连接池在很长一段时间内未使用,或者使用它的应用程序崩溃了,那么它是否会过期?

撒科打诨 2022-08-02 18:18:10
我正在使用从Python脚本连接数据库MySQL(MariaDB)。我使用上下文管理器来连接池中的连接。我想知道如果长时间不使用或我的程序崩溃,池是否会过期。我发现与MySQL数据库的连接过期,即使您忘记或无法在程序中关闭连接,它也会被释放吗,连接池的情况如何?MySQLConnectionPoolfrom contextlib import contextmanagerimport mysql.connectorfrom mysql.connector.errors import Errorfrom mysql.connector import poolingSQL_CONN_POOL = pooling.MySQLConnectionPool(    pool_name="mysqlpool",    pool_size=1,    user=DB_USER,    password=DB_PASS,    host=DB_HOST,    database=DATABASE,    auth_plugin=DB_PLUGIN)@contextmanagerdef mysql_connection_from_pool() -> "conn":    conn_pool = SQL_CONN_POOL  # get connection from the pool, all the rest is the same    _conn = conn_pool.get_connection()    try:        yield _conn    except (Exception, Error) as ex:        # if error happened all made changes during the connection will be rolled back:        _conn.rollback()        # this statement re-raise error to let it be handled in outer scope:        raise    else:        # if everything is fine commit all changes to save them in db:        _conn.commit()    finally:        # actually it returns connection to the pool, rather than close it        _conn.close()@contextmanagerdef mysql_curs_from_pool() -> "curs":    with mysql_connection_from_pool() as _conn:        _curs = _conn.cursor()        try:            yield _curs        finally:            _curs.close()
查看完整描述

1 回答

?
慕村9548890

TA贡献1884条经验 获得超4个赞

是的,它可以超时。有两种超时配置。

查看wait_timeoutinteractive_timeout


查看完整回答
反对 回复 2022-08-02
  • 1 回答
  • 0 关注
  • 218 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号