在python中使用del进行SqlAlchemy连接安全吗

2024-09-30 10:27:18 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我使用self.engine.dispose()时的代码,它给了我异常,所以我应该使用这个del self.engine它工作正常,请给我任何建议。你知道吗

class MSSqlAlchemyConn(object):
def __init__(self, connection_obj):
    """
    Constructor for SQLAlchemy.
    """
    try:
        # connection string
        # example: mysql+mysqldb://[mysqluser]:[password]@[host]:[port]/[db_name]
        constr = "mssql+pymssql://%s:%s@%s/%s" % (connection_obj['USER'], connection_obj['PASSWORD'],
                                                  connection_obj['HOST'],
                                                  connection_obj['DATABASE'])
        self.engine = create_engine(constr, echo=False)
    except Exception as e:
        logger.error(e)

def __del__(self):
    if self.engine is not None:
        del self.engine

Tags: 代码selfobjobjectinitdefconnection建议

热门问题