在python中使用del for SqlAlchemy连接是否安全

时间:2017-12-06 11:31:58

标签: python sql-server sqlalchemy

当我使用self.engine.dispose()时这是我的代码它给了我Exception,所以如果我使用它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

0 个答案:

没有答案