开发者

Database connection take time when user log-off? or signout from asp.net problem

开发者 https://www.devze.com 2023-02-08 13:26 出处:网络
i have ASP.NET application, when user log-off, and immediately trying to log-in again,it fails and page is waiting, after 20 seconds the page logins, i think the connection with database, e.g Access,

i have ASP.NET application,

when user log-off, and immediately trying to log-in again,it fails and page is waiting, after 20 seconds the page logins, i think the connection with database, e.g Access, inst closed. what i need to do when user log-off to close all the database conenction instances. i am using this code when the user click log-off button:

protected void closeCon()
{
    //to close access
    OleDbConnection sqlconConn开发者_开发问答ection = (OleDbConnection)DatabaseConnection.Instance.GetConnection();

    sqlconConnection.Close();
    // to close sql server
    SqlConnection sqlconConnection1 = (SqlConnection)(SqlConnection)DatabaseConnection.Instance.GetConnection();

    if (sqlconConnection1.State != ConnectionState.Closed)
    {
        sqlconConnection1.Close();        
    }
}
     Session.Abandon();
    HttpCookie ObjCookie = new HttpCookie("ourcase");
    ObjCookie.Expires = DateTime.Now;
    HttpContext.Current.Response.Cookies.Add(ObjCookie);
    Response.Redirect("/login/default.aspx");
    System.Web.Security.FormsAuthentication.SignOut();


dbConn.Close() should close the database connection.

Alternatively, where you are using the database connections, you could use a using statement that will automatically close and dispose of the connection object after it is finished with. This is psuedo code:

using (OleDbConnection dbConn = new OleDbConnection())
{
    // do your db work here
}


Similar to what @Ardman said, always wrap the connection in a using statement.

using (OleDbConnection dbConn = new OleDbConnection())
{
    // do your db work here
}

But since Access supports connection pooling, you should open and close the connection on each trip to the database.

Not each time a user logs on and off.

0

精彩评论

暂无评论...
验证码 换一张
取 消