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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:48:04+00:00 2026-05-27T02:48:04+00:00

Here’s my code: <ThreadStatic()> Dim _GlobalConnection As TdConnection Public Property GlobalConnection As TdConnection Get

  • 0

Here’s my code:

<ThreadStatic()>
Dim _GlobalConnection As TdConnection

Public Property GlobalConnection As TdConnection
    Get
        If _GlobalConnection Is Nothing Then
            _GlobalConnection = New TdConnection
        End If
        If _GlobalConnection.State <> ConnectionState.Open Then
            OpenConnection(_GlobalConnection)
        End If
        Return _GlobalConnection
    End Get
    Set(ByVal value As TdConnection)
        _GlobalConnection = value
    End Set
End Property

It’s in a module within an ASP.NET web app, so all members are shared/static by definition. My goal here is essentially laziness. I use connections everywhere, so it just made sense to have one property that is thread static so that it serves as a new instance per thread instead of dimming a new connection object every time I want to use it.

This seems to have been working until I decided to load the same page in two separate browsers. When I do this, an exception is thrown that states the connection object is already in use.

I read in a Microsoft article that instance types are not guaranteed to be thread safe. If this is the case, what can I do to ensure that this property and its field are thread safe?

Edit: What has be confused is that this code works within the page load event:

Dim Tasks As New List(Of Task)

Tasks.Add(Task.Factory.StartNew(Sub() ucEmployee.LoadData()))
Tasks.Add(Task.Factory.StartNew(Sub() ucSales.LoadData()))
Tasks.Add(Task.Factory.StartNew(Sub() ucServers.LoadData()))
Tasks.Add(Task.Factory.StartNew(Sub() ucApps.LoadData()))

Task.WaitAll(Tasks.ToArray())

Each one of those .LoadData() methods is executed in a separate thread and all of them reference my GlobalConnection property above. I initially wrote all that without the ThreadStatic attribute. After encountering errors, I made the GlobalConnection property ThreadStatic and the problem went away. When this rolls out into production, this web app will be used by multiple people. This is what prompted me to open the same page in two web browsers. I thought that would have been two separate threads right there, but perhaps I am wrong about that.

  • 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-27T02:48:05+00:00Added an answer on May 27, 2026 at 2:48 am

    You should have one connection per request instead of one per thread.

    To do that, store it in HttpContext.Current.Items instead of the ThreadStatic field.
    You should also close the connection in an EndRequest handler.

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

Sidebar

Related Questions

Here is an example: I write html code inside of textarea, then I swap
Here is a simplification of my database: Table: Property Fields: ID, Address Table: Quote
Here is my code, which takes two version identifiers in the form 1, 5,
Here is the code I'm using http://jsbin.com/evike5/edit When the jQuery UI dialog is fired
Here's some of my production code (I had to force line breaks): task =
Here is a great article about GC may occur at unexpected point of code
Here is my javascript code for a cursor focus function to go to username
Here is my code...I have two dimensional matrices A,B. I want to develop the
Here is my code sample, let me know if it can be further improved?
Here is my code (Say we have a single button on the page that

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.