예제 #1
0
    def test_pre_ping_weakref_finalizer(self):
        pool = self._pool_fixture(pre_ping=True)

        conn = pool.connect()
        old_dbapi_conn = conn.connection
        conn.close()

        eq_(old_dbapi_conn.mock_calls, [call.cursor(), call.rollback()])

        self.dbapi.shutdown("execute", stop=True)
        self.dbapi.restart()

        conn = pool.connect()
        dbapi_conn = conn.connection
        del conn
        gc_collect()

        # new connection was reset on return appropriately
        eq_(dbapi_conn.mock_calls, [call.cursor(), call.rollback()])

        # old connection was just closed - did not get an
        # erroneous reset on return
        eq_(
            old_dbapi_conn.mock_calls,
            [call.cursor(), call.rollback(), call.cursor(), call.close()],
        )
예제 #2
0
    def test_connect_across_restart(self):
        pool = self._pool_fixture(pre_ping=True)

        conn = pool.connect()
        stale_connection = conn.connection
        conn.close()

        self.dbapi.shutdown("execute")
        self.dbapi.restart()

        conn = pool.connect()
        cursor = conn.cursor()
        cursor.execute("hi")

        stale_cursor = stale_connection.cursor()
        assert_raises(MockDisconnect, stale_cursor.execute, "hi")
예제 #3
0
    def test_waits_til_exec_wo_ping_db_is_stopped(self):
        pool = self._pool_fixture(pre_ping=False)

        conn = pool.connect()
        conn.close()

        self.dbapi.shutdown("execute", stop=True)

        conn = pool.connect()

        cursor = conn.cursor()
        assert_raises_message(
            MockDisconnect,
            "Lost the DB connection on execute",
            cursor.execute, "foo"
        )
예제 #4
0
    def test_waits_til_exec_wo_ping_db_is_stopped(self):
        pool = self._pool_fixture(pre_ping=False)

        conn = pool.connect()
        conn.close()

        self.dbapi.shutdown("execute", stop=True)

        conn = pool.connect()

        cursor = conn.cursor()
        assert_raises_message(
            MockDisconnect,
            "Lost the DB connection on execute",
            cursor.execute,
            "foo",
        )
예제 #5
0
    def test_raise_db_is_stopped(self):
        pool = self._pool_fixture(pre_ping=True)

        conn = pool.connect()
        conn.close()

        self.dbapi.shutdown("execute", stop=True)

        assert_raises_message(MockDisconnect, "database is stopped",
                              pool.connect)
예제 #6
0
    def test_pre_ping_weakref_finalizer(self):
        pool = self._pool_fixture(pre_ping=True)

        conn = pool.connect()
        old_dbapi_conn = conn.connection
        conn.close()

        # no cursor() because no pre ping
        eq_(old_dbapi_conn.mock_calls, [call.rollback()])

        conn = pool.connect()
        conn.close()

        # connect again, we see pre-ping
        eq_(
            old_dbapi_conn.mock_calls,
            [call.rollback(), call.cursor(),
             call.rollback()],
        )

        self.dbapi.shutdown("execute", stop=True)
        self.dbapi.restart()

        conn = pool.connect()
        dbapi_conn = conn.connection
        del conn
        gc_collect()

        # new connection was reset on return appropriately
        eq_(dbapi_conn.mock_calls, [call.rollback()])

        # old connection was just closed - did not get an
        # erroneous reset on return
        eq_(
            old_dbapi_conn.mock_calls,
            [
                call.rollback(),
                call.cursor(),
                call.rollback(),
                call.cursor(),
                call.close(),
            ],
        )
예제 #7
0
        def run_test(name, pool, should_hang):
            if should_hang:
                creator.mock_connector = hanging_dbapi
            else:
                creator.mock_connector = fast_dbapi

            conn = pool.connect()
            conn.operation(name)
            time.sleep(1)
            conn.close()
예제 #8
0
        def run_test(name, pool, should_hang):
            if should_hang:
                creator.mock_connector = hanging_dbapi
            else:
                creator.mock_connector = fast_dbapi

            conn = pool.connect()
            conn.operation(name)
            time.sleep(1)
            conn.close()
예제 #9
0
    def test_raise_db_is_stopped(self):
        pool = self._pool_fixture(pre_ping=True)

        conn = pool.connect()
        conn.close()

        self.dbapi.shutdown("execute", stop=True)

        assert_raises_message(
            MockDisconnect, "database is stopped", pool.connect
        )
예제 #10
0
    def test_handle_error_sets_disconnect(self):
        pool = self._pool_fixture(pre_ping=True, setup_disconnect=False)

        @event.listens_for(pool._dialect, "handle_error")
        def setup_disconnect(ctx):
            assert isinstance(ctx.sqlalchemy_exception, exc.DBAPIError)
            assert isinstance(ctx.original_exception, MockDisconnect)
            ctx.is_disconnect = True

        conn = pool.connect()
        stale_connection = conn.dbapi_connection
        conn.close()

        self.dbapi.shutdown("execute")
        self.dbapi.restart()

        conn = pool.connect()
        cursor = conn.cursor()
        cursor.execute("hi")

        stale_cursor = stale_connection.cursor()
        assert_raises(MockDisconnect, stale_cursor.execute, "hi")
예제 #11
0
 def checkout():
     for j in range(2):
         c1 = pool.connect()
         time.sleep(.02)
         c1.close()
         time.sleep(.02)
예제 #12
0
 def checkout():
     for j in range(2):
         c1 = pool.connect()
         time.sleep(.02)
         c1.close()
         time.sleep(.02)
예제 #13
0
 def reconnect(self):
     """Closes the existing database connection and re-opens it."""
     self.close()
     pool = get_pool(self._db_args, self._pool_size, self._max_overflow)
     self._db = pool.connect()