0

I'm working on a Web app using ASP.NET. I have a class called "Sistema" that uses the Singleton pattern.

When the instance of Sistema is created, the database connection is opened and a process runs that loads some static information for later use. This lasts almost 2 minutes.

private static Sistema instance;
private Sistema()
{
    OpenDataBase();
    LoadStaticInformation();
}

public static Sistema GetInstance()
{
    if (instance == null)
    {
        instance = new Sistema();
    }
    return instance;
}

The reason why I keep the connection to the database open is because I'm using db4o, that strongly suggests this. Here are some references:

db4o best practice to query objects from db

Is it OK to open a DB4o file for query, insert, update multiple times?

Query regarding database connectivity in db4o

On my Web App I have a Master Page that controls if the user is logged in by checking a Session variable. If this Session is null, then the user is sent to the Login Page.

On the Login Page, the first thing I do is to check if the instance of "Sistema" is null. If it is, then when the user hits the Submit button, a message is shown saying "Login can take up to two minutes. Please wait". If it is not null, then no message is shown as the login action takes only a couple of seconds.

I have been told by the users, that when going through the System, they are sometimes sent back to the login page, and when they try to login, the message saying "Login can take up to two minutes" is displayed and login indeed takes a while.

The fact that they are sent back to the login page means that the Session variable is lost, and the message being displayed means that the instance of "Sistema" is also null.

In order to determine why this is happening, I created a web page that sends an email to me when the instance of Sistema detected to be null. I thought that if I was able to know when this occurred, I might discover what is going on.

This web page is really simple. It runs every 10 minutes and checks if the instance of Sistema is null. If it is, then an email is sent and the instance of Sistema is created.

bool isInstanceNull = Sistema.IsInstanceNull();
if (isInstanceNull)
{
    String emailTo = "...";
    String emailContent = "...";
    Functions.SendMail(emailTo, "Sistema is null", emailContent, "");

    Sistema.GetInstance();
    Functions.SendMail(emailTo, "Sistema has been created", emailContent, "");
}

The only thing I discovered is that it's not happening at a specific time. For example, last week it happened around 7pm, but today it happened at 2 am.

Regarding the Session timeout, I'm using a solution in the code behind: http://www.beansoftware.com/ASP.NET-Tutorials/Keep-Session-Alive.aspx.

Any suggestions to why is this happening?

Community
  • 1
  • 1
Gonzalo
  • 982
  • 6
  • 23
  • 39

2 Answers2

0

The application pool has a property that causes it to be automatically recycled every N minutes (defaults to 1740, or every 29 hours.) Make this zero to disable recycling. The propertry is (on IIS7) under the "Recycling" heading and is called "Regular Time Interval (minutes)"

enter image description here

Apart from that, you should always close connections immediately and dont use static connections at all in ASP.NET (when Connection-Pooling is enabled which is default).

I mention it because of:

private static Sistema instance;
private Sistema()
{
    OpenDataBase();
    LoadStaticInformation();
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Hi Tim, thanks for your quick response. I wasn't aware of that! I'll try making in 0 to see if that is causing the problem. Regarding your comment about connection, I'm using db4o that strongly suggests keeping the connection live. I have edited my question with some references if you'd like to check. Thanks! – Gonzalo Sep 26 '12 at 14:12
0

You should not keep a connection to the database open. Typically a new connection is opened for every request. Maybe the database sometimes decides it has too many users or the connection is open for too long, as a result it closes the connection and your object crashes. An open connection to the database is also a security risk.

I'm not 100% sure about this though...

You should probably move connecting to the database down to some kind of query execute method. Also it seems unwise to load such a blob of data all at once, can't you do it on the background or only load the information a user needs to see at the time?

MrFox
  • 4,852
  • 7
  • 45
  • 81
  • Hi Mr.Fox, thanks for your quick response. I'm using db4o that strongly suggests keeping the connection live. I have edited my question with some references if you'd like to check. Thanks! – Gonzalo Sep 26 '12 at 14:16