8
8
from tortoise .backends .asyncpg .executor import AsyncpgExecutor
9
9
from tortoise .backends .asyncpg .schema_generator import AsyncpgSchemaGenerator
10
10
from tortoise .backends .base .client import (BaseDBAsyncClient , BaseTransactionWrapper ,
11
- ConnectionWrapper , SingleConnectionWrapper )
12
- from tortoise .exceptions import (ConfigurationError , DBConnectionError , IntegrityError ,
13
- OperationalError , TransactionManagementError )
11
+ ConnectionWrapper )
12
+ from tortoise .exceptions import (DBConnectionError , IntegrityError , OperationalError ,
13
+ TransactionManagementError )
14
14
from tortoise .transactions import current_transaction_map
15
15
16
16
@@ -49,7 +49,7 @@ def __init__(self, user: str, password: str, database: str, host: str, port: Sup
49
49
port = self .port ,
50
50
database = self .database
51
51
)
52
- self ._db_pool = None # Type: Optional[asyncpg.pool.Pool]
52
+ # self._db_pool = None # Type: Optional[asyncpg.pool.Pool]
53
53
self ._connection = None # Type: Optional[asyncpg.Connection]
54
54
55
55
self ._transaction_class = type (
@@ -58,10 +58,7 @@ def __init__(self, user: str, password: str, database: str, host: str, port: Sup
58
58
59
59
async def create_connection (self ) -> None :
60
60
try :
61
- if not self .single_connection :
62
- self ._db_pool = await asyncpg .create_pool (self .dsn )
63
- else :
64
- self ._connection = await asyncpg .connect (self .dsn )
61
+ self ._connection = await asyncpg .connect (self .dsn )
65
62
self .log .debug (
66
63
'Created connection with params: user=%s database=%s host=%s port=%s' ,
67
64
self .user , self .database , self .host , self .port
@@ -72,14 +69,11 @@ async def create_connection(self) -> None:
72
69
))
73
70
74
71
async def close (self ) -> None :
75
- if self ._db_pool :
76
- await self ._db_pool .close ()
77
72
if self ._connection :
78
73
await self ._connection .close ()
74
+ self ._connection = None
79
75
80
76
async def db_create (self ) -> None :
81
- single_connection = self .single_connection
82
- self .single_connection = True
83
77
self ._connection = await asyncpg .connect (self .DSN_TEMPLATE .format (
84
78
user = self .user ,
85
79
password = self .password ,
@@ -91,11 +85,8 @@ async def db_create(self) -> None:
91
85
'CREATE DATABASE "{}" OWNER "{}"' .format (self .database , self .user )
92
86
)
93
87
await self ._connection .close () # type: ignore
94
- self .single_connection = single_connection
95
88
96
89
async def db_delete (self ) -> None :
97
- single_connection = self .single_connection
98
- self .single_connection = True
99
90
self ._connection = await asyncpg .connect (self .DSN_TEMPLATE .format (
100
91
user = self .user ,
101
92
password = self .password ,
@@ -105,22 +96,15 @@ async def db_delete(self) -> None:
105
96
))
106
97
try :
107
98
await self .execute_script ('DROP DATABASE "{}"' .format (self .database ))
108
- except asyncpg .InvalidCatalogNameError :
99
+ except asyncpg .InvalidCatalogNameError : # pragma: nocoverage
109
100
pass
110
101
await self ._connection .close () # type: ignore
111
- self .single_connection = single_connection
112
102
113
103
def acquire_connection (self ) -> ConnectionWrapper :
114
- if not self .single_connection :
115
- return self ._db_pool .acquire () # type: ignore
116
- else :
117
- return ConnectionWrapper (self ._connection )
104
+ return ConnectionWrapper (self ._connection )
118
105
119
106
def _in_transaction (self ) -> 'TransactionWrapper' :
120
- if self .single_connection :
121
- return self ._transaction_class (self .connection_name , connection = self ._connection )
122
- else :
123
- return self ._transaction_class (self .connection_name , pool = self ._db_pool )
107
+ return self ._transaction_class (self .connection_name , self ._connection )
124
108
125
109
@translate_exceptions
126
110
async def execute_insert (self , query : str , values : list ) -> int :
@@ -142,29 +126,11 @@ async def execute_script(self, query: str) -> None:
142
126
async with self .acquire_connection () as connection :
143
127
await connection .execute (query )
144
128
145
- async def get_single_connection (self ):
146
- if self .single_connection :
147
- return self ._single_connection_class (self .connection_name , self ._connection )
148
- else :
149
- connection = await self ._db_pool ._acquire (None )
150
- return self ._single_connection_class (self .connection_name , connection )
151
-
152
- async def release_single_connection (self , single_connection ):
153
- if not self .single_connection :
154
- await self ._db_pool .release (single_connection .connection )
155
-
156
129
157
130
class TransactionWrapper (AsyncpgDBClient , BaseTransactionWrapper ):
158
- def __init__ (self , connection_name : str , pool = None , connection = None ) -> None :
159
- if pool and connection :
160
- raise ConfigurationError ('You must pass either connection or pool' )
131
+ def __init__ (self , connection_name : str , connection ) -> None :
161
132
self ._connection = connection
162
133
self .log = logging .getLogger ('db_client' )
163
- self ._pool = pool
164
- self .single_connection = True
165
- self ._single_connection_class = type (
166
- 'SingleConnectionWrapper' , (SingleConnectionWrapper , self .__class__ ), {}
167
- )
168
134
self ._transaction_class = self .__class__
169
135
self ._old_context_value = None
170
136
self .connection_name = connection_name
@@ -173,12 +139,7 @@ def __init__(self, connection_name: str, pool=None, connection=None) -> None:
173
139
def acquire_connection (self ) -> ConnectionWrapper :
174
140
return ConnectionWrapper (self ._connection )
175
141
176
- async def _get_connection (self ):
177
- return await self ._pool ._acquire (None )
178
-
179
142
async def start (self ):
180
- if not self ._connection :
181
- self ._connection = await self ._get_connection ()
182
143
self .transaction = self ._connection .transaction ()
183
144
await self .transaction .start ()
184
145
current_transaction = current_transaction_map [self .connection_name ]
@@ -190,17 +151,11 @@ async def commit(self):
190
151
await self .transaction .commit ()
191
152
except asyncpg .exceptions ._base .InterfaceError as exc :
192
153
raise TransactionManagementError (exc )
193
- if self ._pool :
194
- await self ._pool .release (self ._connection )
195
- self ._connection = None
196
154
current_transaction_map [self .connection_name ].set (self ._old_context_value )
197
155
198
156
async def rollback (self ):
199
157
try :
200
158
await self .transaction .rollback ()
201
159
except asyncpg .exceptions ._base .InterfaceError as exc :
202
160
raise TransactionManagementError (exc )
203
- if self ._pool :
204
- await self ._pool .release (self ._connection )
205
- self ._connection = None
206
161
current_transaction_map [self .connection_name ].set (self ._old_context_value )
0 commit comments