diff options
author | Aaron Brashears <aaronb@lindenlab.com> | 2007-09-10 20:10:56 +0000 |
---|---|---|
committer | Aaron Brashears <aaronb@lindenlab.com> | 2007-09-10 20:10:56 +0000 |
commit | 80dfa222fdc3747be9f5b64b9ace35907edf1c4e (patch) | |
tree | 6155c692ce1d625ea495c74cd68b1284712241f5 /indra/lib/python/indra/ipc | |
parent | 0bd992b07cf17ac0e327cb95d6207883d88a60a3 (diff) |
Result of svn merge -r69150:69158 svn+ssh://svn/svn/linden/branches/named-queries-py3 into release.
Diffstat (limited to 'indra/lib/python/indra/ipc')
-rw-r--r-- | indra/lib/python/indra/ipc/mysql_pool.py | 51 | ||||
-rw-r--r-- | indra/lib/python/indra/ipc/russ.py | 4 | ||||
-rw-r--r-- | indra/lib/python/indra/ipc/servicebuilder.py | 8 |
3 files changed, 58 insertions, 5 deletions
diff --git a/indra/lib/python/indra/ipc/mysql_pool.py b/indra/lib/python/indra/ipc/mysql_pool.py index 2e0c40f850..64965c83d4 100644 --- a/indra/lib/python/indra/ipc/mysql_pool.py +++ b/indra/lib/python/indra/ipc/mysql_pool.py @@ -14,6 +14,49 @@ from indra.ipc import saranwrap import MySQLdb +# method 2: better -- admits the existence of the pool +# dbp = my_db_connector.get() +# dbh = dbp.get() +# dbc = dbh.cursor() +# dbc.execute(named_query) +# dbc.close() +# dbp.put(dbh) + +class DatabaseConnector(object): + """\ +@brief This is an object which will maintain a collection of database +connection pools keyed on host,databasename""" + def __init__(self, credentials, min_size = 0, max_size = 4, *args, **kwargs): + """\ + @brief constructor + @param min_size the minimum size of a child pool. + @param max_size the maximum size of a child pool.""" + self._min_size = min_size + self._max_size = max_size + self._args = args + self._kwargs = kwargs + self._credentials = credentials # this is a map of hostname to username/password + self._databases = {} + + def credentials_for(self, host): + if host in self._credentials: + return self._credentials[host] + else: + return self._credentials.get('default', None) + + def get(self, host, dbname): + key = (host, dbname) + if key not in self._databases: + new_kwargs = self._kwargs.copy() + new_kwargs['db'] = dbname + new_kwargs['host'] = host + new_kwargs.update(self.credentials_for(host)) + dbpool = ConnectionPool(self._min_size, self._max_size, *self._args, **new_kwargs) + self._databases[key] = dbpool + + return self._databases[key] + + class ConnectionPool(Pool): """A pool which gives out saranwrapped MySQLdb connections from a pool """ @@ -26,12 +69,14 @@ class ConnectionPool(Pool): return saranwrap.wrap(MySQLdb).connect(*self._args, **self._kwargs) def put(self, conn): - # poke the process to see if it's dead or None + # rollback any uncommitted changes, so that the next process + # has a clean slate. This also pokes the process to see if + # it's dead or None try: - status = saranwrap.status(conn) + conn.rollback() except (AttributeError, DeadProcess), e: conn = self.create() - + # TODO figure out if we're still connected to the database if conn: Pool.put(self, conn) else: diff --git a/indra/lib/python/indra/ipc/russ.py b/indra/lib/python/indra/ipc/russ.py index a9ebd8bb03..437061538b 100644 --- a/indra/lib/python/indra/ipc/russ.py +++ b/indra/lib/python/indra/ipc/russ.py @@ -61,7 +61,7 @@ def format(format_str, context): end = format_str.index('}', pos) #print "directive:", format_str[pos+1:pos+5] if format_str[pos + 1] == '$': - value = context.get(format_str[pos + 2:end]) + value = context[format_str[pos + 2:end]] if value is not None: value = format_value_for_path(value) elif format_str[pos + 1] == '%': @@ -71,7 +71,7 @@ def format(format_str, context): value = _fetch_url_directive(format_str[pos + 1:end]) else: raise UnknownDirective, format_str[pos:end + 1] - if not value == None: + if value is not None: format_str = format_str[:pos]+str(value)+format_str[end+1:] substitutions += 1 diff --git a/indra/lib/python/indra/ipc/servicebuilder.py b/indra/lib/python/indra/ipc/servicebuilder.py index d422b2ed42..c2490593c3 100644 --- a/indra/lib/python/indra/ipc/servicebuilder.py +++ b/indra/lib/python/indra/ipc/servicebuilder.py @@ -17,6 +17,14 @@ try: except: pass +# convenience method for when you can't be arsed to make your own object (i.e. all the time) +_g_builder = None +def build(name, context): + global _g_builder + if _g_builder is None: + _g_builder = ServiceBuilder() + _g_builder.buildServiceURL(name, context) + class ServiceBuilder(object): def __init__(self, services_definition = services_config): """\ |