Why one Public OleDbConnection is deprecated? Alternative to solve the bug: too many connections opened

后端 未结 2 668
慢半拍i
慢半拍i 2020-12-06 13:43

I have to work with a Project made by another developer. A project Win-Form with Visual-Basic code, with MS-Access as db and some OleDbConnections. There is a bug: sometimes

2条回答
  •  悲哀的现实
    2020-12-06 14:05

    Just adding some information that works for years successfully for me (it is somewhat similar to what David-W-Fenton suggests)

    First, an OleDbConnection to Microsoft Access (MDB, JET) is not using connection pooling. As Microsoft states in KB191572:

    Connections that use the Jet OLE DB providers and ODBC drivers are not pooled because those providers and drivers do not support pooling.

    Regarding connection pooling, there is also this blog post from Ivan Mitev that states:

    So what does this mean? It is apparent that that the presence of an actively opened connection made the test with multiple connection closing and opening finish a lot faster (2-3 times). The only possible explanation for me is that the connection pool is released each time there are no active connections. I have to make further investigations and read something like Pooling in the Microsoft Data Access Components. Or maybe hold a single opened connection just for the sake of keeping the pool alive. This would be ugly, but still it is a good enough workaround! If anyone has a better idea, please share it.

    And Microsoft notes in MSDN:

    The ADO Connection object implicitly uses IDataInitialize. However, this means your application needs to keep at least one instance of a Connection object instantiated for each unique user—at all times. Otherwise, the pool will be destroyed when the last Connection object for that string is closed.

    Based on all this and my own tests, my solution to "simulate" connection pooling even with Microsoft Access databases roughly follows these steps:

    1. Open one OleDbConnection to the Access database as early as possible in application lifecycle.
    2. Do your normal SQL queries, disposing OleDbConnections as early as possible, just like recommended.
    3. Dispose that one always-open OleDbConnection as late as possible in application lifecycle.

    This sped up my applications (mostly WinForms) tremendously.

    Please note that this also works for Sqlite which seems to not support connection pooling, too.

提交回复
热议问题