2017-10-13 57 views
0

我定義的模塊級這樣SQLAlchemy的會議在多線程應用程序

from sqlalchemy import create_engine 
from sqlalchemy.orm import sessionmaker 

engine = create_engine('cx_oracle://username:[email protected]') 
Session = sessionmaker(bind=engine) 

... data model located on engine 

我的數據模型,然後我開始有些線程這樣

threads = [] 

# start threads 
for i in range(4): 

    # each thread needs it own session 
    thread = threading.Thread(target=fn, args=(in,)) 
    thread.start() 
    threads.append(thread) 

# wait for threads to finish 
for thread in threads: 
    thread.join() 

其中fn(in)處理一些方法,我使用session = Session(),然後用於各種目的。

有了這個,我得到各種錯誤,這種錯誤有時只發生。它可以是

  • DatabaseError: (cx_Oracle.DatabaseError) DPI-1002: invalid OCI handle
  • DBAPIError: (sqlalchemy.exc.ResourceClosedError) This Connection is closed
  • InvalidRequestError: Session is already flushing

我真的不知道如何解決,雖然

+0

請提供[mcve],特別是'fn'的內容。 – univerio

回答

0

我們一直與我們的應用程序的一些類似的問題,這個問題和cx_Oracle 6.x,它已更新爲使用Oracle OCI的新C接口(ODPI-C),該接口對其發行版中的錯誤有一些引用筆記。我們降級了cx_Oracle,問題消失了。