sessionmaker,scoped_session,declarative_base的使用

關於ORM,以及Python中SQLAlchemy的sessionmaker,scoped_session

orm(object relational mapping):對象關係映射

python面向對象,而數據庫是關係型。

orm是將數據庫關係映射爲Python中的對象,不用直接寫SQL。

缺點是性能略差

 

通過sessionmaker,我們得到一個類,一個能產生session的工廠。

我們可以用這個類的對象來操作數據庫。example:

複製代碼

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
# an Engine, which the Session will use for connection
# resourcessome_engine = create_engine('postgresql://scott:tiger@localhost/')
# create a configured "Session" classSession = sessionmaker(bind=some_engine)
# create a Sessionsession = Session()
# work with sessmyobject = MyObject('foo', 'bar')
session.add(myobject)
session.commit()

複製代碼

然而,此時如果我們再創建一個Session對象的時候,新的對象和原來的對象是不同的:

......
>>> session1 = Session()
>>> session2 = Session()
>>> session1 is session2
False

 

而使用scoped_session的目的主要是爲了線程安全

scoped_session類似單例模式,當我們調用使用的時候,會先在Registry裏找找之前是否已經創建session了。

要是有,就把這個session返回。

要是沒有,就創建新的session,註冊到Registry中以便下次返回給調用者。

這樣就實現了這樣一個目的:在同一個線程中,call scoped_session 的時候,返回的是同一個對象

複製代碼

>>> from sqlalchemy.orm import scoped_session
>>> from sqlalchemy.orm import sessionmaker
>>> session_factory = sessionmaker(bind=some_engine)
>>> Session = scoped_session(session_factory)
>>> some_session = Session()
>>> some_other_session = Session()
>>> some_session is some_other_session
True

複製代碼

scoped_session實現了代理模式。能夠將操作轉發到代理的對象中去執行:

複製代碼

Session = scoped_session(some_factory)
# equivalent to:
#
# session = Session()
# print(session.query(MyClass).all())
#print(Session.query(MyClass).all())

複製代碼

 scoped_session的實現使用了thread local storage技術,使session實現了線程隔離。這樣我們就只能看見本線程的session。

https://www.jb51.net/article/49789.htm

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章