简体   繁体   中英

Azure SQL - connection closes automatically after 30 minutes being idle

There is an old .NET desktop application that is used on daily basis. Since we have moved our database to Azure SQL from our old on-premise server, we have problems with the SQL connection. The Azure SQL database is using tier S2.

After 15-30 minutes of being idle, the application cannot query the SQL anymore because the connection has been closed. The developer of that app told me that, because of some performance issues, he has to keep the SQL connection all the time open and he cannot change it to an open-close principle. Unfortunately, I have to deal with that and I do not know the reason why it has to stay open.

After those 15-30 minutes of being idle, the application gets the following error:

A transport-level error has occurred when receiving results from the server. (provider: TCP Provider, error: 0 - An existing connection was forcibly closed by the remote host.)

He tried to add ConnectionLifetime=0 to his connection string, but it did not help. He also had this error:

SQL Server clients operating over TCP send keepalives at 30 second intervals. (Keepalives are essentially 0 len TCP packets) usually considered negligable traffic.

Is there any way how we can keep alive the connection or increase the timeout after being idle? Any ideas or experience would be appreciated!

The developer of that app told me that, because of some performance issues, he has to keep the SQL connection all the time open

I would be interested to know the "performance issues" that require a long-lived persistent connection. This is not a cloud-friendly pattern but there is a work-around that does not require code changes.

Assuming the connection is being dropped by the Azure SQL Gateway (which has a 30 minute idle timeout), a redirect connection policy is recommended. Below is the implementation excerpt from the documentation.

Redirect (recommended): Clients establish connections directly to the node hosting the database, leading to reduced latency and improved throughput. For connections to use this mode, clients need to:

  • Allow outbound communication from the client to all Azure SQL IP addresses in the region on ports in the range of 11000 to 11999. Use the Service Tags for SQL to make this easier to manage.

  • Allow outbound communication from the client to Azure SQL Database gateway IP addresses on port 1433.

  • When using the Redirect connection policy, refer to the Azure IP Ranges and Service Tags – Public Cloud for a list of your region's IP addresses to allow.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM