Esempio n. 1
0
 def test_initial(self, opts_with_conn_pool_strategy: MySQLOperator):
     _all_conn_pools = database_connection_pools()
     assert _all_conn_pools != {}, "The database connection pools should not be empty."
     assert Test_Pool_Name in _all_conn_pools.keys(
     ), "The pool name should be in the database connection pools."
     assert _all_conn_pools[
         Test_Pool_Name] is not None, "The database connection pool should exist with the pool name (from database_connection_pools)."
     assert get_connection_pool(
         pool_name=Test_Pool_Name
     ) is not None, "The database connection pool should exist with the pool name (from get_connection_pool)."
Esempio n. 2
0
 def get_one_connection(self,
                        pool_name: str = "",
                        **kwargs) -> PooledMySQLConnection:
     while True:
         try:
             # return self.database_connection_pool.get_connection()
             __connection = get_connection_pool(
                 pool_name=pool_name).get_connection()
             return __connection
         except PoolError as e:
             time.sleep(5)
         except AttributeError as ae:
             raise ConnectionError(
                 f"Cannot get the one connection instance from connection pool because it doesn't exist the connection pool with the name '{pool_name}'."
             )
Esempio n. 3
0
    def test_connect_database(
            self, connection_pool_strategy: MySQLDriverConnectionPool):
        _conn_pool = get_connection_pool(pool_name=Test_Pool_Name)
        assert _conn_pool is not None, f"It should return a connection pool object back with the pool name {Test_Pool_Name}."
        assert _conn_pool.pool_name == Test_Pool_Name, f"The pool name we get from the connection pool instance should be equal to the name we set '{Test_Pool_Name}' before."
        assert int(_conn_pool.pool_size) == int(
            Test_Pool_Size
        ), f"The pool size we get from the connection pool instance should be equal to the number we set '{Test_Pool_Size}' before."

        _all_pools = database_connection_pools()
        assert len(
            _all_pools
        ) != 0, "The length should not be '0' because we only instantiate one database connection pool object."
        assert Test_Pool_Name in _all_pools.keys(
        ), f"The pool name '{Test_Pool_Name} should be in the keys of {_all_pools}.'"
        assert _all_pools[
            Test_Pool_Name] is _conn_pool, f"The instances '{_all_pools[Test_Pool_Name]}' and '{_conn_pool}' should be the same."
Esempio n. 4
0
 def _get_one_connection(self,
                         pool_name: str = "",
                         **kwargs) -> PooledMySQLConnection:
     while True:
         try:
             __connection = get_connection_pool(
                 pool_name=pool_name).get_connection()
             return __connection
         except PoolError as e:
             time.sleep(5)
             raise e
         except AttributeError as ae:
             if "'NoneType' object has no attribute 'get_connection'" in str(
                     ae):
                 raise ConnectionError(
                     f"Cannot get the one connection instance from connection pool because it doesn't exist the connection pool with the name '{pool_name}'."
                 )
             else:
                 raise ae
Esempio n. 5
0
 def get_one_connection(self,
                        pool_name: str = "",
                        **kwargs) -> PooledMySQLConnection:
     while True:
         try:
             # return self.database_connection_pool.get_connection()
             __connection = get_connection_pool(
                 pool_name=pool_name).get_connection()
             logging.info(f"Get a valid connection: {__connection}")
             return __connection
         except PoolError as e:
             logging.error(
                 f"Connection Pool: {get_connection_pool(pool_name=pool_name)} "
             )
             logging.error(
                 f"Will sleep for 5 seconds to wait for connection is available."
             )
             time.sleep(5)
         except AttributeError as ae:
             raise ConnectionError(
                 f"Cannot get the one connection instance from connection pool because it doesn't exist the connection pool with the name '{pool_name}'."
             )
Esempio n. 6
0
 def test_connect_database(self, connection_pool_strategy: MySQLDriverConnectionPool):
     _conn_pool = get_connection_pool(pool_name=Test_Pool_Name)
     assert _conn_pool is not None, f"It should return a connection pool object back with the pool name {Test_Pool_Name}."
     assert _conn_pool.pool_name == Test_Pool_Name, f"The pool name we get from the connection pool instance should be equal to the name we set '{Test_Pool_Name}' before."
     assert int(_conn_pool.pool_size) == int(Test_Pool_Size), f"The pool size we get from the connection pool instance should be equal to the number we set '{Test_Pool_Size}' before."
Esempio n. 7
0
 def close_pool(self, pool_name: str) -> None:
     get_connection_pool(pool_name=pool_name).close()