Solved

vb.net question about using db connections with threads

Posted on 2011-02-23
1
292 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 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

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now