Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fd9acfd

Browse files
committed
fix
1 parent b3b4459 commit fd9acfd

2 files changed

Lines changed: 17 additions & 18 deletions

File tree

lib/core/threads.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def __init__(self):
3434
global shared
3535

3636
self.disableStdOut = False
37+
self.hashDBCursor = None
3738
self.lastErrorPage = None
3839
self.lastHTTPError = None
3940
self.lastRedirectMsg = None

lib/utils/hashdb.py

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,31 @@
1111
import sqlite3
1212

1313
from lib.core.settings import UNICODE_ENCODING
14+
from lib.core.threads import getCurrentThreadData
1415

15-
class HashDB:
16+
class HashDB(object):
1617
def __init__(self, filepath):
17-
self.connection = sqlite3.connect(filepath)
18-
self.cursor = self.connection.cursor()
19-
self.cursor.execute("CREATE TABLE IF NOT EXISTS storage (id INTEGER PRIMARY KEY, value TEXT)")
18+
self.filepath = filepath
19+
20+
def _get_cursor(self):
21+
threadData = getCurrentThreadData()
22+
23+
if threadData.hashDBCursor is None:
24+
connection = sqlite3.connect(self.filepath, isolation_level=None)
25+
threadData.hashDBCursor = connection.cursor()
26+
threadData.hashDBCursor.execute("CREATE TABLE IF NOT EXISTS storage (id INTEGER PRIMARY KEY, value TEXT)")
27+
28+
return threadData.hashDBCursor
29+
30+
cursor = property(_get_cursor)
2031

2132
def __del__(self):
2233
self.close()
2334

2435
def close(self):
2536
try:
2637
self.endTransaction()
27-
self.connection.close()
38+
self.cursor.connection.close()
2839
except:
2940
pass
3041

@@ -33,19 +44,6 @@ def hashKey(self, key):
3344
retVal = int(hashlib.md5(key).hexdigest()[:8], 16)
3445
return retVal
3546

36-
def beginTransaction(self):
37-
"""
38-
Great speed improvement can be gained by using explicit transactions around multiple inserts.
39-
Reference: http://stackoverflow.com/questions/4719836/python-and-sqlite3-adding-thousands-of-rows
40-
"""
41-
self.cursor.execute('BEGIN TRANSACTION')
42-
43-
def endTransaction(self):
44-
try:
45-
self.cursor.execute('END TRANSACTION')
46-
except sqlite3.OperationalError:
47-
pass
48-
4947
def retrieve(self, key):
5048
retVal = None
5149
if key:

0 commit comments

Comments
 (0)