class DBConnectionPool:
    def __init__(self, configs):
        try:
            self.pool = PooledDB(psycopg2, maxconnections = configs.max_connection_num, mincached = configs.mincached_num, maxcached = configs.maxcached_num, \
                             maxshared = configs.maxshared_num, application_name = configs.application_name_for_connections,
                             host = configs.database_host,port=configs.database_port, dbname = configs.database_name, user = configs.database_user_name, password=configs.database_password)
        except Exception as e:
            logger.error(
                "Fatal Error: failed to initialize DBConnectionPool, system is exiting!"
            )
            logger.error(e)
            sys.exit(901)

    def get_connection(self):
        try:
            return self.pool.connection()
        except Exception as e:
            logger.error("Error: get connection from pool error!")
            logger.error(e)
        return None

    def get_dedicated_connection(self):
        try:
            return self.pool.dedicated_connection()
        except Exception as e:
            logger.error("Error: get dedicated connection from pool error!")
            logger.error(e)
        return None
class DBUtilPooledDBDataSource(AbstractDataSource):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        from DBUtils.PooledDB import PooledDB
        self._conn_params['use_pure'] = True
        # self._pool = PooledDB(connector,maxcached=8,maxconnections=50,**{'user':self._user_name,'password':self._pass_word,'host':self._host,'port':self._port,'database':self._data_base,'use_pure':True})
        self._pool = PooledDB(connector,
                              maxcached=8,
                              maxconnections=50,
                              **self._conn_params)

    def get_conn(self):
        return self._pool.dedicated_connection()
Example #3
0
class DBUtilPooledDBDataSource(AbstractDataSource):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        from DBUtils.PooledDB import PooledDB
        # self._conn_params['use_pure'] = True
        self._pool = PooledDB(connector,
                              maxcached=8,
                              maxconnections=50,
                              **self._conn_params)
        self._closed = False

    def _inner_close(self):
        self._pool.close()

    def get_conn(self):
        return self._pool.dedicated_connection()

    def release_conn(self, conn):
        conn.close()
Example #4
0
	def test03_CreateConnection(self):
		for threadsafety in (1, 2):
			dbapi.threadsafety = threadsafety
			shareable = threadsafety > 1
			pool = PooledDB(dbapi, 1, 1, 1, 0, False, None, None, None,
				'PooledDBTestDB', user='******')
			self.assert_(hasattr(pool, '_idle_cache'))
			self.assertEqual(len(pool._idle_cache), 1)
			if shareable:
				self.assert_(hasattr(pool, '_shared_cache'))
				self.assertEqual(len(pool._shared_cache), 0)
			else:
				self.assert_(not hasattr(pool, '_shared_cache'))
			self.assert_(hasattr(pool, '_maxusage'))
			self.assertEqual(pool._maxusage, None)
			self.assert_(hasattr(pool, '_setsession'))
			self.assert_(pool._setsession is None)
			con = pool._idle_cache[0]
			from DBUtils.SteadyDB import SteadyDBConnection
			self.assert_(isinstance(con, SteadyDBConnection))
			self.assert_(hasattr(con, '_maxusage'))
			self.assertEqual(con._maxusage, 0)
			self.assert_(hasattr(con, '_setsession_sql'))
			self.assert_(con._setsession_sql is None)
			db = pool.connection()
			self.assertEqual(len(pool._idle_cache), 0)
			if shareable:
				self.assertEqual(len(pool._shared_cache), 1)
			self.assert_(hasattr(db, '_con'))
			self.assertEqual(db._con, con)
			self.assert_(hasattr(db, 'cursor'))
			self.assert_(hasattr(db, '_usage'))
			self.assertEqual(db._usage, 0)
			self.assert_(hasattr(con, '_con'))
			db_con = con._con
			self.assert_(hasattr(db_con, 'database'))
			self.assertEqual(db_con.database, 'PooledDBTestDB')
			self.assert_(hasattr(db_con, 'user'))
			self.assertEqual(db_con.user, 'PooledDBTestUser')
			self.assert_(hasattr(db_con, 'open_cursors'))
			self.assertEqual(db_con.open_cursors, 0)
			self.assert_(hasattr(db_con, 'num_uses'))
			self.assertEqual(db_con.num_uses, 0)
			self.assert_(hasattr(db_con, 'num_queries'))
			self.assertEqual(db_con.num_queries, 0)
			cursor = db.cursor()
			self.assertEqual(db_con.open_cursors, 1)
			cursor.execute('select test')
			r = cursor.fetchone()
			cursor.close()
			self.assertEqual(db_con.open_cursors, 0)
			self.assertEqual(r, 'test')
			self.assertEqual(db_con.num_queries, 1)
			self.assertEqual(db._usage, 1)
			cursor = db.cursor()
			self.assertEqual(db_con.open_cursors, 1)
			cursor.execute('set sessiontest')
			cursor2 = db.cursor()
			self.assertEqual(db_con.open_cursors, 2)
			cursor2.close()
			self.assertEqual(db_con.open_cursors, 1)
			cursor.close()
			self.assertEqual(db_con.open_cursors, 0)
			self.assertEqual(db_con.num_queries, 1)
			self.assertEqual(db._usage, 2)
			self.assertEqual(db_con.session,
				['rollback', 'sessiontest'])
			pool = PooledDB(dbapi, 1, 1, 1)
			self.assertEqual(len(pool._idle_cache), 1)
			if shareable:
				self.assertEqual(len(pool._shared_cache), 0)
			db = pool.connection()
			self.assertEqual(len(pool._idle_cache), 0)
			if shareable:
				self.assertEqual(len(pool._shared_cache), 1)
			db.close()
			self.assertEqual(len(pool._idle_cache), 1)
			if shareable:
				self.assertEqual(len(pool._shared_cache), 0)
			db = pool.connection(True)
			self.assertEqual(len(pool._idle_cache), 0)
			if shareable:
				self.assertEqual(len(pool._shared_cache), 1)
			db.close()
			self.assertEqual(len(pool._idle_cache), 1)
			if shareable:
				self.assertEqual(len(pool._shared_cache), 0)
			db = pool.connection(False)
			self.assertEqual(len(pool._idle_cache), 0)
			if shareable:
				self.assertEqual(len(pool._shared_cache), 0)
			self.assertEqual(db._usage, 0)
			db_con = db._con._con
			self.assert_(db_con.database is None)
			self.assert_(db_con.user is None)
			db.close()
			self.assertEqual(len(pool._idle_cache), 1)
			if shareable:
				self.assertEqual(len(pool._shared_cache), 0)
			db = pool.dedicated_connection()
			self.assertEqual(len(pool._idle_cache), 0)
			if shareable:
				self.assertEqual(len(pool._shared_cache), 0)
			self.assertEqual(db._usage, 0)
			db_con = db._con._con
			self.assert_(db_con.database is None)
			self.assert_(db_con.user is None)
			db.close()
			self.assertEqual(len(pool._idle_cache), 1)
			if shareable:
				self.assertEqual(len(pool._shared_cache), 0)
			pool = PooledDB(dbapi, 0, 0, 0, 0, False, 3, ('set datestyle',))
			self.assertEqual(pool._maxusage, 3)
			self.assertEqual(pool._setsession, ('set datestyle',))
			con = pool.connection()._con
			self.assertEqual(con._maxusage, 3)
			self.assertEqual(con._setsession_sql, ('set datestyle',))
Example #5
0
 def get_conn(self):
     pool = PooledDB(mysql.connector,maxcached=8,maxconnections=50,**{'user':self._user_name,'password':self._pass_word,'host':self._host,'port':self._port,'database':self._data_base})
     return pool.dedicated_connection()
Example #6
0
 def test03_CreateConnection(self):
     for threadsafety in (1, 2):
         dbapi.threadsafety = threadsafety
         shareable = threadsafety > 1
         pool = PooledDB(dbapi,
                         1,
                         1,
                         1,
                         0,
                         False,
                         None,
                         None,
                         None,
                         'PooledDBTestDB',
                         user='******')
         self.assert_(hasattr(pool, '_idle_cache'))
         self.assertEqual(len(pool._idle_cache), 1)
         if shareable:
             self.assert_(hasattr(pool, '_shared_cache'))
             self.assertEqual(len(pool._shared_cache), 0)
         else:
             self.assert_(not hasattr(pool, '_shared_cache'))
         self.assert_(hasattr(pool, '_maxusage'))
         self.assertEqual(pool._maxusage, None)
         self.assert_(hasattr(pool, '_setsession'))
         self.assert_(pool._setsession is None)
         con = pool._idle_cache[0]
         from DBUtils.SteadyDB import SteadyDBConnection
         self.assert_(isinstance(con, SteadyDBConnection))
         self.assert_(hasattr(con, '_maxusage'))
         self.assertEqual(con._maxusage, 0)
         self.assert_(hasattr(con, '_setsession_sql'))
         self.assert_(con._setsession_sql is None)
         db = pool.connection()
         self.assertEqual(len(pool._idle_cache), 0)
         if shareable:
             self.assertEqual(len(pool._shared_cache), 1)
         self.assert_(hasattr(db, '_con'))
         self.assertEqual(db._con, con)
         self.assert_(hasattr(db, 'cursor'))
         self.assert_(hasattr(db, '_usage'))
         self.assertEqual(db._usage, 0)
         self.assert_(hasattr(con, '_con'))
         db_con = con._con
         self.assert_(hasattr(db_con, 'database'))
         self.assertEqual(db_con.database, 'PooledDBTestDB')
         self.assert_(hasattr(db_con, 'user'))
         self.assertEqual(db_con.user, 'PooledDBTestUser')
         self.assert_(hasattr(db_con, 'open_cursors'))
         self.assertEqual(db_con.open_cursors, 0)
         self.assert_(hasattr(db_con, 'num_uses'))
         self.assertEqual(db_con.num_uses, 0)
         self.assert_(hasattr(db_con, 'num_queries'))
         self.assertEqual(db_con.num_queries, 0)
         cursor = db.cursor()
         self.assertEqual(db_con.open_cursors, 1)
         cursor.execute('select test')
         r = cursor.fetchone()
         cursor.close()
         self.assertEqual(db_con.open_cursors, 0)
         self.assertEqual(r, 'test')
         self.assertEqual(db_con.num_queries, 1)
         self.assertEqual(db._usage, 1)
         cursor = db.cursor()
         self.assertEqual(db_con.open_cursors, 1)
         cursor.execute('set sessiontest')
         cursor2 = db.cursor()
         self.assertEqual(db_con.open_cursors, 2)
         cursor2.close()
         self.assertEqual(db_con.open_cursors, 1)
         cursor.close()
         self.assertEqual(db_con.open_cursors, 0)
         self.assertEqual(db_con.num_queries, 1)
         self.assertEqual(db._usage, 2)
         self.assertEqual(db_con.session, ['rollback', 'sessiontest'])
         pool = PooledDB(dbapi, 1, 1, 1)
         self.assertEqual(len(pool._idle_cache), 1)
         if shareable:
             self.assertEqual(len(pool._shared_cache), 0)
         db = pool.connection()
         self.assertEqual(len(pool._idle_cache), 0)
         if shareable:
             self.assertEqual(len(pool._shared_cache), 1)
         db.close()
         self.assertEqual(len(pool._idle_cache), 1)
         if shareable:
             self.assertEqual(len(pool._shared_cache), 0)
         db = pool.connection(True)
         self.assertEqual(len(pool._idle_cache), 0)
         if shareable:
             self.assertEqual(len(pool._shared_cache), 1)
         db.close()
         self.assertEqual(len(pool._idle_cache), 1)
         if shareable:
             self.assertEqual(len(pool._shared_cache), 0)
         db = pool.connection(False)
         self.assertEqual(len(pool._idle_cache), 0)
         if shareable:
             self.assertEqual(len(pool._shared_cache), 0)
         self.assertEqual(db._usage, 0)
         db_con = db._con._con
         self.assert_(db_con.database is None)
         self.assert_(db_con.user is None)
         db.close()
         self.assertEqual(len(pool._idle_cache), 1)
         if shareable:
             self.assertEqual(len(pool._shared_cache), 0)
         db = pool.dedicated_connection()
         self.assertEqual(len(pool._idle_cache), 0)
         if shareable:
             self.assertEqual(len(pool._shared_cache), 0)
         self.assertEqual(db._usage, 0)
         db_con = db._con._con
         self.assert_(db_con.database is None)
         self.assert_(db_con.user is None)
         db.close()
         self.assertEqual(len(pool._idle_cache), 1)
         if shareable:
             self.assertEqual(len(pool._shared_cache), 0)
         pool = PooledDB(dbapi, 0, 0, 0, 0, False, 3, ('set datestyle', ))
         self.assertEqual(pool._maxusage, 3)
         self.assertEqual(pool._setsession, ('set datestyle', ))
         con = pool.connection()._con
         self.assertEqual(con._maxusage, 3)
         self.assertEqual(con._setsession_sql, ('set datestyle', ))