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

  • Home
  • SEARCH
  • 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 302169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:04:22+00:00 2026-05-12T07:04:22+00:00

I created a web service which is called from the client side to store

  • 0

I created a web service which is called from the client side to store the data into the database. These data are sent every 200 ms from a single user and each time the data are sent the database connection is opened and closed which I believe is not good for performance.

The data are stored by calling REST.StoreAcceleration() method and SQLWorks.StoreAcceleration() the following way:

public Response StoreAcceleration(string strSessionString, string strMeasurementTime, string strAccelerationX, string strAccelerationY, string strAccelerationZ)
    {
        SQLWorks sqlWorks = new SQLWorks();
        Response response = new Response();
        try
        {
            string strTime = strMeasurementTime.Replace("_", " ");
            DateTime measurementTime = DateTime.ParseExact(strTime, "yyyy-MM-dd HH:mm:ss:fff", null);
            double accelerationX = Convert.ToDouble(strAccelerationX.Replace(".", ","));
            double accelerationY = Convert.ToDouble(strAccelerationY.Replace(".", ","));
            double accelerationZ = Convert.ToDouble(strAccelerationZ.Replace(".", ","));

            sqlWorks.StoreAcceleration(strSessionString, measurementTime, accelerationX, accelerationY, accelerationZ);

            response.Successful = true;
            response.Comment = "Stored!";
        }
        catch(Exception ex)
        {
            string sDummy = ex.ToString();
            response.Comment = "an error occured!";
            response.Successful = false;
        }

        return response;
    }

public bool StoreAcceleration(string strStringSession, DateTime receivedTime, double accelerationX, double accelerationY, double accelerationZ)
    {
        bool result = false;
        string select =
            "INSERT INTO acceleration (session_id, measurement_time, acceleration_x, acceleration_y, acceleration_z) VALUES (@sessionID, @measurementTime, @accelerationX, @accelerationY, @accelerationZ)";
        SqlConnection conn = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand(select, conn);
        int sessionID = getSessionID(strStringSession);
        if(sessionID == 0)
            return false;
        updateSessions(sessionID);
        string strRecordTime = receivedTime.ToString("yyyy-MM-dd HH:mm:ss:fff");
        cmd.Parameters.AddWithValue("sessionID", sessionID.ToString());
        cmd.Parameters.AddWithValue("measurementTime", strRecordTime);
        cmd.Parameters.AddWithValue("accelerationX", accelerationX.ToString());
        cmd.Parameters.AddWithValue("accelerationY", accelerationY.ToString());
        cmd.Parameters.AddWithValue("accelerationZ", accelerationZ.ToString());
        try
        {
            conn.Open();
            cmd.ExecuteNonQuery();
            result = true;
        }
        catch(Exception ex)
        {
            string sDummy = ex.ToString();
        }
        finally
        {
            conn.Close();
        }
        return result;
    }

The problem here is that SqlConnection is opened and closed on every method call.

I would appreciate if anyone could suggest how to improve the solution in order to prevent frequent database connection opening/closing.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 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. Editorial Team
    Editorial Team
    2026-05-12T07:04:22+00:00Added an answer on May 12, 2026 at 7:04 am

    If you have a connection pool set up, your database connection will not get closed. Never mind the conn.Close(). From MSDN:

    The Close method rolls back any
    pending transactions. It then releases
    the connection to the connection pool,
    or closes the connection if connection
    pooling is disabled.

    See here to set it up, if you’re not already using it:
    SQL Server connection pooling (ADO.NET) and connection strings.

    Basically, unless you have pooling=false or something similar in your connection string, it should already be active. However, you might want to set some MinPoolSize to always have a couple of connections ready to be used.


    By the way, are you storing the received time as actual strings? Otherwise you can get rid of the whole ToString(..) thing. ADO.NET will make sure the date doesn’t get misinterpreted. The same goes for the other values really; why are you converting them to strings?

    Lastly, SqlCommand implements IDisposable, so you should be disposing it, much like the connection. I’d suggest rewriting to something like this:

    public bool StoreAcceleration(string strStringSession, DateTime receivedTime, double accelerationX, double accelerationY, double accelerationZ)
    {
        string select =
            "INSERT INTO acceleration (session_id, measurement_time, acceleration_x, acceleration_y, acceleration_z) VALUES (@sessionID, @measurementTime, @accelerationX, @accelerationY, @accelerationZ)";
    
        int sessionID = getSessionID(strStringSession);
        if (sessionID == 0)
            return false;
        updateSessions(sessionID);
    
        using (SqlConnection conn = new SqlConnection(connectionString))
        using (SqlCommand cmd = new SqlCommand(select, conn))
        {
            cmd.Parameters.AddWithValue("sessionID", sessionID);
            cmd.Parameters.AddWithValue("measurementTime", receivedTime);
            cmd.Parameters.AddWithValue("accelerationX", accelerationX);
            cmd.Parameters.AddWithValue("accelerationY", accelerationY);
            cmd.Parameters.AddWithValue("accelerationZ", accelerationZ);
    
            try
            {
                conn.Open();
                cmd.ExecuteNonQuery();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
    

    You no longer need the Close() call in a finally block. The using block will implicitly call Dispose() when the variable conn falls out of scope. That will in turn call the Close() method internally.

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

Sidebar

Ask A Question

Stats

  • Questions 169k
  • Answers 169k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Not in 3.x - check out the issue @ atlassian… May 12, 2026 at 1:56 pm
  • Editorial Team
    Editorial Team added an answer I think the title is a little misleading if you… May 12, 2026 at 1:56 pm
  • Editorial Team
    Editorial Team added an answer From what I can tell you have to write a… May 12, 2026 at 1:56 pm

Related Questions

I have a ASMX web services running as 'network service'. I want to be
I have an application that I am writing that modifies data on a cached
I have a situation which I think I need the functionality provided by an
I've got a wizard which needs to validate if a user is logged in

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.