From 0fdad0d9d9e68b00f61171bb2a0dfd840ef5345f Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Fri, 19 Jul 2013 14:50:37 -0700 Subject: [PATCH] Close SQLite cursors when creating functions. If the cursors are not closed, then when create_function is called, if they are not GC'd then create_function will fail. On Pythons without reference counting (e.g. PyPy) they will not be GC'd immediately. Change-Id: I39210616d323691ccb745149f24430a7a61382ec --- swift/common/db.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/swift/common/db.py b/swift/common/db.py index de140bdf1c..5a34c154a9 100644 --- a/swift/common/db.py +++ b/swift/common/db.py @@ -16,7 +16,7 @@ """ Database code for Swift """ from __future__ import with_statement -from contextlib import contextmanager +from contextlib import contextmanager, closing import hashlib import logging import os @@ -155,10 +155,11 @@ def get_db_connection(path, timeout=30, okay_to_create=False): 'DB file created by connect?') conn.row_factory = sqlite3.Row conn.text_factory = str - conn.execute('PRAGMA synchronous = NORMAL') - conn.execute('PRAGMA count_changes = OFF') - conn.execute('PRAGMA temp_store = MEMORY') - conn.execute('PRAGMA journal_mode = DELETE') + with closing(conn.cursor()) as cur: + cur.execute('PRAGMA synchronous = NORMAL') + cur.execute('PRAGMA count_changes = OFF') + cur.execute('PRAGMA temp_store = MEMORY') + cur.execute('PRAGMA journal_mode = DELETE') conn.create_function('chexor', 3, chexor) except sqlite3.DatabaseError: import traceback @@ -203,9 +204,10 @@ class DatabaseBroker(object): factory=GreenDBConnection, timeout=0) # creating dbs implicitly does a lot of transactions, so we # pick fast, unsafe options here and do a big fsync at the end. - conn.execute('PRAGMA synchronous = OFF') - conn.execute('PRAGMA temp_store = MEMORY') - conn.execute('PRAGMA journal_mode = MEMORY') + with closing(conn.cursor()) as cur: + cur.execute('PRAGMA synchronous = OFF') + cur.execute('PRAGMA temp_store = MEMORY') + cur.execute('PRAGMA journal_mode = MEMORY') conn.create_function('chexor', 3, chexor) conn.row_factory = sqlite3.Row conn.text_factory = str