简体   繁体   English

如何在Node-MySQL连接池中正确处理连接持久性事物

[英]How to properly handle connection persistent things within node-mysql connection pooling

If I use node-mysql's connection pooling feature, wouldn't this pose a problem in my app because connections are re-used after connection.end() ? 如果我使用node-mysql的连接池功能,那么这不会在我的应用程序中引起问题,因为在connection.end()之后重新使用了connection.end() Here's why I'm concerned: When you define a variable in SQL, or start a transaction, the variable or transaction is persistent until the connection is closed. 这就是我担心的原因:当您在SQL中定义变量或启动事务时,该变量或事务将保持不变,直到关闭连接。 Since the connection is never actually closed, but instead re-used, the variables and transactions can seep into another routine that doesn't expect the variable or expect the transaction to exist; 由于连接从未真正关闭过,而是被重用了,因此变量和事务可以渗入另一个不希望变量或期望事务存在的例程。 it expects a fresh connection. 它期待一个新的连接。

Variables could pose a big problem; 变量可能会带来很大的问题; a routine could never safely assume a variable to be undefined because the connection with wear and tear. 例行程序永远无法安全地假设变量是不确定的,因为这种连接存在磨损。

Transactions could pose an even bigger issue if one routine were to ever fail to rollback or commit a transaction before calling end() . 如果一个例程在调用end()之前无法回滚或提交事务,则事务可能会带来更大的问题。 If the next routine that were to use this connection doesn't deal with transactions, then all queries would be appended to the transaction and halted, never to be executed. 如果要使用该连接的下一个例程不处理事务,则所有查询都将附加到该事务并暂停,从不执行。 I could just be careful when writing my app that such an error never occurs, but mistakes happen, and if it does happen, it'd be extremely difficult to debug (I wouldn't know which routine is mishandling connections, bad bad). 在编写应用程序时,我可能会小心翼翼地保证永远不会发生这样的错误,但是会发生错误,而且如果确实发生了,调试起来将非常困难(我不知道哪个例程会错误地处理连接,很糟糕)。

So these are some of my concerns when thinking about using pooling. 因此,这是我在考虑使用池时需要考虑的一些问题。 Surely I'm not the only person to have thought of these issues, so please shed some light on how to properly handle pooled connections, if you'd be so kind. 当然,我不是唯一想到过这些问题的人,因此,如果您愿意,请说明如何正确处理池化连接。 :) :)

Thank you very much. 非常感谢你。

All transactions will happen in the context of a single connection. 所有交易将在单个连接的上下文中发生。 After you end or terminate the connection, you can no longer operate on that transaction. 结束或终止连接后,您将无法再对该事务进行操作。 The general pattern is to open, query, close and unless you have a long running transaction, you won't have any problems of queries or variables bleeding over into other connections. 通常的模式是打开,查询,关闭,除非您有一个长期运行的事务,否则不会有任何查询或变量渗入其他连接的问题。

In the event of a long running transaction, you will have to manage that connection object to make sure it exists and is only used by code that operates within that transaction. 如果事务长时间运行,则必须管理该连接对象以确保它存在并且仅由在该事务内运行的代码使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM