Solved

vb.net question about using db connections with threads

Posted on 2011-02-23
1
297 Views
Last Modified: 2012-05-11
If I have a simple class

public class MyClass
 
       private myConn as new sqlconnection
       private MyThreadPool(5) as Thread

       public sub New()
          dim x as integer
          for x = 0 to 4
               MyThreadPool(x) = New THread(Addressof MyFunction)
               MyThreadPool(x).Start
           next
        end sub

        private sub MyFunction()
              'Do some work in db
         end sub
end class


Is it safe for all of my threads to use the single instance declared at the class level or do I need to declare one within the MyFunction so each thread has its own instance?

Also, should the connection be opened and closed constantly or just opened once and left open while the thread runs?  The threads will pretty much run constantly.

0
Comment
Question by:rutledgj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 18

Accepted Solution

by:
carlnorrbom earned 500 total points
ID: 34968818
Hi,

You should be able to share the instance cross the threads. I would suggest doing a check in the MyFunction similar to:
    Private Sub MyFunction()
        If Not myConn.State = Data.ConnectionState.Open Then
            myConn.Open()
        End If
        Try
            'Do some work in db
        Catch ex As Exception
            'Manage exception
        Finally
            'Clean up
        End Try
    End Sub

Open in new window


Also, not sure what happens if you were to call "MyConn.Close()" inside MyFunction while another thread was still executing so wouldn't recommend that.

/Carl.
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question