Code

Ticket #7732: oracle_pool.diff

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

Oracle pooling patch derived from tarball.

Line 
1Index: django/db/backends/oracle/base.py
2===================================================================
3--- django/db/backends/oracle/base.py   (revision 7934)
4+++ django/db/backends/oracle/base.py   (working copy)
5@@ -33,6 +33,24 @@
6     interprets_empty_strings_as_nulls = True
7     date_field_supports_time_value = False
8 
9+class DatabasePool:
10+    def __get__(self):
11+        if hasattr(self,'_pool'):
12+            return self._pool
13+        else:
14+            from django.conf import settings
15+            if len(settings.DATABASE_HOST.strip()) == 0:
16+                settings.DATABASE_HOST = 'localhost'
17+            if len(settings.DATABASE_PORT.strip()) != 0:
18+                dsn = Database.makedsn(settings.DATABASE_HOST, int(settings.DATABASE_PORT), settings.DATABASE_NAME)
19+                # FIXME. make the Pool parameters inside the settings
20+                self._pool = cx_Oracle.SessionPool(settings.DATABASE_USER, settings.DATABASE_PASSWORD, dsn, 2, 12, 1)
21+
22+            else:
23+                # FIXME. make the Pool parameters inside the settings
24+                self._pool = cx_Oracle.SessionPool(settings.DATABASE_USER, settings.DATABASE_PASSWORD, settings.DATABASE_NAME, 2, 12, 1)
25+            return self._pool
26+
27 class DatabaseOperations(BaseDatabaseOperations):
28     def autoinc_sql(self, table, column):
29         # To simulate auto-incrementing primary keys in Oracle, we have to
30@@ -199,21 +217,37 @@
31         'iendswith': "LIKEC UPPER(%s) ESCAPE '\\'",
32     }
33     oracle_version = None
34-
35-    def _valid_connection(self):
36-        return self.connection is not None
37-
38-    def _cursor(self, settings):
39-        cursor = None
40-        if not self._valid_connection():
41+   
42+    def _get_pool (self):
43+        if not hasattr (self.__class__, '_pool'):
44+            from django.conf import settings
45+            Database.OPT_Threading = 1
46             if len(settings.DATABASE_HOST.strip()) == 0:
47                 settings.DATABASE_HOST = 'localhost'
48             if len(settings.DATABASE_PORT.strip()) != 0:
49                 dsn = Database.makedsn(settings.DATABASE_HOST, int(settings.DATABASE_PORT), settings.DATABASE_NAME)
50-                self.connection = Database.connect(settings.DATABASE_USER, settings.DATABASE_PASSWORD, dsn, **self.options)
51+                # FIXME. make the Pool parameters inside the settings
52+                p = Database.SessionPool(settings.DATABASE_USER, settings.DATABASE_PASSWORD, dsn, 10, 100, 10, threaded = True)
53+
54             else:
55-                conn_string = "%s/%s@%s" % (settings.DATABASE_USER, settings.DATABASE_PASSWORD, settings.DATABASE_NAME)
56-                self.connection = Database.connect(conn_string, **self.options)
57+                # FIXME. make the Pool parameters inside the settings
58+                p = Database.SessionPool(settings.DATABASE_USER, settings.DATABASE_PASSWORD, settings.DATABASE_NAME, 10, 100, 10, threaded = True)
59+            setattr(self.__class__, '_pool', p)
60+
61+        return getattr(self.__class__, '_pool')
62+       
63+       
64+    pool = property (_get_pool)
65+       
66+    def _cursor(self, settings):
67+        cursor = None
68+        print self.pool
69+        if self.pool is not None:
70+            if self.connection is None:
71+                self.connection = self.pool.acquire()
72+                print "DEBUG: acquire session "
73+            print self.connection
74+               
75             cursor = FormatStylePlaceholderCursor(self.connection)
76             # Set oracle date to ansi date format.  This only needs to execute
77             # once when we create a new connection.
78@@ -237,12 +271,24 @@
79                 # Django docs specify cx_Oracle version 4.3.1 or higher, but
80                 # stmtcachesize is available only in 4.3.2 and up.
81                 pass
82+        else:
83+            print "Nonono, David Blain"
84+       
85         if not cursor:
86             cursor = FormatStylePlaceholderCursor(self.connection)
87         # Default arraysize of 1 is highly sub-optimal.
88         cursor.arraysize = 100
89         return cursor
90 
91+    def close(self):
92+        if self.connection is not None:
93+            print "DEBUG: Release session "
94+            print self.connection
95+           
96+            self.pool.release(self.connection)
97+            self.connection = None
98+
99+
100 class FormatStylePlaceholderCursor(Database.Cursor):
101     """
102     Django uses "format" (e.g. '%s') style placeholders, but Oracle uses ":var"