| | 43 | |
| | 44 | |
| | 45 | get_check_sql = dbmod.get_check_sql |
| | 46 | |
| | 47 | #import logging |
| | 48 | #logger = logging.getLogger( 'back' ) |
| | 49 | |
| | 50 | class 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 | |
| | 69 | import threading |
| | 70 | import thread |
| | 71 | |
| | 72 | def 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 | |
| | 81 | atomcall.lock = threading.Lock() |
| | 82 | |
| | 83 | import pool |
| | 84 | class 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 | |
| | 133 | db = DatabaseWrapper() |