Django

Code

Ticket #7732: oracle_pool.2.diff

File oracle_pool.2.diff, 4.1 kB (added by ikelly, 2 years ago)

Modified patch with print statements removed

  • django/db/backends/oracle/base.py

    old new  
    3333    interprets_empty_strings_as_nulls = True 
    3434    date_field_supports_time_value = False 
    3535 
     36class DatabasePool: 
     37    def __get__(self): 
     38        if hasattr(self,'_pool'): 
     39            return self._pool 
     40        else: 
     41            from django.conf import settings 
     42            if len(settings.DATABASE_HOST.strip()) == 0: 
     43                settings.DATABASE_HOST = 'localhost' 
     44            if len(settings.DATABASE_PORT.strip()) != 0: 
     45                dsn = Database.makedsn(settings.DATABASE_HOST, int(settings.DATABASE_PORT), settings.DATABASE_NAME) 
     46                # FIXME. make the Pool parameters inside the settings 
     47                self._pool = cx_Oracle.SessionPool(settings.DATABASE_USER, settings.DATABASE_PASSWORD, dsn, 2, 12, 1) 
     48 
     49            else: 
     50                # FIXME. make the Pool parameters inside the settings 
     51                self._pool = cx_Oracle.SessionPool(settings.DATABASE_USER, settings.DATABASE_PASSWORD, settings.DATABASE_NAME, 2, 12, 1) 
     52            return self._pool 
     53 
    3654class DatabaseOperations(BaseDatabaseOperations): 
    3755    def autoinc_sql(self, table, column): 
    3856        # To simulate auto-incrementing primary keys in Oracle, we have to 
     
    199217        'iendswith': "LIKEC UPPER(%s) ESCAPE '\\'", 
    200218    } 
    201219    oracle_version = None 
    202  
    203     def _valid_connection(self): 
    204         return self.connection is not None 
    205  
    206     def _cursor(self, settings): 
    207         cursor = None 
    208         if not self._valid_connection(): 
     220     
     221    def _get_pool (self): 
     222        if not hasattr (self.__class__, '_pool'): 
     223            from django.conf import settings 
     224            Database.OPT_Threading = 1 
    209225            if len(settings.DATABASE_HOST.strip()) == 0: 
    210226                settings.DATABASE_HOST = 'localhost' 
    211227            if len(settings.DATABASE_PORT.strip()) != 0: 
    212228                dsn = Database.makedsn(settings.DATABASE_HOST, int(settings.DATABASE_PORT), settings.DATABASE_NAME) 
    213                 self.connection = Database.connect(settings.DATABASE_USER, settings.DATABASE_PASSWORD, dsn, **self.options) 
     229                # FIXME. make the Pool parameters inside the settings 
     230                p = Database.SessionPool(settings.DATABASE_USER, settings.DATABASE_PASSWORD, dsn, 10, 100, 10, threaded = True) 
     231 
    214232            else: 
    215                 conn_string = "%s/%s@%s" % (settings.DATABASE_USER, settings.DATABASE_PASSWORD, settings.DATABASE_NAME) 
    216                 self.connection = Database.connect(conn_string, **self.options) 
     233                # FIXME. make the Pool parameters inside the settings 
     234                p = Database.SessionPool(settings.DATABASE_USER, settings.DATABASE_PASSWORD, settings.DATABASE_NAME, 10, 100, 10, threaded = True) 
     235            setattr(self.__class__, '_pool', p) 
     236 
     237        return getattr(self.__class__, '_pool') 
     238         
     239         
     240    pool = property (_get_pool) 
     241         
     242    def _cursor(self, settings): 
     243        cursor = None 
     244        if self.pool is not None: 
     245            if self.connection is None: 
     246                self.connection = self.pool.acquire() 
     247                 
    217248            cursor = FormatStylePlaceholderCursor(self.connection) 
    218249            # Set oracle date to ansi date format.  This only needs to execute 
    219250            # once when we create a new connection. 
     
    237268                # Django docs specify cx_Oracle version 4.3.1 or higher, but 
    238269                # stmtcachesize is available only in 4.3.2 and up. 
    239270                pass 
     271         
    240272        if not cursor: 
    241273            cursor = FormatStylePlaceholderCursor(self.connection) 
    242274        # Default arraysize of 1 is highly sub-optimal. 
    243275        cursor.arraysize = 100 
    244276        return cursor 
    245277 
     278    def close(self): 
     279        if self.connection is not None: 
     280            self.pool.release(self.connection) 
     281            self.connection = None 
     282 
     283 
    246284class FormatStylePlaceholderCursor(Database.Cursor): 
    247285    """ 
    248286    Django uses "format" (e.g. '%s') style placeholders, but Oracle uses ":var"