Home > Web Front-end > JS Tutorial > body text

How to implement mysql transaction automatic recycling of connections in Node.js

亚连
Release: 2018-06-08 10:43:56
Original
1188 people have browsed it

This article mainly introduces the method of Node.js to implement mysql connection pool using transactions to automatically recycle connections. It analyzes the related techniques of node.js to operate mysql connection pool to implement transaction-based connection recycling operations in the form of examples. Friends who need it can Refer to the following

The example of this article describes how Node.js implements the mysql connection pool to automatically recycle connections using transactions. Share it with everyone for your reference, the details are as follows:

var mysql = require('mysql'),
  Connection = require('mysql/lib/Connection.js');
var pool = mysql.createPool({
  host: '127.0.0.1',
  database: 'myDB',
  port: 3306,
  user: 'root',
  password: 'root',
  debug: false,
  connectionLimit: 3
});
var execPool = function() {
  pool.getConnection(function(err, conn) {
    transAutoRelease(conn);
    conn.beginTransaction(function(err) {
      if (err) throw err;
      conn.query("INSERT INTO test(id,name,date,test) values(1,'123',now(),1)",
        function(err, ret) {
          if (err) {
            console.error(err);
            conn.rollback(function() {});
          } else {
            console.log(ret);
            conn.query('UPDATE test set id=12321312 where id=1', function(err, ret) {
              if (err) {
                console.error(err);
                conn.rollback(function() {
                });
              } else {
                conn.commit(function() {
                  console.log('success' + JSON.stringify(ret));
                });
              }
            });
          }
        });
    });
  });
}
function after(fn, cb) { return function() {
    fn.apply(this, arguments);
    cb();
  }
}
function transAutoRelease(conn) {
  if (conn.commit == Connection.prototype.commit)
    conn.commit = after(conn.commit, release);
  if (conn.rollback == Connection.prototype.rollback)
    conn.rollback = after(conn.rollback, release);
  function release() {
    if (conn) {
      conn.release();
    }
  }
}
var intervalStartProcess = function() {
  setInterval(function() {
    execPool();
  }, 1000);
}
for (var i = 5 - 1; i >= 0; i--) {
  intervalStartProcess();
}
Copy after login

The above is what I compiled for everyone, I hope it will be helpful to everyone in the future.

Related articles:

Why will Node.js become a web application development?

How to get the file upload progress in Node.js?

How to implement the longest common subsequence in javascript

The above is the detailed content of How to implement mysql transaction automatic recycling of connections in Node.js. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!