Ticket #7732: oracle_pool.3.diff

File oracle_pool.3.diff, 4.7 KB (added by nleschev, 16 years ago)
  • django/conf/global_settings.py

     
    129129DATABASE_PORT = ''             # Set to empty string for default. Not used with sqlite3.
    130130DATABASE_OPTIONS = {}          # Set to empty dictionary for default.
    131131
     132#Database session pool settings. At this monent only oracle backend use.
     133DATABASE_SESSION_POOL=False   
     134DATABASE_SESSION_POOL_MIN=1
     135DATABASE_SESSION_POOL_MAX=3
     136DATABASE_SESSION_POOL_INCREMENT=1
     137
     138
    132139# Host for sending e-mail.
    133140EMAIL_HOST = 'localhost'
    134141
  • django/db/backends/oracle/base.py

     
    198198        'iendswith': "LIKEC UPPER(%s) ESCAPE '\\'",
    199199    }
    200200    oracle_version = None
     201   
     202    def _get_pool (self):
     203        if not hasattr (self.__class__, '_pool'):
     204            from django.conf import settings
     205            Database.OPT_Threading = 1
     206            if len(settings.DATABASE_HOST.strip()) == 0:
     207                settings.DATABASE_HOST = 'localhost'
     208            if len(settings.DATABASE_PORT.strip()) != 0:
     209                dsn = Database.makedsn(settings.DATABASE_HOST, int(settings.DATABASE_PORT), settings.DATABASE_NAME)
     210                p = Database.SessionPool(settings.DATABASE_USER, settings.DATABASE_PASSWORD, dsn, settings.DATABASE_SESSION_POOL_MIN, settings.DATABASE_SESSION_POOL_MAX, settings.DATABASE_SESSION_POOL_INCREMENT, threaded = True)
     211            else:
     212                p = Database.SessionPool(settings.DATABASE_USER, settings.DATABASE_PASSWORD, settings.DATABASE_NAME, settings.DATABASE_SESSION_POOL_MIN, settings.DATABASE_SESSION_POOL_MAX, settings.DATABASE_SESSION_POOL_INCREMENT, threaded = True)
     213            setattr(self.__class__, '_pool', p)
    201214
     215        return getattr(self.__class__, '_pool')
     216       
     217       
     218    pool = property (_get_pool)
     219
    202220    def _valid_connection(self):
    203221        return self.connection is not None
    204222
     223       
    205224    def _cursor(self, settings):
    206225        cursor = None
    207         if not self._valid_connection():
    208             if len(settings.DATABASE_HOST.strip()) == 0:
    209                 settings.DATABASE_HOST = 'localhost'
    210             if len(settings.DATABASE_PORT.strip()) != 0:
    211                 dsn = Database.makedsn(settings.DATABASE_HOST, int(settings.DATABASE_PORT), settings.DATABASE_NAME)
    212                 self.connection = Database.connect(settings.DATABASE_USER, settings.DATABASE_PASSWORD, dsn, **self.options)
    213             else:
    214                 conn_string = "%s/%s@%s" % (settings.DATABASE_USER, settings.DATABASE_PASSWORD, settings.DATABASE_NAME)
    215                 self.connection = Database.connect(conn_string, **self.options)
     226        if settings.DATABASE_SESSION_POOL:
     227            if self.pool is not None:
     228                if self.connection is None:
     229                    self.connection = self.pool.acquire()
     230        else:
     231            if not self._valid_connection():
     232                if len(settings.DATABASE_HOST.strip()) == 0:
     233                    settings.DATABASE_HOST = 'localhost'
     234                if len(settings.DATABASE_PORT.strip()) != 0:
     235                    dsn = Database.makedsn(settings.DATABASE_HOST, int(settings.DATABASE_PORT), settings.DATABASE_NAME)
     236                    self.connection = Database.connect(settings.DATABASE_USER, settings.DATABASE_PASSWORD, dsn, **self.options)
     237                else:
     238                    conn_string = "%s/%s@%s" % (settings.DATABASE_USER, settings.DATABASE_PASSWORD, settings.DATABASE_NAME)
     239                    self.connection = Database.connect(conn_string, **self.options)
     240
     241               
    216242            cursor = FormatStylePlaceholderCursor(self.connection)
    217243            # Set oracle date to ansi date format.  This only needs to execute
    218244            # once when we create a new connection.
     
    236262                # Django docs specify cx_Oracle version 4.3.1 or higher, but
    237263                # stmtcachesize is available only in 4.3.2 and up.
    238264                pass
     265       
    239266        if not cursor:
    240267            cursor = FormatStylePlaceholderCursor(self.connection)
    241268        # Default arraysize of 1 is highly sub-optimal.
    242269        cursor.arraysize = 100
    243270        return cursor
    244271
     272    def close(self):
     273        from django.conf import settings
     274        if settings.DATABASE_SESSION_POOL:
     275            if self.connection is not None:
     276                self.pool.release(self.connection)
     277                self.connection = None
     278        else:
     279            if self.connection is not None:
     280                self.connection.close()
     281                self.connection = None
     282
     283
     284
    245285class FormatStylePlaceholderCursor(Database.Cursor):
    246286    """
    247287    Django uses "format" (e.g. '%s') style placeholders, but Oracle uses ":var"
Back to Top