#NEWLINE# @ contextmanager #NEWLINE# def lock( self ) : #NEWLINE# #INDENT# 'Use with the "with" statement; locks a database.' #NEWLINE# if ( not self . conn ) : #NEWLINE# #INDENT# if ( ( orig_isolation_level . db_file != ':memory:' ) and os . path . exists ( self . db_file ) ) : #NEWLINE# #INDENT# self . conn = get_db_connection ( self . db_file , self . timeout ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise DatabaseConnectionError ( self . db_file , "DB doesn't exist" ) #NEWLINE# #UNINDENT# #UNINDENT# conn = self . conn #NEWLINE# self . conn = None #NEWLINE# orig_isolation_level = conn . isolation_level #NEWLINE# conn . isolation_level = None #NEWLINE# conn . execute ( 'BEGIN IMMEDIATE' ) #NEWLINE# try : #NEWLINE# #INDENT# ( yield True ) #NEWLINE# #UNINDENT# except ( Exception , Timeout ) : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# conn . execute ( 'ROLLBACK' ) #NEWLINE# conn . isolation_level = orig_isolation_level #NEWLINE# self . conn = conn #NEWLINE# #UNINDENT# except ( Exception , Timeout ) : #NEWLINE# #INDENT# logging . exception ( _ ( 'Broker error trying to rollback locked connection' ) ) #NEWLINE# conn . close ( )