Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

这个库有没有做“事务”的处理啊? #454

Closed
yylol opened this issue Apr 12, 2013 · 5 comments
Closed

这个库有没有做“事务”的处理啊? #454

yylol opened this issue Apr 12, 2013 · 5 comments

Comments

@yylol
Copy link

yylol commented Apr 12, 2013

createPool是有可能公用链接的,会影响事务,不知道mysql本身有没有对“事务”的封装?

@dresende
Copy link
Collaborator

Sorry? Transactions? Please right your ticket in english.

@yylol
Copy link
Author

yylol commented Apr 15, 2013

yes,Transactions,api for Transactions is what?
I can't find 'connection.startTransaction' in the lastest source code.
How to white code when I need Transactions in my progrome?
can 'transaction' work with 'createPool'?

@imkira
Copy link
Collaborator

imkira commented Apr 15, 2013

yylol, as far as I know, you need to wrap your queries with BEGIN and COMMIT/ROLLBACK.

pool.getConnection(function(err, connection) {
  if (err) {
    // log error, whatever
    return;
  }

  // begin transaction
  connection.query('BEGIN', function(err, rows) {
    if (err) {
      connection.end();
      // log error, whatever
      return;
    }

    // execute your query or set of queries
    connection.query('UPDATE ...', function(err, rows) {
      if (err) {
        connection.query('ROLLBACK', connection.end);
        // log error, whatever
        return;
      }

      connection.query('COMMIT', connection.end);
    });
  });
});

Note that the code above is just sample code.
Please consider using an async library for avoiding such deeply nested code.

@yylol
Copy link
Author

yylol commented Apr 15, 2013

If I have more than one sql code, how can I code then?

@imkira
Copy link
Collaborator

imkira commented Apr 15, 2013

yylol, please consider using something like https://github.com/caolan/async

pool.getConnection(function(err, connection) {
  if (err) {
    // log error, whatever
    return;
  }

  // begin transaction
  connection.query('BEGIN', function(err, rows) {
    if (err) {
      connection.end();
      // log error, whatever
      return;
    }

    async.series([
        function(callback) {
          connection.query('INSERT ...', callback);
        },
        function(callback) {
          connection.query('UPDATE ...', callback);
        },
        function(callback) {
          connection.query('DELETE ...', callback);
        }
      ],
      function(err) {
        var q;
        if (err) {
          q = 'ROLLBACK';
          // log error, whatever
        }
        else {
          q = 'COMMIT';
        }
        connection.query(q, connection.end);
      });
    });
  });
});

@imkira imkira closed this as completed Apr 15, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants