PDO数据库连接问题

发布于 2024-09-03 12:32:36 字数 751 浏览 4 评论 0原文

一年多前,我创建了自己的数据库类,它使用 PDO,并处理所有准备、执行和关闭连接。到目前为止,这些课程一直运行良好。

我从两个不同的数据库服务器获取:MySQL 和 MS SQL Express。我正在从 MySQL 服务器检索员工 ID,并使用它从 MS SQL 服务器获取该员工信息。 MySQL 服务器大约有 11k 条记录,而我的程序仅通过 1200 条就崩溃并出现如下错误。

Connection failed (odbc:Driver=FreeTDS;Servername=MSSQLExpress;Database=SMDINC) Class (PDOException)
SQLSTATE[08001] SQLDriverConnect: 0 [unixODBC][FreeTDS][SQL Server]Unable to connect to data source

该程序似乎无法连接到数据源,但在此之前它运行了大约 30 次完全相同的查询并且没有任何问题。另外,我已经彻底检查了查询中的所有数据,一切看起来都很好。

我相信问题可能是创建了太多连接,但我尝试关闭许多不同地方的所有连接,但似乎没有任何方法可以解决问题。任何调试帮助或建议将不胜感激!

Craig Metrolis

更新

好的,我发现了问题,我在准备查询之后、执行之前使用了 closeCursor。我拿出 closeCursor ,它似乎已经解决了这个问题......但是为什么?这对我来说没有任何意义......

Over a year ago I created my own database classes which use PDO, and handle all preparing, executing, and closing connections. These classes have been working great up until now.

There are two different database severs I am grabbing from, MySQL, and MS SQL Express. I am retrieving an employee id from the MySQL server and using it to get that employees information from the MS SQL server. There are about 11k records coming from the MySQL server and my program is only making it through 1200 before crashing with an error like the following.

Connection failed (odbc:Driver=FreeTDS;Servername=MSSQLExpress;Database=SMDINC) Class (PDOException)
SQLSTATE[08001] SQLDriverConnect: 0 [unixODBC][FreeTDS][SQL Server]Unable to connect to data source

It seems like the program is not able to connect to the data source, but it is running the exact same query about 30 times before this and having no problem. Also, I have thoroughly checked all of the data coming into the query and it all looks fine.

I believe the issue may be that there are to many connections being created, but I have tried to close all connections in many different places, and nothing seems to be fixing the problem. Any debugging help, or suggestions would be appreciated!

Craig Metrolis

UPDATED

Ok, I found the problem, I was using closeCursor after preparing the query, and before the execute. I took out the closeCursor and it seems to have fixed this problem.... BUT WHY?? That does not make any sense to me.....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨离汐 2024-09-10 12:32:36

PDOStatement::closeCursor() 释放
与服务器的连接,以便
可能会发出其他 SQL 语句,
但将语句保留在状态中
这使得它能够再次执行。

换句话说,它正在关闭你与你的联系。

但这仍然无法解释为什么在问题发生之前它已经完成了 1200 条记录。

PDOStatement::closeCursor() frees up
the connection to the server so that
other SQL statements may be issued,
but leaves the statement in a state
that enables it to be executed again.

In other words, it was closing your connection on you.

That still doesn't explain why it got through 1200 records before the problem occurred, though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文