summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPavel Zuna <pzuna@redhat.com>2010-01-05 13:38:20 +0100
committerRob Crittenden <rcritten@redhat.com>2010-01-11 13:51:05 -0500
commit314fe71787901225929bf18c20bd0376310b8ca1 (patch)
treec8d0064ab82035a6247eb2b2c932b94951bf11a5
parent49fb5ad493e262667e91c004f1276b2b81b90cf4 (diff)
downloadfreeipa-314fe71787901225929bf18c20bd0376310b8ca1.tar.gz
freeipa-314fe71787901225929bf18c20bd0376310b8ca1.tar.xz
freeipa-314fe71787901225929bf18c20bd0376310b8ca1.zip
Allow creation of new connections by unshared instances of backend.Connectible.
-rw-r--r--ipalib/backend.py34
-rw-r--r--ipalib/request.py2
2 files changed, 22 insertions, 14 deletions
diff --git a/ipalib/backend.py b/ipalib/backend.py
index 2436ce052..8aa057802 100644
--- a/ipalib/backend.py
+++ b/ipalib/backend.py
@@ -44,52 +44,60 @@ class Connectible(Backend):
`request.destroy_context()` can properly close all open connections.
"""
+ def __init__(self, shared_instance=True):
+ Backend.__init__(self)
+ if shared_instance:
+ self.id = self.name
+ else:
+ self.id = '%s_%s' % (self.name, str(id(self)))
+
def connect(self, *args, **kw):
"""
Create thread-local connection.
"""
- if hasattr(context, self.name):
+ if hasattr(context, self.id):
raise StandardError(
"connect: 'context.%s' already exists in thread %r" % (
- self.name, threading.currentThread().getName()
+ self.id, threading.currentThread().getName()
)
)
conn = self.create_connection(*args, **kw)
- setattr(context, self.name, Connection(conn, self.disconnect))
+ setattr(context, self.id, Connection(conn, self.disconnect))
assert self.conn is conn
- self.info('Created connection context.%s' % self.name)
+ self.info('Created connection context.%s' % self.id)
def create_connection(self, *args, **kw):
- raise NotImplementedError('%s.create_connection()' % self.name)
+ raise NotImplementedError('%s.create_connection()' % self.id)
def disconnect(self):
- if not hasattr(context, self.name):
+ if not hasattr(context, self.id):
raise StandardError(
"disconnect: 'context.%s' does not exist in thread %r" % (
- self.name, threading.currentThread().getName()
+ self.id, threading.currentThread().getName()
)
)
self.destroy_connection()
- self.info('Destroyed connection context.%s' % self.name)
+ delattr(context, self.id)
+ self.info('Destroyed connection context.%s' % self.id)
def destroy_connection(self):
- raise NotImplementedError('%s.destroy_connection()' % self.name)
+ raise NotImplementedError('%s.destroy_connection()' % self.id)
def isconnected(self):
"""
Return ``True`` if thread-local connection on `request.context` exists.
"""
- return hasattr(context, self.name)
+ return hasattr(context, self.id)
def __get_conn(self):
"""
Return thread-local connection.
"""
- if not hasattr(context, self.name):
+ if not hasattr(context, self.id):
raise AttributeError('no context.%s in thread %r' % (
- self.name, threading.currentThread().getName())
+ self.id, threading.currentThread().getName())
)
- return getattr(context, self.name).conn
+ return getattr(context, self.id).conn
conn = property(__get_conn)
diff --git a/ipalib/request.py b/ipalib/request.py
index d2c39c911..f21ac03c7 100644
--- a/ipalib/request.py
+++ b/ipalib/request.py
@@ -52,10 +52,10 @@ def destroy_context():
"""
Delete all attributes on thread-local `request.context`.
"""
+ # need to use .items(), 'cos value.disconnect modifies the dict
for (name, value) in context.__dict__.items():
if isinstance(value, Connection):
value.disconnect()
- delattr(context, name)
def ugettext(message):