Home  >  Article  >  Web Front-end  >  How to use transaction automatic recycling in mysql connection pool (with code)

How to use transaction automatic recycling in mysql connection pool (with code)

php中世界最好的语言
php中世界最好的语言Original
2018-04-12 17:53:452055browse

This time I will show you how to use automatic transaction recycling in mysql connection pool (with code). What are the precautions for mysql connection pool to use automatic transaction recycling (with code). The following is a practical case. Let’s take a look.

The example of this article describes how Node.js implements the mysql connection pool to use transactions to automatically recycle connections. 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();
}

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Customized ajax cross-domain component encapsulation

Why use Node.js for web application development

express multer implements the specific steps to upload node images

The above is the detailed content of How to use transaction automatic recycling in mysql connection pool (with code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
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