In connectivity of SQL database using ADO.Net C#, I am getting exceptions randomly while working with thousands of data simultaneously which is being executed in thread such as
A connection was successfully established with the server, but then an error occurred during the pre-login handshake. (provider: SSL Provider, error: 0 - The wait operation timed out.)
The client was unable to establish a connection because of an error during connection initialization process before login. Possible causes include the following: the client tried to connect to an unsupported version of SQL Server; the server was too busy to accept new connections; or there was a resource limitation (insufficient memory or maximum allowed connections) on the server. (provider: TCP Provider, error: 0 - An existing connection was forcibly closed by the remote host.)
Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.
My connection string is as
<add name="ConnDBString" connectionString="datasource;Initial Catalog=dbname;pooling=true;connection lifetime=120;Max Pool Size=1000" providerName="System.Data.SqlClient"/>
In reference of other questions I have optimized my code for connection establishment like below
public static int ExecuteNonQuery(string commandText, CommandType commandType, ref List<SqlParameter> parameters)
{
int result = 0;
if (!string.IsNullOrEmpty(commandText))
{
using (var cnn = new SqlConnection(Settings.GetConnectionString()))
{
var cmd = cnn.CreateCommand();
cmd.CommandText = commandText;
cmd.CommandType = commandType;
cmd.CommandTimeout = Convert.ToInt32(Settings.GetAppSetting("CommandTimeout") ?? "3600");
cmd.Parameters.AddRange(parameters.ToArray());
cnn.Open();
result = cmd.ExecuteNonQuery();
cmd.Dispose();
}
}
return result;
}
Please advise.