数据库驱动中的连接池设置导致的怪问题

程序运行过程中如检测到数据库连接已断开,将重新进行连接,代码中是对 Connection 先 Close 再 Open,但奇怪地是明明已经把数据库服务器停掉了,Open 操作仍然成功(未抛出异常),更奇怪的是有几次单步跟踪时就没这个问题。。。然后注意到单步跟踪时如果在 Close 后多等一会再 Open 就正常,才想到可能是连接池之类的因素在里头起作用。程序通过 ADO 连接 MySQL,起作用的应该是 Microsoft OLE DB Provider for ODBC Drivers,然后查到这个

Connection is not closed in given scenario due to using one of pooling types – oledb
resource pooling or odbc connection pooling.

Pooling can be disabled if necessary in 2 stages:
1. Disable oledb resource pooling for MSDASQL provider (OLEDB provider for ODBC drivers).
Set
HKEY_LOCAL_MACHINESOFTWAREClassesCLSID{c8b522cb-5cf3-11ce-ade5-00aa0044773d}OLEDB_SER
VICES equal to 0xfffffffe.
After this pooling is totally controlled on ODBC level.
2. To disable ODBC connection pooling, open ODBC Data Source Manager (odbcad32.exe). Go to
Connection pooling tab. Double click a driver in the list and disable pooling.

连接池对我的程序来说没啥意义,就照着说明把注册表里的键值改为 0xfffffffe,果然 ok 了!如果不改注册表,单从程序代码上应该也有办法解决,有空再研究吧。

This entry was posted in 软件开发 and tagged , . Bookmark the permalink.

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s