1
1
# django-db-connection-pool
2
2
3
- * :star : If this project is helpful to you, please light up the star, Thank you:smile : *
3
+ :star : If this project is helpful to you, please light up the star, Thank you:smile :
4
4
5
- MySQL & Oracle & PostgreSQL & JDBC (Oracle, OceanBase) connection pool components for Django,
6
- Be based on [ SQLAlchemy] ( https://github.com/sqlalchemy/sqlalchemy ) .
5
+ MySQL & Oracle & PostgreSQL & JDBC (Oracle, OceanBase) connection pool components for Django,
6
+ Be based on [ SQLAlchemy] ( https://github.com/sqlalchemy/sqlalchemy ) .
7
7
Works fine in multiprocessing and multithreading django project.
8
8
9
9
* [ 中文版] ( README_CN.md )
@@ -13,22 +13,29 @@ Works fine in multiprocessing and multithreading django project.
13
13
### Installation
14
14
15
15
Install with ` pip ` with all engines:
16
+
16
17
``` bash
17
18
$ pip install django-db-connection-pool[all]
18
19
```
20
+
19
21
or select specific engines:
22
+
20
23
``` bash
21
24
$ pip install django-db-connection-pool[mysql,oracle,postgresql,jdbc]
22
25
```
26
+
23
27
or one of mysql,oracle,postgresql,jdbc
28
+
24
29
``` bash
25
30
$ pip install django-db-connection-pool[oracle]
26
31
```
27
32
28
33
### Update settings.DATABASES
29
34
30
- #### MySQL
35
+ #### MySQL
36
+
31
37
change ` django.db.backends.mysql ` to ` dj_db_conn_pool.backends.mysql ` :
38
+
32
39
``` python
33
40
DATABASES = {
34
41
' default' : {
@@ -37,8 +44,10 @@ DATABASES = {
37
44
}
38
45
```
39
46
40
- #### Oracle
47
+ #### Oracle
48
+
41
49
change ` django.db.backends.oracle ` to ` dj_db_conn_pool.backends.oracle ` :
50
+
42
51
``` python
43
52
DATABASES = {
44
53
' default' : {
@@ -47,8 +56,10 @@ DATABASES = {
47
56
}
48
57
```
49
58
50
- #### PostgreSQL
59
+ #### PostgreSQL
60
+
51
61
change ` django.db.backends.postgresql ` to ` dj_db_conn_pool.backends.postgresql ` :
62
+
52
63
``` python
53
64
DATABASES = {
54
65
' default' : {
@@ -58,82 +69,91 @@ DATABASES = {
58
69
```
59
70
60
71
#### Pool options(optional)
72
+
61
73
you can provide additional options to pass to SQLAlchemy's pool creation, key's name is ` POOL_OPTIONS ` :
62
74
63
75
``` python
64
76
DATABASES = {
65
77
' default' : {
66
- ' POOL_OPTIONS' : {
78
+ ' POOL_OPTIONS' : {
67
79
' POOL_SIZE' : 10 ,
68
80
' MAX_OVERFLOW' : 10 ,
69
81
' RECYCLE' : 24 * 60 * 60
70
82
}
71
- }
72
- }
83
+ }
84
+ }
73
85
```
74
86
75
- ` django-db-connection-pool ` has more configuration options here: [ PoolContainer.pool_default_params] ( https://github.com/altairbow/django-db-connection-pool/blob/master/dj_db_conn_pool/core/__init__.py#L13-L20 )
76
-
87
+ ` django-db-connection-pool ` has more configuration options
88
+ here: [ PoolContainer.pool_default_params] ( https://github.com/altairbow/django-db-connection-pool/blob/master/dj_db_conn_pool/core/__init__.py#L13-L20 )
89
+
77
90
Here's the explanation of these options(from SQLAlchemy's Doc):
78
91
79
92
* ** pool_size** : The size of the pool to be maintained,
80
- defaults to 5. This is the largest number of connections that
81
- will be kept persistently in the pool. Note that the pool
82
- begins with no connections; once this number of connections
83
- is requested, that number of connections will remain.
84
- ` pool_size ` can be set to 0 to indicate no size limit; to
85
- disable pooling, use a :class:` ~sqlalchemy.pool.NullPool `
86
- instead.
93
+ defaults to 5. This is the largest number of connections that
94
+ will be kept persistently in the pool. Note that the pool
95
+ begins with no connections; once this number of connections
96
+ is requested, that number of connections will remain.
97
+ ` pool_size ` can be set to 0 to indicate no size limit; to
98
+ disable pooling, use a :class:` ~sqlalchemy.pool.NullPool `
99
+ instead.
87
100
88
101
* ** max_overflow** : The maximum overflow size of the
89
- pool. When the number of checked-out connections reaches the
90
- size set in pool_size, additional connections will be
91
- returned up to this limit. When those additional connections
92
- are returned to the pool, they are disconnected and
93
- discarded. It follows then that the total number of
94
- simultaneous connections the pool will allow is pool_size +
95
- ` max_overflow ` , and the total number of "sleeping"
96
- connections the pool will allow is pool_size. ` max_overflow `
97
- can be set to -1 to indicate no overflow limit; no limit
98
- will be placed on the total number of concurrent
99
- connections. Defaults to 10.
100
-
101
- * ** recycle** : If set to a value other than -1, number of seconds
102
- between connection recycling, which means upon checkout,
103
- if this timeout is surpassed the connection will be closed
104
- and replaced with a newly opened connection.
105
- Defaults to -1.
102
+ pool. When the number of checked-out connections reaches the
103
+ size set in pool_size, additional connections will be
104
+ returned up to this limit. When those additional connections
105
+ are returned to the pool, they are disconnected and
106
+ discarded. It follows then that the total number of
107
+ simultaneous connections the pool will allow is pool_size +
108
+ ` max_overflow ` , and the total number of "sleeping"
109
+ connections the pool will allow is pool_size. ` max_overflow `
110
+ can be set to -1 to indicate no overflow limit; no limit
111
+ will be placed on the total number of concurrent
112
+ connections. Defaults to 10.
113
+
114
+ * ** recycle** : If set to a value other than -1, number of seconds
115
+ between connection recycling, which means upon checkout,
116
+ if this timeout is surpassed the connection will be closed
117
+ and replaced with a newly opened connection.
118
+ Defaults to -1.
106
119
107
120
Or, you can use dj_db_conn_pool.setup to change default arguments(for each pool's creation), before using database pool:
108
121
109
122
``` python
110
123
import dj_db_conn_pool
124
+
111
125
dj_db_conn_pool.setup(pool_size = 100 , max_overflow = 50 )
112
126
```
113
127
114
128
#### multiprocessing environment
115
- In a multiprocessing environment, such as uWSGI, each process will have its own ` dj_db_conn_pool.core:pool_container ` object,
116
- It means that each process has an independent connection pool, for example:
129
+
130
+ In a multiprocessing environment, such as uWSGI, each process will have its own ` dj_db_conn_pool.core:pool_container `
131
+ object,
132
+ It means that each process has an independent connection pool, for example:
117
133
The ` POOL_OPTIONS ` configuration of database ` db1 ` is` { 'POOL_SIZE': 10, 'MAX_OVERFLOW': 20 } ` ,
118
134
If uWSGI starts 8 worker processes, then the total connection pool size of ` db1 ` is ` 8 * 10 ` ,
119
135
The maximum number of connections will not exceed ` 8 * 10 + 8 * 20 `
120
136
121
-
122
137
## JDBC
138
+
123
139
Thanks to [ JPype] ( https://github.com/jpype-project/jpype ) ,
124
140
django-db-connection-pool can connect to database by jdbc
125
141
126
142
### Usage
143
+
127
144
#### Set Java runtime environment
145
+
128
146
``` bash
129
147
export JAVA_HOME=$PATH_TO_JRE ;
130
148
export CLASSPATH=$PATH_RO_JDBC_DRIVER_JAR
131
149
```
132
150
133
151
#### Update settings.DATABASES
152
+
134
153
##### Oracle
135
154
136
155
change ` django.db.backends.oracle ` to ` dj_db_conn_pool.backends.jdbc.oracle ` :
156
+
137
157
``` python
138
158
DATABASES = {
139
159
' default' : {
@@ -143,7 +163,9 @@ DATABASES = {
143
163
```
144
164
145
165
##### OceanBase
166
+
146
167
use ` dj_db_conn_pool.backends.jdbc.oceanbase ` :
168
+
147
169
``` python
148
170
DATABASES = {
149
171
' default' : {
@@ -153,8 +175,10 @@ DATABASES = {
153
175
```
154
176
155
177
### Performing raw SQL queries
156
- Just like django's built-in backends, all JDBC backends support named parameters in raw SQL queries,
178
+
179
+ Just like django's built-in backends, all JDBC backends support named parameters in raw SQL queries,
157
180
you can execute raw sql queries like this:
181
+
158
182
``` python
159
183
from django.db import connections
160
184
0 commit comments