I am working on multi threaded application(a server) where I used to handle 2000 clients at a time and I am opening separate database connection of MySQL database in each thread. So I have enabled the connection pooling. I searched on many blocks that after using connections we should close it then it will return back to pool and will be used by other thread.
on the other hand we know that connection making is a time consuming process. So my question is why should we close connection in connection pooling. and what is better keep connection open or close them?
we know that connection making is a time consuming process
Correct - that's why we have connection pools. They maintain connections, so you don't create new ones.
why should we close connection in connection pooling
So they are returned to the pool to be used by other threads.
Connections are expensive resources, so you want to open, use and close them as quickly as possible, so they will return to the pool and be available to other threads.
When you 'Close' a connection that is pooled;You are saying that you are done with the connection and the pool can use it again.
Calling Close does not physically tear down the connection. The pool has its own logic to determine when connections are physically closed.
Related
I have some doubts related to connection pooling. In SQL Server Connection Pooling article it was mentioned like " When a new connection is opened, if the connection string is not an exact match to an existing pool, a new pool is created. Connections are pooled per process, per application domain, per connection string and when integrated security is used, per Windows identity."
Now i have my own windows form application which has SQL connection.
So when I open application the SQL connection open for first time and a pool is created. So if i close the application does pool gets destroyed automatically or will it be exists even after application is closed?
If I open the application again after some time does the connection drawn from the existing pool if already exists or not?(but it is mentioned like pool is per process)
There exists connection timeout for a connection. So is there any timeout for a pool too?
Connection pools are in the end nothing magical. Think of them as .NET objects that simply keep a list of connections.
As such these objects exist per AppDomain and live inside the CLR, which is bound to the lifetime of your process.
If your process goes away, so does the CLR instance, so do the AppDomains and so do the connection pools. Just like any other .NET object you had.
If you restart your application the connection pools get recreated, again based on the rules described by MSDN.
1) as you quoted the pool is pooled per process, so if you close your application it closes the pool.
2) Does not matter as the pool is per process, when you close the application it closes the pool.
3) Yes, it is set in the connection string with the key Connection Lifetime or Load Balance Timeout.
Connection Lifetime
-or- Load Balance Timeout
When a connection is returned to the pool, its creation time is
compared with the current time, and the connection is destroyed if
that time span (in seconds) exceeds the value specified by Connection
Lifetime. This is useful in clustered configurations to force load
balancing between a running server and a server just brought online. A
value of zero (0) causes pooled connections to have the maximum
connection timeout.
If you do not use the above setting (it defaults to 0) it will just use the default behavior of the pool
The connection pooler removes a connection from the pool after it has been idle for approximately 4-8 minutes, or if the pooler detects that the connection with the server has been severed.
The connection pooler removes a connection from the pool after it has been idle for approximately 4-8 minutes, or if the pooler detects that the connection with the server has been severed. Note that a severed connection can be detected only after attempting to communicate with the server. If a connection is found that is no longer connected to the server, it is marked as invalid. Invalid connections are removed from the connection pool only when they are closed or reclaimed.
If a connection exists to a server that has disappeared, this connection can be drawn from the pool even if the connection pooler has not detected the severed connection and marked it as invalid. This is the case because the overhead of checking that the connection is still valid would eliminate the benefits of having a pooler by causing another round trip to the server to occur. When this occurs, the first attempt to use the connection will detect that the connection has been severed, and an exception is thrown
refer this link Connection Pooling remove connections section
Pools are destroyed if there is no activity for a specified amount of time. More details on inactivity time can be found here. I believe this will answer #3 as well. However, if you specify the MinPoolSize in the connection string, it is destroyed when AppDomain is unloaded and process ends.
A pool is specific to process so each time a pool will be created.
In my C# application I connect to a MySQL database and run 10,000 queries. If I keep a connection to my database, these queries take roughly 14 seconds. However, if I rely on the connection pooling my queries take around 15 seconds. (I have run this test multiple times.)
// Connection pooling.
using (var connection = CreateConnection())
{
connection.ConnectionString = ConnectionString;
connection.Open();
Most samples on the net make use of the 'connect and close' construction above. However, it seems connection pooling is slower than keeping the connection. So the question is...
Q: Why should I use connection pooling?
Its a big debatable topic and would find many blog out there would tell that why we use Pool.
It will not slow things down. There is a lot of time spend on Connecting to DB Server and Hand shake and establishing communication between client and DB server.
So in multi request paradigm where many request are entertained by the server, it would be hard to establish and put on wait each client. POOL helps us that it gives us pre prepared connection and we use it and discard it. POOL get that connection and re-establish it for the next request.
But in a single threaded environment it is the other way around. POOL would be a very heavy resource for a single threaded env.
Q: Why should I use connection pooling?
Usually so that you can use more than one connection at a time. This is clearly important for web applications - you wouldn't want one user query to have to wait for another user's query to finish.
If you're writing a thick client application which talks straight to the database and you know you'll only ever have one query executing at a time, it's less important - but it's still global state, and that tends to be something you should avoid. You're doing several independent things - why would you want to constrain them to use the same connection?
Connection pooling is great for scalability - if you have 100 threads/clients/end-users, each of which need to talk to the database, you don't want them all to have a dedicated connection open to the database (connections are expensive resources), but rather to share connections (via pooling).
The using mini-pattern is also great for ensuring the connection is closed in a timely fashion which will end any transactions on the connection and thus ensure any locks taken by the transactions are released. This can be a great help for performance, and for minimising the potential for deadlocks.
If all your application does is run the 10,000 queries and then close again without any user interaction then it's fine to use one single connection.
However it's generally not a good idea to keep a database connection open while your application is just sitting there waiting for user input. This is where connection pooling is appropriate.
Pseudo code ...
<open connection>
<fetch data>
<close connection>
<user interaction with data ...>
<open connection>
<save updated data>
<close connection>
Depending on the language / database used, the second connection will be generated from the connection pool.
Suppose that i have an application that connects to a sql server, and this application connects to this server on the start of the application and close this connection on the exit of the application,i would like to know if any one can use this connection to connect to sql server
A single database connection will be restricted to the process that owns it. External applications will only have access to this connection via whatever API your application exposes.
However, inside that application, "connection pooling" means that different SqlConnection instances may all resolve to the same underlying unmanaged connection, as long as they don't overlap. More likely, repeated SqlConnection usage (different SqlConnection instances) will result in a low number of underlying connections.
I don't completely understand your question, however, connecting to a sql server on start and closing it on exit is a bad practice:
Assuming you just use a regular SqlConnection .net will create its own internal connection pool. Keeping it open the entire time could cause unwanted problems, for example by locking
I don't know what kind of error is this.. I can't open my site anymore
Server Error in '/site' Application.
Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.
please help me.. tnx
10 Tips for Writing High-Performance Web Applications
Connection Pooling
Setting up the TCP connection between your Web application and SQL Serverâ„¢ can be an expensive operation. Developers at Microsoft have been able to take advantage of connection pooling for some time now, allowing them to reuse connections to the database.
Always close your connections when you're finished with them. Do not trust the common language runtime (CLR) to clean up and close your connection for you at a predetermined time. The CLR will eventually destroy the class and force the connection closed, but you have no guarantee when the garbage collection on the object will actually happen.
To use connection pooling optimally, there are a couple of rules to live by. First, open the connection, do the work, and then close the connection. It's okay to open and close the connection multiple times on each request if you have to (optimally you apply Tip 1) rather than keeping the connection open and passing it around through different methods. Second, use the same connection string (and the same thread identity if you're using integrated authentication). If you don't use the same connection string, for example customizing the connection string based on the logged-in user, you won't get the same optimization value provided by connection pooling. And if you use integrated authentication while impersonating a large set of users, your pooling will also be much less effective. The .NET CLR data performance counters can be very useful when attempting to track down any performance issues that are related to connection pooling.
Whenever your application is connecting to a resource, such as a database, running in another process, you should optimize by focusing on the time spent connecting to the resource, the time spent sending or retrieving data, and the number of round-trips. Optimizing any kind of process hop in your application is the first place to start to achieve better performance.
A timeout expired (something took longer than it should). Specifically, the timeout period elapsed prior to obtaining a connection from the connection pool. It turns out This may have occurred because all pooled connections were in use and max pool size was reached.
(You're using Connection Pooling and probably not closing your connections. After you are done with a SqlConnection or similar connection object, call .Close() on it)
I've got a c# WINDOWS Application that is multi-threaded. It is my understanding that in a web environment, connections are pooled automatically. It is also my understanding that in a Windows app, this is not the case. Therefore, for a Windows app, the same connection should be used and not closed after each call, but instead closed when the app shuts down.
I'm curious though - is my correct? If it is, can two threads use the same connection to get a dataset from the DB at the same time or is that functionality queued up?
Thanks
The Connection Pooling is one feature of ADO.NET. Therefore the connections are already pooled. Not only in the web environment.
http://www.ondotnet.com/pub/a/dotnet/2004/02/09/connpool.html
It is my understanding that in a web
environment, connections are pooled
automatically. It is also my
understanding that in a Windows app,
this is not the case.
No, this is wrong, as m3rLinEz pointed out. Connections are always pooled.
Therefore, for a Windows app, the same
connection should be used and not
closed after each call, but instead
closed when the app shuts down.
You could keep a connection open for the duration of the application in a monolithic WinForms app. But it's better to use the standard pattern of opening/closing connections whenever you need them. Connection pooling means you won't notice a performance difference. And your data access code will be compatible with server applications such as ASP.NET.
If it is, can two threads use the same
connection to get a dataset from the
DB at the same time or is that
functionality queued up?
No. The ADO.NET classes (connection, command etc) are not thread-safe and should not be shared between threads without synchronisation. But as noted above, you should prefer the standard pattern for data access.
ok - so this assumption of mine was brought on by observation: When I tried a win app setup in the typical pool fashion, I always experience a 3-5 second delay while a real connection is established to the remote server. Even when I did an open, then a close, the next query would always have this delay.
When the server connects, it obviously doesn't establish a connection for each connection in the pool. Also, is the pooling mechanism smart enough to grab a connection that it knows is already open or is it possible for it to simply grab any random connection?
What is the default max connections in the pool?