fix: close database read sessions
This commit is contained in:
+14
-3
@@ -1,8 +1,7 @@
|
|||||||
import json
|
import json
|
||||||
|
|
||||||
from sqlalchemy import create_engine, Column, String, ForeignKey, Integer
|
from sqlalchemy import create_engine, Column, String, ForeignKey, Integer
|
||||||
from sqlalchemy.ext.declarative import declarative_base
|
from sqlalchemy.orm import declarative_base, sessionmaker
|
||||||
from sqlalchemy.orm import sessionmaker
|
|
||||||
from sqlalchemy.exc import IntegrityError
|
from sqlalchemy.exc import IntegrityError
|
||||||
|
|
||||||
engine = create_engine('sqlite:///data/qotnews.sqlite', connect_args={'timeout': 360})
|
engine = create_engine('sqlite:///data/qotnews.sqlite', connect_args={'timeout': 360})
|
||||||
@@ -32,7 +31,10 @@ def init():
|
|||||||
|
|
||||||
def get_story(sid):
|
def get_story(sid):
|
||||||
session = Session()
|
session = Session()
|
||||||
return session.query(Story).get(sid)
|
try:
|
||||||
|
return session.get(Story, sid)
|
||||||
|
finally:
|
||||||
|
session.close()
|
||||||
|
|
||||||
def put_story(story):
|
def put_story(story):
|
||||||
story = story.copy()
|
story = story.copy()
|
||||||
@@ -61,15 +63,22 @@ def put_story(story):
|
|||||||
|
|
||||||
def get_story_by_ref(ref):
|
def get_story_by_ref(ref):
|
||||||
session = Session()
|
session = Session()
|
||||||
|
try:
|
||||||
return session.query(Story).filter(Story.ref==ref).first()
|
return session.query(Story).filter(Story.ref==ref).first()
|
||||||
|
finally:
|
||||||
|
session.close()
|
||||||
|
|
||||||
def get_reflist(amount):
|
def get_reflist(amount):
|
||||||
session = Session()
|
session = Session()
|
||||||
|
try:
|
||||||
q = session.query(Reflist).order_by(Reflist.rid.desc()).limit(amount)
|
q = session.query(Reflist).order_by(Reflist.rid.desc()).limit(amount)
|
||||||
return [dict(ref=x.ref, sid=x.sid, source=x.source) for x in q.all()]
|
return [dict(ref=x.ref, sid=x.sid, source=x.source) for x in q.all()]
|
||||||
|
finally:
|
||||||
|
session.close()
|
||||||
|
|
||||||
def get_stories(amount, skip=0):
|
def get_stories(amount, skip=0):
|
||||||
session = Session()
|
session = Session()
|
||||||
|
try:
|
||||||
q = session.query(Reflist, Story.meta_json).\
|
q = session.query(Reflist, Story.meta_json).\
|
||||||
order_by(Reflist.rid.desc()).\
|
order_by(Reflist.rid.desc()).\
|
||||||
join(Story).\
|
join(Story).\
|
||||||
@@ -77,6 +86,8 @@ def get_stories(amount, skip=0):
|
|||||||
offset(skip).\
|
offset(skip).\
|
||||||
limit(amount)
|
limit(amount)
|
||||||
return [x[1] for x in q]
|
return [x[1] for x in q]
|
||||||
|
finally:
|
||||||
|
session.close()
|
||||||
|
|
||||||
def put_ref(ref, sid, source):
|
def put_ref(ref, sid, source):
|
||||||
try:
|
try:
|
||||||
|
|||||||
Reference in New Issue
Block a user