Solved

vb.net question about using db connections with threads

Posted on 2011-02-23
1
291 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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 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

11 Experts available now in Live!

Get 1:1 Help Now