Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

vb.net question about using db connections with threads

Posted on 2011-02-23
1
Medium Priority
?
300 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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 …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

885 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