comparison roundup/backends/back_sqlite.py @ 2374:31cb1014300c

Switch to using sqlite's own locking mechanisms... Automatically start a new transaction on hyperdb commit(). Better RDBMS demo.py nuke handling. (minor doc fixes)
author Richard Jones <richard@users.sourceforge.net>
date Fri, 28 May 2004 01:09:11 +0000
parents c77483d2cda4
children 091711fb2f8c
comparison
equal deleted inserted replaced
2372:c26bb78d2f0c 2374:31cb1014300c
1 # $Id: back_sqlite.py,v 1.28 2004-05-10 00:39:40 richard Exp $ 1 # $Id: back_sqlite.py,v 1.29 2004-05-28 01:09:11 richard Exp $
2 '''Implements a backend for SQLite. 2 '''Implements a backend for SQLite.
3 3
4 See https://pysqlite.sourceforge.net/ for pysqlite info 4 See https://pysqlite.sourceforge.net/ for pysqlite info
5 5
6 6
48 hyperdb.Number : rdbms_common._num_cvt, 48 hyperdb.Number : rdbms_common._num_cvt,
49 hyperdb.Multilink : lambda x: x, # used in journal marshalling 49 hyperdb.Multilink : lambda x: x, # used in journal marshalling
50 } 50 }
51 51
52 def sql_open_connection(self): 52 def sql_open_connection(self):
53 '''Open a standard, non-autocommitting connection.
54
55 pysqlite will automatically BEGIN TRANSACTION for us.
56 '''
53 db = os.path.join(self.config.DATABASE, 'db') 57 db = os.path.join(self.config.DATABASE, 'db')
54 conn = sqlite.connect(db=db) 58 conn = sqlite.connect(db=db)
59 # set a 30 second timeout (extraordinarily generous) for handling
60 # locked database
61 conn.db.sqlite_busy_timeout(30 * 1000)
55 cursor = conn.cursor() 62 cursor = conn.cursor()
56 return (conn, cursor) 63 return (conn, cursor)
57 64
58 def open_connection(self): 65 def open_connection(self):
59 # ensure files are group readable and writable 66 # ensure files are group readable and writable
60 os.umask(0002) 67 os.umask(0002)
61
62 # lock the database
63 lockfilenm = os.path.join(self.dir, 'lock')
64 self.lockfile = locking.acquire_lock(lockfilenm)
65 self.lockfile.write(str(os.getpid()))
66 self.lockfile.flush()
67 68
68 (self.conn, self.cursor) = self.sql_open_connection() 69 (self.conn, self.cursor) = self.sql_open_connection()
69 70
70 try: 71 try:
71 self.load_dbschema() 72 self.load_dbschema()
243 def sql_close(self): 244 def sql_close(self):
244 ''' Squash any error caused by us already having closed the 245 ''' Squash any error caused by us already having closed the
245 connection. 246 connection.
246 ''' 247 '''
247 try: 248 try:
248 try: 249 self.conn.close()
249 self.conn.close() 250 except sqlite.ProgrammingError, value:
250 except sqlite.ProgrammingError, value: 251 if str(value) != 'close failed - Connection is closed.':
251 if str(value) != 'close failed - Connection is closed.': 252 raise
252 raise
253 finally:
254 # always release the lock
255 if self.lockfile is not None:
256 locking.release_lock(self.lockfile)
257 self.lockfile.close()
258 self.lockfile = None
259 253
260 def sql_rollback(self): 254 def sql_rollback(self):
261 ''' Squash any error caused by us having closed the connection (and 255 ''' Squash any error caused by us having closed the connection (and
262 therefore not having anything to roll back) 256 therefore not having anything to roll back)
263 ''' 257 '''
278 try: 272 try:
279 self.conn.commit() 273 self.conn.commit()
280 except sqlite.DatabaseError, error: 274 except sqlite.DatabaseError, error:
281 if str(error) != 'cannot commit - no transaction is active': 275 if str(error) != 'cannot commit - no transaction is active':
282 raise 276 raise
277 # open a new cursor for subsequent work
278 self.cursor = self.conn.cursor()
283 279
284 def sql_index_exists(self, table_name, index_name): 280 def sql_index_exists(self, table_name, index_name):
285 self.cursor.execute('pragma index_list(%s)'%table_name) 281 self.cursor.execute('pragma index_list(%s)'%table_name)
286 for entry in self.cursor.fetchall(): 282 for entry in self.cursor.fetchall():
287 if entry[1] == index_name: 283 if entry[1] == index_name:

Roundup Issue Tracker: http://roundup-tracker.org/