2012-03-28 43 views
2

我正在使用遠程數據庫和應用程序安裝在多臺機器上的python-pyside桌面應用程序,應用程序使用sql alchemy。Sqlalchemy遠程數據庫會話刷新/刷新問題

問題是: 應用程序有一個表打印機,它包括8個記錄。 如果m1用戶刪除/更新/插入打印機記錄,則在2臺機器m1和m2上運行應用程序, ,如果m2獲取打印機,則顯示8臺打印機(與啓動時相同) 如果m2執行任何更新/在應用程序中插入/刪除操作並嘗試從打印機獲取記錄,然後顯示正確的數據。

我認爲這個問題/行爲是由於sql鍊金術會議。

代碼:

#config file 
self.engine = create_engine ('mysql://user:[email protected]/database') 

#Database session file 
configuration=Config()   #config consist db,dbusername, 
           #host name, pwd and engin 
engine =configuration.engine 
db_session = scoped_session(sessionmaker(autocommit=False, 
         autoflush=False, 
         bind=engine)) 
Base = declarative_base() 
Base.query = db_session.query_property() 

#initialization 
import models 
Base.metadata.create_all(bind=engine) 

#Printer model file 
#get query 
printers = db_session.query(Printer).filter(Printer.status != 0) 
.order_by(asc(Printer.name)).all() 

#alternative get query used/it also have same problem 
printers = Printer.query.filter(Printer.status != 0) 
.order_by(asc(Printer.name)).all() 

會議只有一次在主/啓動的應用程序的文件進行初始化。

請幫幫我。

我試過db_session.flush()也autoflush = True,但它不起作用。

+0

請嘗試session.commit。 – Nilesh 2012-03-28 13:31:38

+0

@Lafada session.commit在查詢後使用,問題是爲不同的用戶獲取(選擇/過濾)數據。 – anils 2012-03-28 13:53:08

+0

謝謝拉法達! session.commit get(select/filter)查詢解決了我的問題。 – anils 2012-03-28 14:02:28

回答

1

在獲取數據之前使用session.commit而不是session.flush。

1

謝謝Lafada,才把

session.commit(選擇/過濾器)查詢解決我的問題。

#Printer model file 
db_session.commit() 
#get query 
printers = db_session.query(Printer).filter(Printer.status != 0) 
.order_by(asc(Printer.name)).all() 

#alternative get query used/it also have same problem 
db_session.commit() 
printers = Printer.query.filter(Printer.status != 0) 
.order_by(asc(Printer.name)).all()