Django

Code

Ticket #1237: __init__.py.2.diff

File __init__.py.2.diff, 2.7 kB (added by junzhang.jn@gmail.com, 3 years ago)

delete AutoReleaseCursor? , it breaks transaction in multithread when 'threadsafety > 1'.

  • __init__.py

    old new  
    2424        (exc, DATABASE_ENGINE, ", ".join(map(repr, available_backends))) 
    2525 
    2626DatabaseError = dbmod.DatabaseError 
    27 db = dbmod.DatabaseWrapper() 
     27#db = dbmod.DatabaseWrapper() 
    2828dictfetchone = dbmod.dictfetchone 
    2929dictfetchmany = dbmod.dictfetchmany 
    3030dictfetchall = dbmod.dictfetchall 
     
    4040OPERATOR_MAPPING = dbmod.OPERATOR_MAPPING 
    4141DATA_TYPES = dbmod.DATA_TYPES 
    4242DATA_TYPES_REVERSE = dbmod.DATA_TYPES_REVERSE 
     43 
     44 
     45get_check_sql = dbmod.get_check_sql 
     46 
     47#import logging 
     48#logger = logging.getLogger( 'back' ) 
     49 
     50class ConnectObjectFactory: 
     51    def __init__( self ): 
     52        pass 
     53         
     54    def create_object( self ): 
     55        return dbmod.DatabaseWrapper() 
     56     
     57    def destroy_object( self , obj ): 
     58        obj.close() 
     59        del obj 
     60     
     61    def validate_object( self , obj ): 
     62        cu = obj.cursor() 
     63        try: 
     64            cu.execute( get_check_sql() ) 
     65            return True 
     66        except: 
     67            return False 
     68 
     69import threading 
     70import thread 
     71 
     72def atomcall( func ): 
     73    def call( *arg , **kwarg ): 
     74        try: 
     75            atomcall.lock.acquire() 
     76            return func( *arg , **kwarg ) 
     77        finally: 
     78            atomcall.lock.release() 
     79    return call 
     80     
     81atomcall.lock = threading.Lock() 
     82 
     83import pool 
     84class DatabaseWrapper: 
     85    def __init__(self): 
     86        self._conn = dbmod.DatabaseWrapper() 
     87        self.connections = {} 
     88        self.pool = pool.DBPoolWithThread( ConnectObjectFactory() , freetime = 60 * 5 , threadsafety = dbmod.Database.threadsafety ) 
     89        self.queries = [] 
     90 
     91    def cursor(self): 
     92        con = self.getcon() 
     93        if con is None: 
     94            con = self.pool.borrow_object() 
     95            self.setcon( con ) 
     96        if con: 
     97            return con.cursor() 
     98 
     99    def commit(self): 
     100        con = self.getcon() 
     101        if con: 
     102            return con.commit() 
     103 
     104    def rollback(self): 
     105        con = self.getcon() 
     106        if con: 
     107            return con.rollback() 
     108 
     109    def close(self): 
     110        con = self.getcon() 
     111        if con is not None: 
     112            self.pool.return_object( con ) 
     113            self.delcon() 
     114     
     115    def quote_name(self, name): 
     116        return self._conn.quote_name( name ) 
     117 
     118    @atomcall 
     119    def getcon( self ): 
     120        threadid = thread.get_ident() 
     121        return self.connections.get( threadid , None ) 
     122         
     123    @atomcall 
     124    def delcon( self ): 
     125        threadid = thread.get_ident() 
     126        del self.connections[ threadid ] 
     127         
     128    @atomcall 
     129    def setcon( self , con ): 
     130        threadid = thread.get_ident() 
     131        self.connections[threadid] = con 
     132 
     133db = DatabaseWrapper()