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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:58:05+00:00 2026-05-31T05:58:05+00:00

lock (_connectionLock) { if (conn == null) { conn = GetOpenConnection(connectionString); } try {

  • 0
lock (_connectionLock) {
    if (conn == null) {
        conn = GetOpenConnection(connectionString);
    } 

    try {
        PerformDbAction(conn);
    } finally {
        conn.Dispose();
    }
}

I have run into a problem where multithreading can cause issues with null connections as they can be opened and closed by several threads running at once. I tried to solve the issue by locking the process (above, code simplified for clarity) but have found that this seriously slows down the performance.

I tried to get around this problem by using two separate locks for the creation/disposal of database connections and to perform database action outside of locking:

lock (_connectionLock) {
    if (conn == null) {
        conn = GetOpenConnection(connectionString);
    } 
}

try {
    PerformDbAction(conn);
} finally {
    lock(_connectionLock) 
    conn.Dispose();
}
}

Only I realized that the above doesn’t work as another thread may try to perform a database action with a connection that has already been disposed by another thread.

Could anyone suggest an alternative or solution where I can safely lock access to the database connection strings without slowing everything down so much?

EDIT: Sorry for not including this previously, but the reason I am not just creating new connections and disposing of them immediately is that I am trying to avoid unwanted MSDTC escalation. In using GetOpenConnection I am reusing an existing connection as this is one of the things that triggers MSDTC escalation.

I have managed to avoid the escalation with the top code example, but it performs way too slow.

  • 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-31T05:58:06+00:00Added an answer on May 31, 2026 at 5:58 am

    Simply don’t have one shared connection variable. Instead, each time you need to do something, open a connection, use it, and close it as soon as you can. You don’t need to use any locks in your code, and the connection pool will manage the real network connections to the database.

    At the moment, you’ve essentially built a primitive connection pool containing exactly one connection, which means you’ve got no concurrency at all in the database (well, not per process).

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

Sidebar

Related Questions

A Lock is always followed by a try/finally block, why? ReentrantReadWriteLock readWriteLockBitmap = new
Results Lock: 85.3 microseconds Monitor.TryEnter: 11.0 microseconds Isn't the lock expanded into the same
Do I have to lock access to instance members? Example: public class HttpModule :
I try to lock a file and write to it with the code below:
We'd like to 'lock-down' an iPhone/iPod/iPad so that the user can only run one
when using a lock does the thing you are locking on have to be
I am developing a lock screen in my application and i have the same
I would like to 'lock' the mouse into a specific Y coordinate on the
We have created a lock file to avoid a race condition. The lockfile is
I have a lock in my code. I have two threads running at the

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.