@@ -122,6 +122,38 @@ class SQLConnectionHandler(object):
122
122
2275 : str , # cstring
123
123
}
124
124
125
+ _user_args = {
126
+ 'user' : qiita_config .user ,
127
+ 'password' : qiita_config .password ,
128
+ 'database' : qiita_config .database ,
129
+ 'host' : qiita_config .host ,
130
+ 'port' : qiita_config .port }
131
+
132
+ _admin_args = {
133
+ 'user' : qiita_config .admin_user ,
134
+ 'password' : qiita_config .admin_password ,
135
+ 'database' : qiita_config .database ,
136
+ 'host' : qiita_config .host ,
137
+ 'port' : qiita_config .port }
138
+
139
+ _admin_nodb_args = {
140
+ 'user' : qiita_config .admin_user ,
141
+ 'password' : qiita_config .admin_password ,
142
+ 'host' : qiita_config .host ,
143
+ 'port' : qiita_config .port }
144
+
145
+ _user_conn = None
146
+ _admin_conn = None
147
+ _admin_nodb_conn = None
148
+
149
+ _conn_map = {'no_admin' : '_user_conn' ,
150
+ 'admin_with_database' : '_admin_conn' ,
151
+ 'admin_without_database' : '_admin_nodb_conn' }
152
+
153
+ _args_map = {'no_admin' : '_user_args' ,
154
+ 'admin_with_database' : '_admin_args' ,
155
+ 'admin_without_database' : '_admin_nodb_args' }
156
+
125
157
"""Encapsulates the DB connection with the Postgres DB
126
158
127
159
Parameters
@@ -142,39 +174,25 @@ def __init__(self, admin='no_admin'):
142
174
"'admin_without_database'}" )
143
175
144
176
self .admin = admin
177
+
178
+ self ._conn_attr = self ._conn_map [self .admin ]
179
+ self ._args_attr = self ._args_map [self .admin ]
180
+ self ._conn_args = getattr (SQLConnectionHandler , self ._args_attr )
181
+ self ._connection = getattr (SQLConnectionHandler , self ._conn_attr )
145
182
self ._open_connection ()
183
+
146
184
# queues for transaction blocks. Format is {str: list} where the str
147
185
# is the queue name and the list is the queue of SQL commands
148
186
self .queues = {}
149
187
150
- def __del__ (self ):
151
- # make sure if connection close fails it doesn't raise error
152
- # should only error if connection already closed
153
- try :
154
- self ._connection .close ()
155
- except :
156
- pass
157
-
158
188
def _open_connection (self ):
159
- # connection string arguments for a normal user
160
- args = {
161
- 'user' : qiita_config .user ,
162
- 'password' : qiita_config .password ,
163
- 'database' : qiita_config .database ,
164
- 'host' : qiita_config .host ,
165
- 'port' : qiita_config .port }
166
-
167
- # if this is an admin user, use the admin credentials
168
- if self .admin != 'no_admin' :
169
- args ['user' ] = qiita_config .admin_user
170
- args ['password' ] = qiita_config .admin_password
171
-
172
- # Do not connect to a particular database unless requested
173
- if self .admin == 'admin_without_database' :
174
- del args ['database' ]
189
+ # if the connection has been created and is not closed
190
+ if self ._connection is not None and self ._connection .closed == 0 :
191
+ return
175
192
176
193
try :
177
- self ._connection = connect (** args )
194
+ setattr (SQLConnectionHandler , self ._conn_attr ,
195
+ connect (** self ._conn_args ))
178
196
except OperationalError as e :
179
197
# catch threee known common exceptions and raise runtime errors
180
198
try :
@@ -202,6 +220,19 @@ def _open_connection(self):
202
220
'\n \n \t %s\n %s For more information, review `INSTALL.md`'
203
221
' in the Qiita installation base directory.' )
204
222
raise RuntimeError (ebase % (e .message , etext ))
223
+ else :
224
+ self ._connection = getattr (SQLConnectionHandler , self ._conn_attr )
225
+
226
+ @staticmethod
227
+ def close ():
228
+ if SQLConnectionHandler ._user_conn is not None :
229
+ SQLConnectionHandler ._user_conn .close ()
230
+
231
+ if SQLConnectionHandler ._admin_conn is not None :
232
+ SQLConnectionHandler ._admin_conn .close ()
233
+
234
+ if SQLConnectionHandler ._admin_nodb_conn is not None :
235
+ SQLConnectionHandler ._admin_nodb_conn .close ()
205
236
206
237
@contextmanager
207
238
def get_postgres_cursor (self ):
@@ -213,9 +244,7 @@ def get_postgres_cursor(self):
213
244
214
245
Raises a QiitaDBConnectionError if the cursor cannot be created
215
246
"""
216
- if self ._connection .closed :
217
- # Currently defaults to non-admin connection
218
- self ._open_connection ()
247
+ self ._open_connection ()
219
248
220
249
try :
221
250
with self ._connection .cursor (cursor_factory = DictCursor ) as cur :
0 commit comments