sqlalchemy关闭所有连接

时间:2013-08-01 11:57:11

标签: python sqlalchemy

我试图使用sqlalchemy删除我的数据库: 我用下一个:

def dropDb(self, dbName):
    self.closeConnection()
    self.selectDb(None)
    self.execute("drop database %s" % dbName)

def closeConnection(self):
    self._Session.close_all()
    self._engine.dispose()
    del self._Session
    del self._engine

我也是通过下一个方式创建engene:

sqlalchemy.create_engine(connection_string, poolclass = NullPool)

但我总是收到错误:

  

详细编程错误:(编程错误)(' 42000',' [42000]   [Microsoft] [ODBC SQL Server驱动程序] [SQL Server]无法删除数据库   " TEST_DB"因为它目前正在使用中。 (3702)(SQLExecDirectW)')   ' drop database test_db' ()

如何强行关闭所有连接?

1 个答案:

答案 0 :(得分:3)

The current method 用于关闭所有使用 SQLAlchemy 会话 api 的连接是

from sqlalchemy.orm import close_all_sessions

close_all_sessions()

因为 session.close_all() 已被弃用。

相关问题