By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,372 Members | 1,333 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,372 IT Pros & Developers. It's quick & easy.

What is best SQLConnection strategy

P: n/a
hi
I would like to know whats better---
To declare a SQLConnection variable as a Static member of
a Class containing functions to MakeConnection and
CloseConnection, or should I just declare it as a Public
member of the above mentioned class containing Connection
code.
I ask this because I have read in MS documents that-----
"Any public static (Shared in Visual Basic) members of
SqlConnection type are safe for multithreaded operations.
Any instance members are not guaranteed to be thread safe."
Does that mean if SqlConnection object is a non static
member of a class, it will not be able to take advantage
of Connection Pooling?
-lenin

Nov 15 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hi Lenin,

I would say that you should share only connection string.
You should create a connection instance only when you need it, open
immediately before you need it and close it asap.

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

"Lenin Vyas" <le*******@rediffmail.com> wrote in message
news:07****************************@phx.gbl...
hi
I would like to know whats better---
To declare a SQLConnection variable as a Static member of
a Class containing functions to MakeConnection and
CloseConnection, or should I just declare it as a Public
member of the above mentioned class containing Connection
code.
I ask this because I have read in MS documents that-----
"Any public static (Shared in Visual Basic) members of
SqlConnection type are safe for multithreaded operations.
Any instance members are not guaranteed to be thread safe."
Does that mean if SqlConnection object is a non static
member of a class, it will not be able to take advantage
of Connection Pooling?
-lenin

Nov 15 '05 #2

P: n/a
Lenin Vyas <le*******@rediffmail.com> wrote:
I would like to know whats better---
To declare a SQLConnection variable as a Static member of
a Class containing functions to MakeConnection and
CloseConnection, or should I just declare it as a Public
member of the above mentioned class containing Connection
code.
I ask this because I have read in MS documents that-----
"Any public static (Shared in Visual Basic) members of
SqlConnection type are safe for multithreaded operations.
Any instance members are not guaranteed to be thread safe."
Does that mean if SqlConnection object is a non static
member of a class, it will not be able to take advantage
of Connection Pooling?


The statement about SqlConnection's thread-safety has nothing to do
with where the variable is declared - it's talking about instance
members *of SqlConnection* and static members *of SqlConnection*.

Aside from that, I agree with Miha's post.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3

P: n/a
hi
Thanks for the above answers.
Now, my next question is----
is it a good strategy to put my connection code in a seperate class file
and DataManipulation code in seperate class?. This will help me to keep
my connection code in one place and whenever i want to open connection i
just need to create an object of let's say clsConnection class and then
create an object of the class containing the DataManipulation code. This
obviously increases code resuability, but it also involves an extra
object creation overhead when manipulating data.
Instead of the above, can i simply put my connection code in the same
class which contains DataManipulation code? This reduces object
overheads but certainly involves redundancy for each DataManipulation
class.
Please reply.
-lenin
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #4

P: n/a
Lenin Vyas <le*******@rediffmail.com> wrote:
Thanks for the above answers.
Now, my next question is----
is it a good strategy to put my connection code in a seperate class file
and DataManipulation code in seperate class?. This will help me to keep
my connection code in one place and whenever i want to open connection i
just need to create an object of let's say clsConnection class and then
create an object of the class containing the DataManipulation code. This
obviously increases code resuability, but it also involves an extra
object creation overhead when manipulating data.
Instead of the above, can i simply put my connection code in the same
class which contains DataManipulation code? This reduces object
overheads but certainly involves redundancy for each DataManipulation
class.


I would suggest that if you really need a separate class for
connection-handling, it should probably just have a static method which
takes some parameters and returns a SqlConnection (or whatever).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.