My websites were facing 503 error. It seems to be due to the overcrowding of the application pool.
The memory excess can be attributed to two possibilities;
(a) There is a memory leak somewhere due to suboptimal code, or
(b) The site really requires that much memory
{
public MyFunction()
{
//
// TODO: Add constructor logic here
//
}
public static string ConnectionString
{
get
{
try
{
return System.Configuration.ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;
}
catch (Exception ex)
{
throw new ApplicationException("Unable to get Database Connection string from Web Config File");
}
}
}
public static string Execute(string query)
{
try
{
int ret = 0;
SqlConnection con = new SqlConnection(Database.ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand(query, con);
ret = cmd.ExecuteNonQuery();
con.Close();
return ret.ToString();
}
catch (Exception ex)
{
return ex.Message.ToString();
}
}
public static string GenerateOTP(int length, string is_alpha_numeric)
{
string otp = "";
try
{
string alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string small_alphabets = "abcdefghijklmnopqrstuvwxyz";
string numbers = "1234567890";
string characters = numbers;
if (is_alpha_numeric == "1")
{
characters += alphabets + small_alphabets + numbers;
}
//int length = int.Parse(ddlLength.SelectedItem.Value);
otp = string.Empty;
for (int i = 0; i < length; i++)
{
string character = string.Empty;
do
{
int index = new Random().Next(0, characters.Length);
character = characters.ToCharArray()[index].ToString();
} while (otp.IndexOf(character) != -1);
otp += character;
}
}
catch (Exception ex)
{
}
return otp;
}
}
protected void btn_save_Click(object sender, EventArgs e)
{
try
{
string query = "";
int success = 0;
int error = 0;
string member_key = "";
string res = "";
for (int i = 0; i < 1500; i++)
{
repeat:
member_key = MyFunction.GenerateOTP(8, "1");
query = "Insert into [Person].[MemberCode]([MemberKey],[CreateDate]) values('" + member_key + "','" + DateTime.Now.ToShortDateString() + "')";
res = MyFunction.Execute(query);
if (res == "1")
{
success++;
}
else if (res.Contains("Violation of UNIQUE KEY constraint"))
{
goto repeat;
}
else
{
error++;
}
}
if (success > 0)
{
msg.InnerHtml = "
}
else
{
msg.InnerHtml = "
}
}
catch (Exception ex)
{
msg.InnerHtml = "
}
}
Nitin SontakkePosted Apr 3, 2017, 11:01 PM
TABISH ALAMPosted Apr 3, 2017, 9:31 AM
I am facing this problem on regular basis when visitors come on our websites. So I want to check to resolving this error using thousands of rows entry, because this code occure same error 503 service unavailable.
I just want to know how can I solve application pool problem and how to release memory explicitly.
Amit GuptaPosted Apr 3, 2017, 6:00 AM
Nitin SontakkePosted Apr 3, 2017, 5:04 AM
Nitin SontakkePosted Apr 3, 2017, 4:55 AM