I have a Web Service that requires an SqlConnection. There are 5 web methods that use this SqlConnection, but presently they are declared (locally) in each of the methods. Would it be acceptable to make it a global variable in this case?
Thanks
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No.
According to the documentation for SqlConnection:
Since your different web methods could be called simultaneously from different threads, you’d better not share the same connection instance between them.
The typical pattern is that every time you need a database connection, you create a new instance, and Dispose it as soon as you’re done. Threading is one of the reasons. Connection pooling makes sure it’s not too expensive to create all those connection instances.