Use always active connection in DbCursor
This commit is contained in:
parent
a54f5f3e9f
commit
dca1dcdd2d
2 changed files with 15 additions and 21 deletions
|
@ -222,7 +222,7 @@ class Db(object):
|
||||||
if not self.conn:
|
if not self.conn:
|
||||||
self.connect()
|
self.connect()
|
||||||
|
|
||||||
cur = DbCursor(self.conn, self)
|
cur = DbCursor(self)
|
||||||
return cur
|
return cur
|
||||||
|
|
||||||
def getSharedCursor(self):
|
def getSharedCursor(self):
|
||||||
|
|
|
@ -2,14 +2,12 @@ import time
|
||||||
import re
|
import re
|
||||||
from util import helper
|
from util import helper
|
||||||
|
|
||||||
|
|
||||||
# Special sqlite cursor
|
# Special sqlite cursor
|
||||||
|
|
||||||
|
|
||||||
class DbCursor:
|
class DbCursor:
|
||||||
|
|
||||||
def __init__(self, conn, db):
|
def __init__(self, db):
|
||||||
self.conn = conn
|
|
||||||
self.db = db
|
self.db = db
|
||||||
self.logging = False
|
self.logging = False
|
||||||
|
|
||||||
|
@ -96,24 +94,19 @@ class DbCursor:
|
||||||
query, params = self.parseQuery(query, params)
|
query, params = self.parseQuery(query, params)
|
||||||
|
|
||||||
s = time.time()
|
s = time.time()
|
||||||
cursor = self.conn.cursor()
|
cursor = self.db.getConn().cursor()
|
||||||
|
self.db.cursors.add(cursor)
|
||||||
|
|
||||||
try:
|
if params:
|
||||||
if self.db.lock.locked():
|
res = cursor.execute(query, params)
|
||||||
self.db.log.debug("Query delayed: db locked")
|
else:
|
||||||
self.db.lock.acquire(True)
|
res = cursor.execute(query)
|
||||||
if params:
|
taken_query = time.time() - s
|
||||||
res = cursor.execute(query, params)
|
if self.logging or taken_query > 0.1:
|
||||||
|
if params: # Query has parameters
|
||||||
|
self.db.log.debug("Query: " + query + " " + str(params) + " (Done in %.4f)" % (time.time() - s))
|
||||||
else:
|
else:
|
||||||
res = cursor.execute(query)
|
self.db.log.debug("Query: " + query + " (Done in %.4f)" % (time.time() - s))
|
||||||
taken_query = time.time() - s
|
|
||||||
if self.logging or taken_query > 0.1:
|
|
||||||
if params: # Query has parameters
|
|
||||||
self.db.log.debug("Query: " + query + " " + str(params) + " (Done in %.4f)" % (time.time() - s))
|
|
||||||
else:
|
|
||||||
self.db.log.debug("Query: " + query + " (Done in %.4f)" % (time.time() - s))
|
|
||||||
finally:
|
|
||||||
self.db.lock.release()
|
|
||||||
|
|
||||||
# Log query stats
|
# Log query stats
|
||||||
if self.db.collect_stats:
|
if self.db.collect_stats:
|
||||||
|
@ -139,7 +132,8 @@ class DbCursor:
|
||||||
self.db.last_query_time = time.time()
|
self.db.last_query_time = time.time()
|
||||||
|
|
||||||
s = time.time()
|
s = time.time()
|
||||||
cursor = self.conn.cursor()
|
cursor = self.db.getConn().cursor()
|
||||||
|
self.db.cursors.add(cursor)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.db.lock.acquire(True)
|
self.db.lock.acquire(True)
|
||||||
|
|
Loading…
Reference in a new issue