Every query fails after a failover of a source database

Every query fails after a failover of a source database#

Symptoms#

After a planned or unplanned failover of a source database - for example a SQL Server availability group whose primary replica changed - every query that reads from that source fails in the sessions that were connected before the failover, with an error such as:

Error at line 0 column 0: BeginExecuteReader requires an open and available Connection. The connection’s current state is closed.

The source itself is available again, and a new session to Querona reads from it without an error.

Cause#

Querona keeps the connections a session opened to a source for the whole life of that session and reuses them for its later statements. When the source ends the session behind such a connection - a failover, a KILL, an idle-session policy or a network cut - the driver reports one error, to the first statement it sends on that connection afterwards, and leaves the connection object closed. Until the connection was replaced, every later statement that reused it failed with the error above.

Resolution#

Querona replaces a connection the source has closed by itself, for sources it reaches through an ADO.NET driver such as SQL Server or PostgreSQL: the next statement that needs the connection opens a new one, which after a failover reaches the new primary replica, and runs the data source’s initialization SQL on it again. No action is needed, and the Querona service does not have to be restarted.

Some cases still need the client’s attention:

  • On each connection that was open when the source ended its session, one statement fails with the driver’s error: the statement that was running, or the next one sent on that connection. Run it again.

  • A connection on which an explicit transaction was open when the source ended the session cannot be replaced, because the source rolled that transaction back and a new connection could not continue it. Every statement of the transaction is refused with:

    The connection to data source '<name>' was lost while a transaction was open on it. The transaction cannot be continued on a new connection; roll it back and run the statements again.
    

    Issue ROLLBACK and run the transaction again. A COMMIT is refused with the same message and the transaction is rolled back, so after a refused COMMIT only run the transaction again.

  • A temporary table (#name) that a session created on the source is lost with the source session. Drop it and create it again.

  • Sources reached through JDBC are not covered: reconnect the client.

On a version of Querona without this behavior, the sessions connected before the failover keep failing; reconnect the client, or restart the Querona service when the sessions belong to a connection pool that cannot be reconnected.