MySQL Connecter - Is it most efficient to keep a connection open for the lifetime of the data access layer?
I have read that ASP.NET uses connection pooling to manage database connections so it is not inefficient to open and close database connections on a per request basis. I assume this for SQL Server. What about for MySQL using the .NET connecter?
I am building a D.A.L. and I am basically wondering if I should open and close db connections in each function or if I should kee开发者_StackOverflow社区p a connection open until the class is cleaned up and use Disposable/Finalize to ensure the db connection is closed.
Always go for pooling if you can - the largest cost in (simple) database access is always opening and closing a connection.
The NET Connector supports pooling. And take note:
it is best to let the connection pooling system manage all connections. You should not create a globally accessible instance of MySqlConnection and then manually open and close it. This interferes with the way the pooling works and can lead to unpredictable results or even exceptions
精彩评论