I'm trying to figure out an issue that I'm seeing when you setup a readonly replica. I have a simple application that will query the readonly replica and I can validate that it hits the correct one by running a @@servername. Everything works as planned. I can failover to the synchronous servers without any issue and my test application still reads from the replica.
The issue I have is that when I reboot the readonly replica, the application never returns back to the readonly replica. It goes to whatever is set in the routing even after the readonly replica is back online. New connections from a different server will hit the readonly replica. If I try a different method of connecting to the AG on the server I'm running the test, it will connect to the readonly replica. The powershell script below will continue to hit a different server.
If I stop the application, wait about 10 or 15 minutes then start up the application, I'll then hit the readonly replica. If I set my connection string to use pooling=false. Then it behaves as it's supposed to by hitting the replica once the replica is ready to accept connections.
This is what I'm using to test out the readonly replica scenario.
function Get-Readonly-Server
{
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=AG_OLTP_Listnr.myenv.local;Database=AGTest;Integrated Security=True;applicationintent=readonly"
$SqlConnection.Open()
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = "select @@servername as colName into #tmp; select colName from #tmp; drop table #tmp"
$SqlCmd.Connection = $SqlConnection
$servername = $SqlCmd.ExecuteScalar()
$SqlConnection.Close()
$servername
}
for ($i=1; $i -le 1000000; $i++)
{
Get-Readonly-Server
write-host $i
Start-Sleep -s 1
}
What am I missing? Or is my test incorrect?