In these series of Platform Runtime posts, we will be offering some best practices, recommendations and guidelines to assist you in optimizing your code for a better reliable experience.
In the Cloud World Today, there are several things to consider because of the way Azure cloud Works. In this topic will talk about the usage of UserConnection with Azure SNAT Pool.
Azure uses source network address translation (SNAT) to communicate with endpoints outside Azure,
When an instance initiates an outbound flow to a destination in the public IP address space, Azure dynamically maps the private IP address to a public IP address.
The SNAT ports are a finite resource, the pre-allocation size is pre-defined for SNAT Pool. If the ports get used, the next request will be throttled.
UserConnection is the class to create the SQL Connection from D365 Fin&Ops. The Connection is running on the physical networking connection with SNAT port assigned, which means the max number of connections is limited by the pool and not only by SQL.
Once the SNAT Pool is exhausted, the request to setup SQL Connection will be dropped.
Let’s Examine the following code,
Can you find something wrong here ?
Let’s compare the results from SQL system table of sys.dm_exec_sessions.
The UserConnection is still holding the connection of 56 even after the call of the InsertData().
Wait.... UserConnection is a managed object, why not delete it? Because Garbage Collector won’t free the resources instantaneously even if the object is out of scope. If InsertData() is a very popular function, it may be called very often, which will definitely lead to the SNAT Pool exhaustion and no more AOS Session allocated.
When the UserConnection is the only choice for you, we strongly suggest to call finalize() to close the Connection.
Here is the Good Sample,
By Using the finalize() will efficiently use the UserConnection and will reduce the possibility for the application to run out of connections.
Can we write "userConnection = null;"? Will it be the same?
Thanks for the tip, sound like good practice to end with finalise.
There is no different b/w A and B. XPP compiler will set conn as null if conn is not explicitly assigned as null.