2012-03-28 132 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()