Ticket #7732: oracle_pool.diff

File oracle_pool.diff, 4.3 KB (added by jbronn, 16 years ago)

Oracle pooling patch derived from tarball.

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

     
    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        print self.pool
     245        if self.pool is not None:
     246            if self.connection is None:
     247                self.connection = self.pool.acquire()
     248                print "DEBUG: acquire session "
     249            print self.connection
     250               
    217251            cursor = FormatStylePlaceholderCursor(self.connection)
    218252            # Set oracle date to ansi date format.  This only needs to execute
    219253            # once when we create a new connection.
     
    237271                # Django docs specify cx_Oracle version 4.3.1 or higher, but
    238272                # stmtcachesize is available only in 4.3.2 and up.
    239273                pass
     274        else:
     275            print "Nonono, David Blain"
     276       
    240277        if not cursor:
    241278            cursor = FormatStylePlaceholderCursor(self.connection)
    242279        # Default arraysize of 1 is highly sub-optimal.
    243280        cursor.arraysize = 100
    244281        return cursor
    245282
     283    def close(self):
     284        if self.connection is not None:
     285            print "DEBUG: Release session "
     286            print self.connection
     287           
     288            self.pool.release(self.connection)
     289            self.connection = None
     290
     291
    246292class FormatStylePlaceholderCursor(Database.Cursor):
    247293    """
    248294    Django uses "format" (e.g. '%s') style placeholders, but Oracle uses ":var"
Back to Top