【发布时间】:2015-04-17 01:44:31
【问题描述】:
我正在使用 sqlAlchemy 核心来访问 postgres 数据库。从中进行选择后,我在删除表时遇到问题。查看 CPU 进程 (ps -aux |grep postgres) 我可以看到 drop 命令正在等待。似乎它正在等待选择语句清除,这应该已经发生了。我进行了选择并处理了所有行,以便结束选择过程。我什至试图关闭光标,但没有做任何事情。我唯一能做的就是关闭连接,但我认为我应该为每个数据库查询重新创建连接,还是应该?
下面是关于水滴如何悬挂的基本概念。
engine = create_engine('postgresql://***:***@localhost:5432/Junk')
metadata = MetaData()
temp_table = Table('index_tmp', metadata,
Column('pid', Integer, primary_key = True),
Column('match_id', Integer),
Column('id', Integer)
)
people = Table('people', metadata, schema='public', autoload=True, autoload_with=engine)
metadata.create_all(engine)
conn = engine.connect()
sel = select([func.min(people.c.id).label('match_id'), people.c.id])
ins = temp_table.insert().from_select(['match_id', 'id'], sel)
conn.execute(ins)
result = conn.execute(select([tmp_table]))
#Here the process shows "idle in transaction"
result.fetchall()
#This closes the cursor but the process is still "idle in transaction"
temp_table.drop(engine, checkfirst=True)
#The script will hang here since the select command is still "idle in transaction" and blocking this drop.
【问题讨论】:
标签: transactions sqlalchemy core