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