Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

SQL connection in threading

Posted on 2004-10-11
7
Medium Priority
?
176 Views
Last Modified: 2010-04-23
Hi all,

i'm trying to make a program with multiple threads that do some work then insert records into my SQL 2000 . should i declare SQLconnection1 as shared, or private to each thread ? what do i have to look out for when working with databases and threads ?
0
Comment
Question by:shenlong_wc
  • 4
  • 3
7 Comments
 
LVL 27

Expert Comment

by:Dabas
ID: 12283545
Hi shenlong_wc,
I suggest you share the SQLConnection and within the thread just check its state and see if it needs opening.

Dabas
0
 

Author Comment

by:shenlong_wc
ID: 12283580
is it ok if i declare it private shared to the whole class ? (this classes might be part of a thred too )

it'll be used by some subs that are multi-threaded and some which are not.

other than checking if it's open, is there anything i should be aware of ?
0
 
LVL 27

Accepted Solution

by:
Dabas earned 200 total points
ID: 12283595
shenlong_wc,
Without seeing your class structure, it is hard for me to answer that.
But essentially you should try to limit your connection to a minimum. If you have various instances of the same class, then it would not make sense to declare it within the class. It would cause the same connection to be open many times.
In that case you should consider holding your connection at application level instead of at class level.

Dabas
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:shenlong_wc
ID: 12284120
code's kinda long , kinda difficult to post it here

how about if i create only 1 SQLconnection for the entire project then pass it by ref to all the methods that need it ? or maybe even declare it as a global variable ? I will open it when i start the app and close it when i exit the app. in between, it'll be free for all the threads , objects , sub etc to use, and they won't have to worry about opening or closing it ?

is that a good idea ?

sorry, my connection pooling concepts are kinda weak .
0
 
LVL 27

Expert Comment

by:Dabas
ID: 12284173
Sounds like the way to go, yes!
0
 

Author Comment

by:shenlong_wc
ID: 12284185
thanks !
0
 

Author Comment

by:shenlong_wc
ID: 12355593
Hi there,

the problem didn't seem to go away ...

if i shared my OleDbCommand and OleDbConnection  as a global, sometimes my threads scream something about the OleDbCommand still open or fecthing some data .

if i shared only my OleDbConnection as a global, i get errors saying that it still has open datareaders on it .

what should i do ? short of creating a new OleDbCommand and OleDbConnection everywhere , that is . how can i make my OleDbCommand thread safe ?
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

810 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