3

This is the typical way I create MySQL connection in node.js

var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'root',
  password : '',
  database : 'address_book'
});
var app = express();

connection.connect(function(err){
if(!err) {
    console.log("Database is connected ... nn");    
} else {
    console.log("Error connecting database ... nn");    
}
});

Is it good enough for production use? When should one use connection pooling? What are the advantages and disadvantages of using connection pooling?

1 Answers1

1

This will work but creating mysql connection for each and every request is not recommended.

So it would be better to have a pool of mysql connections, as cost of getting a connection will be nil compared to prior case.

Somil
  • 567
  • 5
  • 13
  • Is it correct to say using a connection pool will ALWAYS be better than my current approach of creating a connection for each and every request? –  Jul 06 '16 at 07:24
  • 1
    Yes , pool will always be a better option. However if you are using mysql transactions than you need to explicitly create a connection and release a connection after commit or rollback. – Somil Jul 06 '16 at 07:26