@@ -170,9 +170,12 @@ def __init__(self, filename=None, tablename='unnamed', flag='c',
170
170
171
171
logger .info ("opening Sqlite table %r in %s" % (tablename , filename ))
172
172
MAKE_TABLE = 'CREATE TABLE IF NOT EXISTS "%s" (key TEXT PRIMARY KEY, value BLOB)' % self .tablename
173
- self .conn = self ._new_conn ()
174
- self .conn .execute (MAKE_TABLE )
175
- self .conn .commit ()
173
+ try :
174
+ self .conn = self ._new_conn ()
175
+ self .conn .execute (MAKE_TABLE )
176
+ self .conn .commit ()
177
+ except sqlite3 .OperationalError as e :
178
+ raise RuntimeError (str (e ))
176
179
if flag == 'w' :
177
180
self .clear ()
178
181
@@ -381,20 +384,14 @@ def __init__(self, filename, autocommit, journal_mode):
381
384
self .setDaemon (True ) # python2.5-compatible
382
385
self .exception = None
383
386
self .log = logging .getLogger ('sqlitedict.SqliteMultithread' )
387
+ self .connect ()
384
388
self .start ()
385
389
386
390
def run (self ):
387
- if self .autocommit :
388
- conn = sqlite3 .connect (self .filename , isolation_level = None , check_same_thread = False )
389
- else :
390
- conn = sqlite3 .connect (self .filename , check_same_thread = False )
391
- conn .execute ('PRAGMA journal_mode = %s' % self .journal_mode )
392
- conn .text_factory = str
393
- cursor = conn .cursor ()
394
- conn .commit ()
395
- cursor .execute ('PRAGMA synchronous=OFF' )
396
391
397
392
res = None
393
+ conn = None
394
+ cursor = None
398
395
while True :
399
396
req , arg , res , outer_stack = self .reqs .get ()
400
397
if req == '--close--' :
@@ -404,35 +401,25 @@ def run(self):
404
401
conn .commit ()
405
402
if res :
406
403
res .put ('--no more--' )
404
+ elif req == '--connect--' :
405
+ try :
406
+ if self .autocommit :
407
+ conn = sqlite3 .connect (self .filename , isolation_level = None , check_same_thread = False )
408
+ else :
409
+ conn = sqlite3 .connect (self .filename , check_same_thread = False )
410
+ except Exception as err :
411
+ self .store_error (outer_stack , err )
412
+ else :
413
+ conn .execute ('PRAGMA journal_mode = %s' % self .journal_mode )
414
+ conn .text_factory = str
415
+ cursor = conn .cursor ()
416
+ conn .commit ()
417
+ cursor .execute ('PRAGMA synchronous=OFF' )
407
418
else :
408
419
try :
409
420
cursor .execute (req , arg )
410
421
except Exception as err :
411
- self .exception = (e_type , e_value , e_tb ) = sys .exc_info ()
412
- inner_stack = traceback .extract_stack ()
413
-
414
- # An exception occurred in our thread, but we may not
415
- # immediately able to throw it in our calling thread, if it has
416
- # no return `res` queue: log as level ERROR both the inner and
417
- # outer exception immediately.
418
- #
419
- # Any iteration of res.get() or any next call will detect the
420
- # inner exception and re-raise it in the calling Thread; though
421
- # it may be confusing to see an exception for an unrelated
422
- # statement, an ERROR log statement from the 'sqlitedict.*'
423
- # namespace contains the original outer stack location.
424
- self .log .error ('Inner exception:' )
425
- for item in traceback .format_list (inner_stack ):
426
- self .log .error (item )
427
- self .log .error ('' ) # deliniate traceback & exception w/blank line
428
- for item in traceback .format_exception_only (e_type , e_value ):
429
- self .log .error (item )
430
-
431
- self .log .error ('' ) # exception & outer stack w/blank line
432
- self .log .error ('Outer stack:' )
433
- for item in traceback .format_list (outer_stack ):
434
- self .log .error (item )
435
- self .log .error ('Exception will be re-raised at next call.' )
422
+ self .store_error (outer_stack , err )
436
423
437
424
if res :
438
425
for rec in cursor :
@@ -443,9 +430,38 @@ def run(self):
443
430
conn .commit ()
444
431
445
432
self .log .debug ('received: %s, send: --no more--' , req )
446
- conn .close ()
433
+ if conn is not None :
434
+ conn .close ()
447
435
res .put ('--no more--' )
448
436
437
+ def store_error (self , outer_stack , err ):
438
+ """ Store error to be raised in any next call """
439
+ self .exception = (e_type , e_value , e_tb ) = sys .exc_info ()
440
+ inner_stack = traceback .extract_stack ()
441
+
442
+ # An exception occurred in our thread, but we may not
443
+ # immediately able to throw it in our calling thread, if it has
444
+ # no return `res` queue: log as level ERROR both the inner and
445
+ # outer exception immediately.
446
+ #
447
+ # Any iteration of res.get() or any next call will detect the
448
+ # inner exception and re-raise it in the calling Thread; though
449
+ # it may be confusing to see an exception for an unrelated
450
+ # statement, an ERROR log statement from the 'sqlitedict.*'
451
+ # namespace contains the original outer stack location.
452
+ self .log .error ('Inner exception:' )
453
+ for item in traceback .format_list (inner_stack ):
454
+ self .log .error (item )
455
+ self .log .error ('' ) # deliniate traceback & exception w/blank line
456
+ for item in traceback .format_exception_only (e_type , e_value ):
457
+ self .log .error (item )
458
+
459
+ self .log .error ('' ) # exception & outer stack w/blank line
460
+ self .log .error ('Outer stack:' )
461
+ for item in traceback .format_list (outer_stack ):
462
+ self .log .error (item )
463
+ self .log .error ('Exception will be re-raised at next call.' )
464
+
449
465
def check_raise_error (self ):
450
466
"""
451
467
Check for and raise exception for any previous sqlite query.
@@ -527,6 +543,9 @@ def commit(self, blocking=True):
527
543
# otherwise, we fire and forget as usual.
528
544
self .execute ('--commit--' )
529
545
546
+ def connect (self ):
547
+ self .execute ('--connect--' )
548
+
530
549
def close (self , force = False ):
531
550
if force :
532
551
# If a SqliteDict is being killed or garbage-collected, then select_one()
0 commit comments