Eliminate the need of connecting to database each time web service method is called

a1jit

Regular
Joined
Aug 19, 2005
Messages
89
Hi Guys,

I need some example code or guidance here.

Below is the description of my problem.
-------------------------------------

I have a web service with couples of web methods, example is like
1) getNames
2) getLectures
3) getClasses, etc..

So in each of this method, there is a statement which connects to database, meaning whenever user calls "getNames", it connects to db, then retrives the data, release the connection. Then if user calls "getLecturers" again, it connects to db, then retrieves the data again, and releases the connection again.

Im trying to eliminate the need of connecting to db for several times each time a web method is called

I plan to have two additional webmethods.
1) connect
2) disconnect

Meaning each time any developer wants to access this web service methods, they have to call the "connect" method, and then it will create a session of connection, and they can use a list of web methods, once they have finished, they call the "disconnect" web method.

Im not sure how to implement this, i want to only connect once and make the connection last for several minutes until its idle for certain time..

Really appreciate if someone can point out an example or reference on how can i implement this logic..

thank you very much..
 
a1jit said:
Im trying to eliminate the need of connecting to db for several times each time a web method is called

I plan to have two additional webmethods.
1) connect
2) disconnect
You do not want to do this. A .NET web service really does not lend itself very well to context sharing. It can be done by utilizing the Session but it could get ugly fast once you start talking about sharing database connections and such. Regardless, the SqlConnection class supports some fairly robust pooling. Here's some more info:

http://samples.gotdotnet.com/quickstart/howto/doc/adoplus/connectionpooling.aspx
 
Hi Gill
Thanks for the reply, but how its normally done in a normal situation.
Lets say we take a web application for an example, do me connect &
disconnect everytime we need to pullout something from the database?
Or we do it once during the login page and use it across the page until connection is idle for certain time?

Gill Bates said:
You do not want to do this. A .NET web service really does not lend itself very well to context sharing. It can be done by utilizing the Session but it could get ugly fast once you start talking about sharing database connections and such. Regardless, the SqlConnection class supports some fairly robust pooling. Here's some more info:

http://samples.gotdotnet.com/quickstart/howto/doc/adoplus/connectionpooling.aspx
 
Yep, you connect, query, and disconnect every time. The SqlConnection pool will manage everything for you under the hood. Although you are calling the Connect() method there may be an existing connection that is available in the pool. The framework will use that. If there's no connection available, it will create a new one as needed.
 
Back
Top