简体   繁体   中英

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() ? 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. 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() . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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