26 | | connection = backend.DatabaseWrapper() |
| 26 | if 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() |
| 107 | else: |
| 108 | connection = backend.DatabaseWrapper() |
| 109 | |