Text only
15 Jul 2011
 

Django database pooling (postgreql and mysql) via sqlalchemy

 
Tools: wrap/unwrap  
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
diff -u -r db/backends/mysql/base.py db/backends/mysql/base.py
--- db/backends/mysql/base.py 2010-09-12 15:31:28.000000000 +0400
+++ db/backends/mysql/base.py 2010-09-12 15:54:33.944885295 +0400
@@ -13,6 +13,9 @@
from django.core.exceptions import ImproperlyConfigured
raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
+from sqlalchemy import pool
+Database = pool.manage(Database)
+
# We want version (1, 2, 1, 'final', 2) or later. We can't just use
# lexicographic ordering in this check because then (1, 2, 1, 'gamma')
# inadvertently passes the version test.
@@ -294,7 +297,27 @@
# "UPDATE", not the number of changed rows.
kwargs['client_flag'] = CLIENT.FOUND_ROWS
kwargs.update(settings_dict['OPTIONS'])
- self.connection = Database.connect(**kwargs)
+ #self.connection = Database.connect(**kwargs)
+ if settings.DATABASE_HOST.startswith('/'):
+ self.connection = Database.connect(
+ port=kwargs['port'],
+ unix_socket=kwargs['unix_socket'],
+ user=kwargs['user'],
+ db=kwargs['db'],
+ passwd=kwargs['passwd'],
+ use_unicode=kwargs['use_unicode'],
+ charset='utf8'
+ )
+ else:
+ self.connection = Database.connect(
+ host=kwargs['host'],
+ port=kwargs.get('port', 3306),
+ user=kwargs['user'],
+ db=kwargs['db'],
+ passwd=kwargs['passwd'],
+ use_unicode=kwargs['use_unicode'],
+ charset='utf8'
+ )
self.connection.encoders[SafeUnicode] = self.connection.encoders[unicode]
self.connection.encoders[SafeString] = self.connection.encoders[str]
connection_created.send(sender=self.__class__, connection=self)
diff -u -r db/backends/postgresql_psycopg2/base.py db/backends/postgresql_psycopg2/base.py
--- db/backends/postgresql_psycopg2/base.py 2010-09-12 15:31:28.000000000 +0400
+++ db/backends/postgresql_psycopg2/base.py 2010-09-12 15:29:33.734884573 +0400
@@ -16,6 +16,7 @@
from django.db.backends.postgresql_psycopg2.introspection import DatabaseIntrospection
from django.utils.safestring import SafeUnicode, SafeString
+
try:
import psycopg2 as Database
import psycopg2.extensions
@@ -23,6 +24,9 @@
from django.core.exceptions import ImproperlyConfigured
raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
+import sqlalchemy.pool as pool
+Database = pool.manage(Database)
+
DatabaseError = Database.DatabaseError
IntegrityError = Database.IntegrityError