Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'value,': 0.04; 'context': 0.07; 'none:': 0.07; '22,': 0.09; 'appropriate.': 0.09; 'bits': 0.09; 'subject:method': 0.09; 'try:': 0.09; 'type,': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'accepts': 0.16; 'exit.': 0.16; 'parameter:': 0.16; 'sqlite': 0.16; 'suggested,': 0.16; 'thread,': 0.16; 'wrote:': 0.18; 'feb': 0.22; 'cc:addr:python.org': 0.22; 'skip': 0.24; 'sorry,': 0.24; "haven't": 0.24; 'cc:2**0': 0.24; 'define': 0.26; 'header:In- Reply-To:1': 0.27; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; 'class': 0.32; 'skip:m 30': 0.32; 'skip:_ 10': 0.34; 'subject:the': 0.34; 'could': 0.34; 'subject:with': 0.35; "can't": 0.35; 'connection': 0.35; 'except': 0.35; 'received:google.com': 0.35; 'manager': 0.38; 'pm,': 0.38; 'to:addr:gmail.com': 0.65; 'here': 0.66; 'close': 0.67; 'delegates': 0.74; 'attention': 0.75; '2015': 0.84; 'subject:try': 0.84; 'careful': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=csQv2EQuNwfEQ6xSAWS7H1lm6hpi7UTyfAeZOzEbgi8=; b=aruWOiLPrzQLA3q217IhMhseuSM1xHn5q0YoiqD4APz6FyUyvO5nYjDQkfZr7t06Vt eECwRklkxkz7/kjRfdY8h1PdNDHCS+KdGxdv4cD1esGUdw2ijIoCDnqnZefsLNRM7lPp uDhhC+tfn/HxvO9Ncr2plor9M9TckZ8CuL7MAsUJoatt0Qlrlgi32HF1YsiA/yTv7ncB EoFYhnKSO2NzcsF0ybomqjSU5NiYYiwm2497SljXPQzp+cgI9eBahH6bphFdHm66+9F6 RTit9hEyAuSZH60FYDWnWFwEVFCja2zUvPO31Qp0MjdztJRiSZLuNyMaXlf5R1VKbQpB EsAw== MIME-Version: 1.0 X-Received: by 10.60.133.203 with SMTP id pe11mr2061564oeb.26.1424632509267; Sun, 22 Feb 2015 11:15:09 -0800 (PST) In-Reply-To: References: <6trfeate2ppvm1mcapgr0g4g2fd3vceab6@4ax.com> Date: Sun, 22 Feb 2015 13:15:09 -0600 Subject: Re: try pattern for database connection with the close method From: Skip Montanaro To: Mario Figueiredo Content-Type: text/plain; charset=UTF-8 Cc: Python X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 37 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1424633976 news.xs4all.nl 2975 [2001:888:2000:d::a6]:52467 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:86150 On Sun, Feb 22, 2015 at 12:41 PM, Mario Figueiredo 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