00001 """\
00002 @file mysql_pool.py
00003 @brief Thin wrapper around eventlet.db_pool that chooses MySQLdb and Tpool.
00004
00005 $LicenseInfo:firstyear=2007&license=mit$
00006
00007 Copyright (c) 2007-2008, Linden Research, Inc.
00008
00009 Permission is hereby granted, free of charge, to any person obtaining a copy
00010 of this software and associated documentation files (the "Software"), to deal
00011 in the Software without restriction, including without limitation the rights
00012 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00013 copies of the Software, and to permit persons to whom the Software is
00014 furnished to do so, subject to the following conditions:
00015
00016 The above copyright notice and this permission notice shall be included in
00017 all copies or substantial portions of the Software.
00018
00019 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00020 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00021 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00022 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00023 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00024 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00025 THE SOFTWARE.
00026 $/LicenseInfo$
00027 """
00028
00029 import MySQLdb
00030 from eventlet import db_pool
00031
00032 class DatabaseConnector(db_pool.DatabaseConnector):
00033 def __init__(self, credentials, min_size = 0, max_size = 4, *args, **kwargs):
00034 super(DatabaseConnector, self).__init__(MySQLdb, credentials, min_size, max_size, conn_pool=db_pool.ConnectionPool, *args, **kwargs)
00035
00036
00037 def get(self, host, dbname, port=3306):
00038 key = (host, dbname, port)
00039 if key not in self._databases:
00040 new_kwargs = self._kwargs.copy()
00041 new_kwargs['db'] = dbname
00042 new_kwargs['host'] = host
00043 new_kwargs['port'] = port
00044 new_kwargs.update(self.credentials_for(host))
00045 dbpool = ConnectionPool(self._min_size, self._max_size, *self._args, **new_kwargs)
00046 self._databases[key] = dbpool
00047
00048 return self._databases[key]
00049
00050 class ConnectionPool(db_pool.TpooledConnectionPool):
00051 """A pool which gives out saranwrapped MySQLdb connections from a pool
00052 """
00053
00054 def __init__(self, min_size = 0, max_size = 4, *args, **kwargs):
00055 super(ConnectionPool, self).__init__(MySQLdb, min_size, max_size, *args, **kwargs)
00056
00057 def get(self):
00058 conn = super(ConnectionPool, self).get()
00059
00060
00061
00062
00063 arg_names = ['host','user','passwd','db','port','unix_socket','conv','connect_timeout',
00064 'compress', 'named_pipe', 'init_command', 'read_default_file', 'read_default_group',
00065 'cursorclass', 'use_unicode', 'charset', 'sql_mode', 'client_flag', 'ssl',
00066 'local_infile']
00067
00068
00069
00070
00071
00072
00073
00074
00075 converted_kwargs = dict([ (arg_names[i], arg) for i, arg in enumerate(self._args) ])
00076 converted_kwargs.update(self._kwargs)
00077 conn.connection_parameters = converted_kwargs
00078 return conn