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

0.6.2 sqlite transactions don't work with update #298

Closed
koskimas opened this Issue Jun 5, 2014 · 2 comments

Comments

Projects
None yet
2 participants
@koskimas
Collaborator

koskimas commented Jun 5, 2014

The code below works perfectly on knex 0.5.x but fails on 0.6.2.

var knex = require('Knex');
var Promise = require('bluebird');

var config = {
  debug: false,
  client: 'sqlite3',
  connection: {
    filename: './test.db'
  }
};

var db = knex.initialize(config);

db.schema.createTable('table', function(table) {

  table.integer('id').primary();
  table.string('column');

}).then(function() {

  return db.transaction(function(trx) {
    var promises = [];
    for (var i = 0; i < 10000; ++i) {
      promises.push(db('table').transacting(trx).insert({id: i, column: 'stuff ' + i}));
    }
    return Promise.all(promises).then(trx.commit, trx.rollback);
  });

}).then(function() {

  return db.transaction(function(trx) {
    var promises = [];
    for (var i = 0; i < 10000; ++i) {
      promises.push(db('table').transacting(trx).where({id: i}).update({column: 'new stuff ' + i}));
    }
    return Promise.all(promises).then(trx.commit, trx.rollback);
  });

}).then(function() {
  console.log('done');
});

The error is usually something like this

Possibly unhandled Error: Release called multiple times on the same object
    at Object.Pool.release (node_modules/Knex/node_modules/generic-pool-redux/pool.js:126:32)
    at Pool_SQLite3.Pool.release (node_modules/Knex/lib/pool.js:58:27)
    at node_modules/Knex/lib/client.js:39:10
    at tryCatch2 (node_modules/Knex/node_modules/bluebird/js/main/util.js:73:19)
    at Promise$_resolveFromResolver [as _resolveFromResolver] (node_modules/Knex/node_modules/bluebird/js/main/promise.js:595:13)
    at new Promise (node_modules/Knex/node_modules/bluebird/js/main/promise.js:87:37)
    at Client_SQLite3.Client.releaseConnection (node_modules/Knex/lib/client.js:38:10)
    at Runner_SQLite3.<anonymous> (node_modules/Knex/lib/runner.js:207:24)
    at tryCatch1 (node_modules/bluebird/js/main/util.js:63:19)
    at Runner_SQLite3.Promise$_method [as cleanupConnection] (node_modules/Knex/node_modules/bluebird/js/main/promise.js:266:25)

But sometimes the following error is also shown:

Possibly unhandled Error: SQLITE_ERROR: cannot commit - no transaction is active
@tgriesser

This comment has been minimized.

Owner

tgriesser commented Jun 5, 2014

So actually, if you remove the .then(trx.commit, trx.rollback); it should work.

If you return a promise within the transaction block, it now uses the result of that promise to call commit and rollback for you automatically. I should probably document this better in the change log.

@tgriesser tgriesser added the change label Jun 5, 2014

@koskimas

This comment has been minimized.

Collaborator

koskimas commented Jun 6, 2014

Thanks. I don't even know why I was returning the promise in the first place :D

I still have one problem with the transactions that didn't happen in 0.5.x, but is happening in 0.6.2. Returning null or undefined as a result from a transaction resolves to an empty array outside the transaction. See the example below.

var knex = require('knex');
var Promise = require('bluebird');

var config = {
  client : 'sqlite3',
  connection : {
    filename: 'test.db'
  }
};

var db = knex(config);

db.transaction(function(trx) {
  return Promise.resolve(null);
}).then(function(result) {
  console.log(result); // --> Prints []
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment