Ticket #1237: django.db.__init__.py.diff

File django.db.__init__.py.diff, 2.9 KB (added by junzhang.jn@…, 18 years ago)
  • __init__.py

     
    2323get_introspection_module = lambda: __import__('django.db.backends.%s.introspection' % settings.DATABASE_ENGINE, '', '', [''])
    2424get_creation_module = lambda: __import__('django.db.backends.%s.creation' % settings.DATABASE_ENGINE, '', '', [''])
    2525
    26 connection = backend.DatabaseWrapper()
     26if settings.USE_DBPOOL:
     27    class ConnectObjectFactory:
     28        def create_object( self ):
     29            return backend.DatabaseWrapper()
     30       
     31        def destroy_object( self , obj ):
     32            obj.close()
     33            del obj
     34       
     35        def validate_object( self , obj ):
     36            cu = obj.cursor()
     37            try:
     38                cu.execute( backend.get_check_sql() )
     39                return True
     40            except:
     41                return False
     42
     43    import threading
     44    import thread
     45
     46    def atomcall( func ):
     47        def call( *arg , **kwarg ):
     48            try:
     49                atomcall.lock.acquire()
     50                return func( *arg , **kwarg )
     51            finally:
     52                atomcall.lock.release()
     53        return call
     54       
     55    atomcall.lock = threading.Lock()
     56
     57    import pool
     58    class DatabaseWrapper:
     59        def __init__(self):
     60            self._conn = backend.DatabaseWrapper()
     61            self.connections = {}
     62            self.pool = pool.DBPoolWithThread( ConnectObjectFactory() , freetime = 1 , threadsafety = backend.Database.threadsafety )
     63            self.queries = []
     64
     65        def cursor(self):
     66            con = self.getcon()
     67            if con is None:
     68                con = self.pool.borrow_object()
     69                self.setcon( con )
     70            if con:
     71                return con.cursor()
     72
     73        def commit(self):
     74            con = self.getcon()
     75            if con:
     76                return con.commit()
     77
     78        def rollback(self):
     79            con = self.getcon()
     80            if con:
     81                return con.rollback()
     82
     83        def close(self):
     84            con = self.getcon()
     85            if con is not None:
     86                self.pool.return_object( con )
     87                self.delcon()
     88       
     89        def quote_name(self, name):
     90            return self._conn.quote_name( name )
     91
     92        @atomcall
     93        def getcon( self ):
     94            threadid = thread.get_ident()
     95            return self.connections.get( threadid , None )
     96           
     97        @atomcall
     98        def delcon( self ):
     99            threadid = thread.get_ident()
     100            del self.connections[ threadid ]
     101           
     102        @atomcall
     103        def setcon( self , con ):
     104            threadid = thread.get_ident()
     105            self.connections[threadid] = con
     106    connection = DatabaseWrapper()
     107else:
     108    connection = backend.DatabaseWrapper()
     109   
    27110DatabaseError = backend.DatabaseError
    28111
    29112# Register an event that closes the database connection
Back to Top