Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #86150
| References | <6trfeate2ppvm1mcapgr0g4g2fd3vceab6@4ax.com> <mailman.18955.1424521415.18130.python-list@python.org> <mk8keahatht34gt0kq1ncmtaskdu2s3arc@4ax.com> |
|---|---|
| Date | 2015-02-22 13:15 -0600 |
| Subject | Re: try pattern for database connection with the close method |
| From | Skip Montanaro <skip.montanaro@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.19025.1424633976.18130.python-list@python.org> (permalink) |
On Sun, Feb 22, 2015 at 12:41 PM, Mario Figueiredo <marfig@gmail.com> wrote:
> The sqlite context manager doesn't close a database connection on
> exit. It only ensures, commits and rollbacks are performed.
Sorry, I haven't paid careful attention to this thread, so perhaps
this has already been suggested, however... Can't you write your own
class which delegates to the necessary sqlite3 bits and has a context
manager with the desired behavior? Thinking out loud, you could define
a ConnectionMgr class which accepts a sqlite3 connection as a
parameter:
class ConnectionMgr(object):
def __init__(self, conn):
self.conn = conn
def __enter__(self):
...
def __exit__(self, type, value, exception):
if self.conn is not None:
... close self.conn connection here ...
self.conn = None
def __getattr__(self, attr):
return getattr(self.conn, attr)
then...
try:
with MyConnection(lite.connect('data.db')) as db:
...
except lite.DatabaseError:
...
Might also have to __enter__ and __exit__ self.conn as appropriate.
Skip
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
try pattern for database connection with the close method Mario Figueiredo <marfig@gmail.com> - 2015-02-21 03:42 +0100
Re: try pattern for database connection with the close method Chris Kaynor <ckaynor@zindagigames.com> - 2015-02-20 18:59 -0800
Re: try pattern for database connection with the close method Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-02-21 12:22 +0000
Re: try pattern for database connection with the close method Mario Figueiredo <marfig@gmail.com> - 2015-02-22 19:41 +0100
Re: try pattern for database connection with the close method Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-02-22 19:07 +0000
Re: try pattern for database connection with the close method Mario Figueiredo <marfig@gmail.com> - 2015-02-23 00:25 +0100
Re: try pattern for database connection with the close method Skip Montanaro <skip.montanaro@gmail.com> - 2015-02-22 13:15 -0600
Re: try pattern for database connection with the close method Mario Figueiredo <marfig@gmail.com> - 2015-02-23 00:30 +0100
Re: try pattern for database connection with the close method Ian Kelly <ian.g.kelly@gmail.com> - 2015-02-21 08:16 -0700
Re: try pattern for database connection with the close method Peter Otten <__peter__@web.de> - 2015-02-21 16:22 +0100
Re: try pattern for database connection with the close method Mario Figueiredo <marfig@gmail.com> - 2015-02-23 00:35 +0100
Re: try pattern for database connection with the close method Peter Otten <__peter__@web.de> - 2015-02-21 16:27 +0100
Re: try pattern for database connection with the close method Ian Kelly <ian.g.kelly@gmail.com> - 2015-02-21 08:50 -0700
Re: try pattern for database connection with the close method Peter Otten <__peter__@web.de> - 2015-02-21 18:02 +0100
csiph-web