Night Walker
Night Walker

Reputation: 21280

web methods and DB connections

If I open some DB connection in some global viable in one call of the web service's method and if concurrently at second call of this method will it see this instance in this global viable ? Are this resources shared or each call has it's own resources ?

Thanks

Upvotes: 2

Views: 259

Answers (3)

oleksii
oleksii

Reputation: 35925

For services it is better to use some kind of database connection management. Usually you can adapt Open/Close new connection on per-request basis. Note, most likely you will be working with logical connections and connection pool. Those are helpful to significantly reduce load to open physical connection. Physical connection is created without your direct control and is really heavy weight operation.

Do not put connection in a shared static variable, because connection is typically a disposable resource, which means you must dispose it. If something goes wrong and your connection to the database is corrupted then all your subsequent calls are deemed in doom.

Upvotes: 1

driis
driis

Reputation: 164331

Global variables tend to be just that, global. If your global variable is a C# static, it will be shared by webservice methods in the AppDomain. This is obviously error-prone - It is better, if each webservice method obtains a new connection when needed, and close it before the method finishes.

Upvotes: 3

arthur86
arthur86

Reputation: 531

Usually Web service use Http request. In this case it's possible that each call you have to define the object, because the service are stateless...

Upvotes: 1

Related Questions