Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 88203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:31:22+00:00 2026-05-10T22:31:22+00:00

This is my first crack at a method that is run periodically during the

  • 0

This is my first crack at a method that is run periodically during the lifetime of my ASP.NET application to clean up expired sessions stored in my database. It seems to work pretty well, but the software engineer in me doesn’t feel ‘right’ about this code. I’ve been working with LINQ to SQL for a few months now, but I’m not very confident in the following code. I’m worried about a few things:

  1. Is the following code safe to run in a situation where the database is being accessed by different threads in my application? I have a decent understanding of the idea of transactions, but I want to make sure I’m using them properly.

  2. Is my query going to cause performance issues? Or is it appropriate in this case to select all of the records in this particular table? This method only runs every 15 minutes, so it’s not like that query will be made over and over again in a short period of time.

  3. Is there a better way that I could do this? I have a nagging feeling that there is.

Code:

/// <summary> /// Method, run periodically, to remove all sign in records that correspond to expired sessions. /// </summary> /// <param name='connectionString'>Database connection string</param> /// <returns>Number of expired sign in records removed</returns> public static int Clean(String connectionString) {     MyDatabaseDataContext db = new MyDatabaseDataContext(connectionString);      var signIns = db.SignIns.Select(x => x);     int removeCount = 0;      using (TransactionScope scope = new TransactionScope())     {         foreach (SignIn signIn in signIns)         {             DateTime currentTime = DateTime.Now;             TimeSpan span = currentTime.Subtract(signIn.LastActivityTime);              if (span.Minutes > 10)             {                 db.SignIns.DeleteOnSubmit(signIn);                 ++removeCount;             }         }          db.SubmitChanges();         scope.Complete();     }      return removeCount; } 
  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T22:31:23+00:00Added an answer on May 10, 2026 at 10:31 pm

    This sounds like something you could easily do in a sproc. SQLServer gives you a GETDATE() method that returns the current time… I don’t see why you can’t just

     DELETE * FROM tblSignIns   WHERE LastActivityTime < DATEADD('minute', -10, GETDATE()); 

    Wouldn’t that do the same thing?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.