Nodejs from 0 to 1 (MySQL)

Source: Internet
Author: User

1. Create a connection pool:

Why was it created? Because the database is expensive to create connections to the server each time, if a lot of people want to connect to the database, each user creates a connection and the performance is greatly degraded, so create a connection pool. In a server application, you typically create and maintain a connection pool for multiple databases. When the connection is no longer needed, these connections can be slowed down to the connection pool, and when the next client request is received, the connection is removed from the connection pool and re-utilized without establishing a new connection.

1.1 Syntax: Create CreatePool method:

var Pool=mysql.createpool ({

1 {2     ' localhost ' , 3     User ' Root ' , 4     ' 123456 ' , 5     Database ' TestDB ' 6 }

});

1.2 Remove the connection from the connection pool. Getconnection method.

1 pool.getconnection (function  (err, connection) {2         function  ( Err, rows) {3            callback (err, rows); 4             Connection.release (); // Release link   5                }); 6     });

1.3 When the connection is not in use, it is returned to the connection pool with the release method of the Connection object. connection.release ();//Release link

1.4 Remove a connection from the connection pool, using the destroy of the connection object. Connection.destroy ();

1.5 When a connection pool is no longer in use, close the connection pool with the end method of the connection pool object. pool.end ();

Content reference: http://blog.csdn.net/qq_31280709/article/details/52297957

Nodejs from 0 to 1 (MySQL)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.