Node.js mySQL connection via singleton

I use a singleton template for mySQL connections in node.js, there is one and only one connection for the entire application to use, I am worried if there is some timeout for this connection contained in singleton.

This connection is expected to work throughout the life of the application. I searched and found persistence examples using the pool, but not sure if this applies to this example, there is no connection pool, there is only one connection that needs to be shared between the components, the question is, is there some kind of timeout, which will remove the connection after it lasts a long time?

puremvc.define( { name: "common.model.connections.App", constructor: function() { var mysql = require("mysql"); this.connection = mysql.createConnection({ host: common.Config.mySQLHost, user: common.Config.mySQLUsername, password: common.Config.mySQLPassword, database: common.Config.mySQLDatabase }); this.connection.connect(); } }, { }, { NAME: "App", instance: null, connection: null, getInstance: function() { if(common.model.connections.App.instance == null) { common.model.connections.App.instance = new common.model.connections.Fico(); } return common.model.connections.App.instance; }, getConnection: function() { return common.model.connections.App.getInstance().connection; } } ); 
+2
mysql connection-pooling
source share
1 answer

This is actually a MySQL problem, not Node.js. You can use the wait_timeout property for MySQL to increase the amount of time to maintain an open connection.

Read more about this ad here.

Below is an example code for your singleton that will create a new connection when the current one is closed. In the script, you will never have more than 1 active connection if you always use the Singleton application to get the connection:

 getConnection: function() { var app = common.model.connections.App.getInstance(); if(!app.connection.isConnected()){ //connect if connection is closed. app.connection.connect(); } return app.connection; } 
-2
source share

All Articles