~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/canonical/database/sqlbase.py

  • Committer: Stuart Bishop
  • Date: 2011-09-28 12:49:24 UTC
  • mfrom: (9893.10.1 trivial)
  • mto: This revision was merged to the branch mainline in revision 14178.
  • Revision ID: stuart.bishop@canonical.com-20110928124924-m5a22fymqghw6c5i
Merged trivial into distinct-db-users.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright 2009 Canonical Ltd.  This software is licensed under the
 
1
# Copyright 2009-2011 Canonical Ltd.  This software is licensed under the
2
2
# GNU Affero General Public License version 3 (see the file LICENSE).
3
3
 
4
4
__metaclass__ = type
25
25
    'SQLBase',
26
26
    'sqlvalues',
27
27
    'StupidCache',
28
 
    'ZopelessTransactionManager',
29
28
    ]
30
29
 
31
30
 
262
261
        clear_property_cache(self)
263
262
 
264
263
 
265
 
class ZopelessTransactionManager(object):
266
 
    """Compatibility shim for initZopeless()"""
267
 
 
268
 
    _installed = None
269
 
 
270
 
    def __init__(self):
271
 
        raise AssertionError("ZopelessTransactionManager should not be "
272
 
                             "directly instantiated.")
273
 
 
274
 
    @classmethod
275
 
    def initZopeless(cls, dbuser=None, isolation=ISOLATION_LEVEL_DEFAULT):
276
 
        if dbuser is None:
277
 
            raise AssertionError(
278
 
                "dbuser is now required. All scripts must connect as unique "
279
 
                "database users.")
280
 
 
281
 
        isolation_level = {
282
 
            ISOLATION_LEVEL_AUTOCOMMIT: 'autocommit',
283
 
            ISOLATION_LEVEL_READ_COMMITTED: 'read_committed',
284
 
            ISOLATION_LEVEL_SERIALIZABLE: 'serializable'}[isolation]
285
 
 
286
 
        dbconfig.override(dbuser=dbuser, isolation_level=isolation_level)
287
 
 
288
 
        cls._dbuser = dbuser
289
 
        cls._isolation = isolation
290
 
        cls._reset_stores()
291
 
        cls._installed = cls
292
 
 
293
 
    @staticmethod
294
 
    def _reset_stores():
295
 
        """Reset the active stores.
296
 
 
297
 
        This is required for connection setting changes to be made visible.
298
 
        """
299
 
        for name, store in getUtility(IZStorm).iterstores():
300
 
            connection = store._connection
301
 
            if connection._state == storm.database.STATE_CONNECTED:
302
 
                if connection._raw_connection is not None:
303
 
                    connection._raw_connection.close()
304
 
 
305
 
                # This method assumes that calling transaction.abort() will
306
 
                # call rollback() on the store, but this is no longer the
307
 
                # case as of jamesh's fix for bug 230977; Stores are not
308
 
                # registered with the transaction manager until they are
309
 
                # used. While storm doesn't provide an API which does what
310
 
                # we want, we'll go under the covers and emit the
311
 
                # register-transaction event ourselves. This method is
312
 
                # only called by the test suite to kill the existing
313
 
                # connections so the Store's reconnect with updated
314
 
                # connection settings.
315
 
                store._event.emit('register-transaction')
316
 
 
317
 
                connection._raw_connection = None
318
 
                connection._state = storm.database.STATE_DISCONNECTED
319
 
        transaction.abort()
320
 
 
321
 
    @classmethod
322
 
    def uninstall(cls):
323
 
        """Uninstall the ZopelessTransactionManager.
324
 
 
325
 
        This entails removing the config overlay and resetting the store.
326
 
        """
327
 
        assert cls._installed is not None, (
328
 
            "ZopelessTransactionManager not installed")
329
 
        dbconfig.override(dbuser=None, isolation_level=None)
330
 
        cls._reset_stores()
331
 
        cls._installed = None
332
 
 
333
 
 
334
264
def clear_current_connection_cache():
335
265
    """Clear SQLObject's object cache. SQLObject compatibility - DEPRECATED.
336
266
    """