~wgrant/ubuntu/natty/landscape-client/natty-updates-broken

« back to all changes in this revision

Viewing changes to landscape/lib/store.py

  • Committer: Bazaar Package Importer
  • Author(s): Free Ekanayaka
  • Date: 2010-04-21 19:58:10 UTC
  • mfrom: (1.1.16 upstream)
  • Revision ID: james.westby@ubuntu.com-20100421195810-s30uv3s6i27lue38
Tags: 1.5.2-0ubuntu0.10.10.0
* New upstream version (LP: #594594):
  - A new includes information about active network devices and their
    IP address in sysinfo output (LP: #272344).
  - A new plugin collects information about network traffic (#LP :284662).
  - Report information about which packages requested a reboot (LP: #538253).
  - Fix breakage on Lucid AMIs having no ramdisk (LP: #574810).
  - Migrate the inter-process communication system from DBus to Twisted AMP.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""Functions used by all sqlite-backed stores."""
 
2
 
 
3
try:
 
4
    import sqlite3
 
5
except ImportError:
 
6
    from pysqlite2 import dbapi2 as sqlite3
 
7
 
 
8
 
 
9
def with_cursor(method):
 
10
    """Decorator that encloses the method in a database transaction.
 
11
 
 
12
    Even though SQLite is supposed to be useful in autocommit mode, we've
 
13
    found cases where the database continued to be locked for writing
 
14
    until the cursor was closed.  With this in mind, instead of using
 
15
    the autocommit mode, we explicitly terminate transactions and enforce
 
16
    cursor closing with this decorator.
 
17
    """
 
18
 
 
19
    def inner(self, *args, **kwargs):
 
20
        if not self._db:
 
21
            # Create the database connection only when we start to actually
 
22
            # use it. This is essentially just a workaroud of a sqlite bug
 
23
            # happening when 2 concurrent processes try to create the tables
 
24
            # around the same time, the one which fails having an incorrect
 
25
            # cache and not seeing the tables
 
26
            self._db = sqlite3.connect(self._filename)
 
27
            self._ensure_schema()
 
28
        try:
 
29
            cursor = self._db.cursor()
 
30
            try:
 
31
                result = method(self, cursor, *args, **kwargs)
 
32
            finally:
 
33
                cursor.close()
 
34
            self._db.commit()
 
35
        except:
 
36
            self._db.rollback()
 
37
            raise
 
38
        return result
 
39
    return inner