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 7180585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:22:47+00:00 2026-05-28T17:22:47+00:00

I am creating a class that is used as an portal to read, insert

  • 0

I am creating a class that is used as an portal to read, insert and update values within a SQL Server database. This class implements the singleton design pattern, and therefore there will only be one instance of this class at any one time.

This class has individual methods for each action for the database values (read, write & delete), and each of those methods will open a SQLConnection, create, populate & execute the SQLCommand, and finally close the connection once completed.

Here is the example for adding a value:

public void AddGlossaryValue(string name, string value)
{
   SqlConnection connection = null;
   SqlCommand command = null;

   try
   {
      connection = new SqlConnection(connectionString);
      connection.Open();

      command = new SqlCommand(SQL_INSERT_COMMAND, connection);
      command.Parameters.Add("@name", SqlDbType.NVarChar, 50);
      command.Parameters.Add("@value", SqlDbType.NVarChar, 50);

      command.Parameters["@name"].Value = name;
      command.Parameters["@value"].Value = value;

      command.ExecuteNonQuery();
   }
   catch (Exception ex)
   {
      // handle exception - omitted in this example because it's irrelevant
   }
   finally
   {
      try
      {
         connection.Close();
      }
      catch (Exception) { }
   }
}

I come from a Java background, where typically you would declare such methods as Synchronized, which means that the method can only be executed from one source at any one time. From my research into C#, it would appear that the typical method for achieving this is to lock and object.

My question is however, would that be recommended in a case such as above, and if so, where would the lock object be placed? Would it be immediately before the creation of the SQLConnection?

Many 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-28T17:22:48+00:00Added an answer on May 28, 2026 at 5:22 pm

    Why lock at all? You’re not using any shared resources in your dll code.

    Just catch the sql error you’d likely see from a collision. If you insert something that’s already there, then you can do whatever happens to be appropriate at the time.

    Save locking for shared resources in the code; things multiple threads could be accessing where you need to ensure determinant behavior. This code looks like you’re just firing something off to the database, you won’t be getting any behavior that you couldn’t easily explain by not using locks here.

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

Sidebar

Related Questions

I'm creating a class that can read from a dicom file. This is basically
I'm creating a specialised proxy class that implements IList<T> and wraps an internal List<T>
I have a module that is used by creating a custom class loader. The
I am creating a class that implements the composite pattern; the class is supposed
When creating a class that has internal private methods, usually to reduce code duplication,
I am creating a class that determines which of number of registered WCF client
I'm creating a class that derives from List... public class MyList : List<MyListItem> {}
I'm in the process of creating a class that stores metadata about a particular
im creating a Factory class that will contruct and return an object. I normally
I'm creating a class named TetraQueue that inherits System.Collections.Generic.Queue class overriding the Dequeue method,

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.