Django

Code

Ticket #1237: __init__.py.diff

File __init__.py.diff, 3.4 kB (added by junzhang.jn@gmail.com, 3 years ago)
  • __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 
     83class AutoReleaseCursor: 
     84    def __init__( self , dbwrap , host ): 
     85        self.dbwrap = dbwrap 
     86        self.host   = host 
     87     
     88    def __getattr__( self , key ): 
     89        if hasattr( self.host , key ): 
     90            return getattr( self.host , key ) 
     91        else: 
     92            return getattr( self , key ) 
     93     
     94    def __del__( self ): 
     95        #logger.info( 'cursor close %s' , self.dbwrap ) 
     96        self.host.close() 
     97        self.dbwrap.close() 
     98     
     99import pool 
     100class DatabaseWrapper: 
     101    def __init__(self): 
     102        self.connections = {} 
     103        self.pool = pool.DBPoolWithThread( ConnectObjectFactory() , freetime = 60 * 5 , threadsafety = dbmod.Database.threadsafety ) 
     104        self.queries = [] 
     105 
     106    def cursor(self): 
     107        con = self.getcon() 
     108        if con is None: 
     109            con = self.pool.borrow_object() 
     110            self.setcon( con ) 
     111        if con: 
     112            return AutoReleaseCursor( self , con.cursor() ) 
     113 
     114    def commit(self): 
     115        con = self.getcon() 
     116        if con: 
     117            return con.commit() 
     118 
     119    def rollback(self): 
     120        con = self.getcon() 
     121        if con: 
     122            return con.rollback() 
     123 
     124    def close(self): 
     125        con = self.getcon() 
     126        if con is not None: 
     127            self.pool.return_object( con ) 
     128            self.delcon() 
     129     
     130    def quote_name(self, name): 
     131        con = self.getcon() 
     132        if con is None: 
     133            try: 
     134                con = self.pool.borrow_object() 
     135                ret = con.quote_name(name) 
     136            finally: 
     137                self.pool.return_object( con ) 
     138        else: 
     139            ret = con.quote_name(name) 
     140        return ret 
     141 
     142    @atomcall 
     143    def getcon( self ): 
     144        threadid = thread.get_ident() 
     145        return self.connections.get( threadid , None ) 
     146         
     147    @atomcall 
     148    def delcon( self ): 
     149        threadid = thread.get_ident() 
     150        del self.connections[ threadid ] 
     151         
     152    @atomcall 
     153    def setcon( self , con ): 
     154        threadid = thread.get_ident() 
     155        self.connections[threadid] = con 
     156 
     157db = DatabaseWrapper()