Skip to content

Commit

Permalink
0.1.0_tested
Browse files Browse the repository at this point in the history
commit, rollback, timeout, fatal error tested with set and chain method
  • Loading branch information
HBYoon committed Sep 11, 2013
1 parent 8466e10 commit ba48320
Show file tree
Hide file tree
Showing 7 changed files with 220 additions and 427 deletions.
54 changes: 31 additions & 23 deletions README.md
Expand Up @@ -12,16 +12,17 @@ npm install node-mysql-transaction
```


Make transaction connection
Make transaction object
---
```
// tested mysql2.0.0-alpha7
var mysql = require('mysql');
var transaction = require('node-mysql-transaction');
var transaction = require('node-mysql-transaction');
var trCon = transaction({
// mysql driver set
connection: [mysql.createConnection,{
// mysql connection config
user: ...,
password: ...,
database: ...,
Expand All @@ -35,7 +36,7 @@ var trCon = transaction({
dynamicConnection: 32,
// set dynamicConnection soft removing time.
idleConnectionCutoffTime: 600,
idleConnectionCutoffTime: 1000,
// auto timeout rollback time in ms
// turn off is 0
Expand Down Expand Up @@ -203,7 +204,7 @@ trCon.set(function(err, safeCon){
return console.log(err);
}
safeCon.on('commit', function(){
console.log('79 / 71 commit, after several event loop');
console.log('commit');
});
safeCon.on('rollback', function(err){
console.log(err);
Expand All @@ -216,10 +217,10 @@ trCon.set(function(err, safeCon){
if (err) {
safeCon.rollback(err);
}
// .set transaction can work after several event loop.
// if you forget commit or rollback,
// and no timeout setting, connection will be leak.
safeCon.commit();
// .set transaction can work after several event loop.
// if you forget commit or rollback,
// and no timeout setting, connection will be leak.
safeCon.commit();
});
});
});
Expand All @@ -233,37 +234,42 @@ test.set(function(err, safeCon){
return console.log(err);
}
safeCon.on('commit', function(){
console.log('23731 commit!');
console.log('commit!');
}).
on('rollback', function(err){
console.log('23731 rollback');
console.log('rollback');
console.log(err);
});
var insertNumber = 2;
var resultCount = 0;
reqQuery1 = safeCon.query('insert ......', [...]);
reqQuery2 = safeCon.query('insert ......', [...]);
function resultOn (result) {
resultCount += 1;
if (resultCount === insertNumber) {
safeCon.commit();
}
};
reqQuery1.
on('result', function(result){
console.log('23731: ' + result.insertId);
}).
on('error', function(err){
safeCon.rollback(err);
});
on('result', resultOn).
on('error', safeCon.rollback.bind(safeCon));
reqQuery2.
on('result', function(result){
safeCon.commit()
}).
on('error', function(err){
on('result', resultOn).
on('error', function(err){
// safeCon.rollback.bind(safeCon) doing same work of this function.
safeCon.rollback(err);
});
});
```

###top level error handling

every fatal connection error and basic transaction query(START TRANSACTION, COMMIT, ROLLBACK) error and request queue's type error will bubble to the top transaction object. this bubbled error will link to the current transaction work on failed connections too, if it possible.
Every fatal connection error and basic transaction query(START TRANSACTION, COMMIT, ROLLBACK) error and request queue's type error will bubble to the top transaction object. This bubbled error will link to the current transaction work on failed connections too, if it possible.

```
var transaction = require('node-mysql-transaction');
Expand All @@ -282,6 +288,8 @@ trCon.on('error', function(err){
});
```

If you don't set listener for top level error, the top level error is bubbled to the process and kill the process. For internal error recovery (simply, it will remove failed connection and create new one for next transaction request), you must take top level error event before.

###Terminating

Call end method. Method sending error to all callback function in the queue and connection terminating after current transaction finished.
Expand Down
4 changes: 2 additions & 2 deletions lib/chain.js
Expand Up @@ -78,10 +78,10 @@ function commitLoop (safeCon) {
if (safeCon._count === 0){
return safeCon.commit.bind(safeCon)()
}
return setImmediate(commitLoop.bind(null,safeCon));
return setImmediate(commitLoop.bind(null, safeCon));
};

// rollback dosen't need any safety timer
// rollback doesn't need any safety timer
function rollbackFacroty (safeCon) {
return function(err){
safeCon._count = 0;
Expand Down
107 changes: 74 additions & 33 deletions test/chain_loop.js
@@ -1,6 +1,8 @@
// transaction test
// tested with mysql 2.0 alpha

// 0.1.0 pass 130911(yy/mm/dd)

// test table query -> CREATE TABLE `transaction_test` (`num` INT(10) NULL AUTO_INCREMENT, `test` INT(10) NULL DEFAULT '0',PRIMARY KEY (`num`))COLLATE='latin1_swedish_ci'ENGINE=InnoDB;

var mysql = require('mysql');
Expand All @@ -10,56 +12,66 @@ var test = transaction({
connection: [mysql.createConnection,{
user: 'test',
password: 'test',
database: 'test'
database: 'test',
// for error handler test
// port:9090
}],
staticConnection:0,
staticConnection: 0,

dynamicConnection:64,
dynamicConnection: 32,
idleConnectionCutoffTime: 100,
// auto time out rollback in ms
timeOut:600
}).
on('error', function(err){
console.log('top level error');
console.error(err);
});

var number = 0;
var to = 10000;

// don't use this test for compare of set_loop test.
// this test has 3 times insert query in each round.

// /* //<<<<<<<<<<<<<block

// chain transaction API test
console.time('test');
var number = 0;
var to = 10000;


var numberTo = 0;

for (var i = 0; i < to; i+=1) {
(function(){
var num = number+=1;

var chain = test.chain();

chain.
on('commit', function(){
if (num === to) {
console.timeEnd('test');
}
}).
on('rollback', function(err){
if (num === to) {
console.timeEnd('test');
}
});


chain.
// query chaining is important and most heavy process in a chain method.
query('insert transaction_test set test=?',[num]).
query('insert transaction_test set test=?',[num]).
query('insert transaction_test set test=?',[num]).
on('result', function(result){
if (num%2) {
// I don't want any odd number!
chain.rollback();
}
});
var num = number+=1;
var chain = test.chain();
chain.
on('commit', function(){
if (num === to) {
console.timeEnd('test');
}
}).
on('rollback', function(err){
if (num === to) {
console.timeEnd('test');
}
});

chain.
// query chaining is important and most heavy process in a chain method.
query('insert transaction_test set test=?',[num]).
query('insert transaction_test set test=?',[num]).
query('insert transaction_test set test=?',[num]).
on('result', function(result){
if (num%2) {
// I don't want any odd number!
chain.rollback();
}
});
})();
}

Expand All @@ -71,4 +83,33 @@ for (var i = 0; i < to; i+=1) {
// 16 -> 88xx ~ 89xx ms
// 8 -> 119xx ~ 120xx ms

// */


/* //<<<<<<<<<<<<<block
var testCon = mysql.createConnection({
user: 'test',
password: 'test',
database: 'test'
});
var count = 0;
// odd number test
for (var i = 0; i < (to * 3); i+=1) {
testCon.query('select test from transaction_test where num=?',[i+1],function(err,result){
if (result) {
if (result[0]) {
if (result[0].test) {
if (result[0].test%2) {
console.log('odd number!!!!!! '+result[0].test);
}
}
}
}
count += 1;
if (count === (to * 3)) {
console.log('test end');
}
});
};
// */
50 changes: 26 additions & 24 deletions test/chain_test.js
@@ -1,6 +1,8 @@
// transaction test
// tested with mysql 2.0 alpha

// 0.1.0 pass 130911(yy/mm/dd)

// test table query -> CREATE TABLE `transaction_test` (`num` INT(10) NULL AUTO_INCREMENT, `test` INT(10) NULL DEFAULT '0',PRIMARY KEY (`num`))COLLATE='latin1_swedish_ci'ENGINE=InnoDB;

var mysql = require('mysql');
Expand All @@ -24,6 +26,8 @@ var test = transaction({
});


// result vaule -> 1 ~ 7 / 19 ~ 29 /

// /* //<<<<<<<<<<<<<block

// chain transaction API test
Expand Down Expand Up @@ -51,16 +55,12 @@ on('result', function(result){
query('insert transaction_test set test=?',[3]).
on('result',function(result){
console.log('3: '+result.insertId);
// chain rush
// auto commit on
chain.
query('insert transaction_test set test=?',[result.insertId]).
query('insert transaction_test set test=?',[7]).
query('insert transaction_test set test=?',[8]).
query('insert transaction_test set test=?',[9]).
on('result', function(){
chain.commit();
}).
autoCommit(false);
query('insert transaction_test set test=?',[4]).
query('insert transaction_test set test=?',[5]).
query('insert transaction_test set test=?',[6]).
query('insert transaction_test set test=?',[7]);

// each chain set it's own auto commit function if you did not set auto commit off
// so must need auto commit off for make chain stream to after event loop
Expand All @@ -78,19 +78,20 @@ on('rollback', function(err){
console.log('chain2 rollback');
console.log(err);
}).
query('insert transaction_test set test=?',[10]).
query('insert transaction_test set test=?',[8]).
on('result', function(result){
chain2.
query('insert transaction_test set test=?',[11]).
query('insert transaction_test set test=?',[12]).
query('insert transaction_test set test=?',[9]).
query('insert transaction_test set test=?',[10]).
on('result',function(result){
// chain with error
chain2.
query('insert transaction_test set test=?',['err']).
query('insert transaction_test set test=?',[14])
query('insert transaction_test set test=?',[12])
}).autoCommit(false);
}).autoCommit(false);

// timeout test
var chain3 = test.chain();
chain3.
on('commit', function(){
Expand All @@ -99,12 +100,13 @@ on('commit', function(){
on('rollback', function(err){
console.log(err);
}).
query('insert transaction_test set test=?',[13]).
query('insert transaction_test set test=?',[14]).
query('insert transaction_test set test=?',[15]).
query('insert transaction_test set test=?',[16]).
query('insert transaction_test set test=?',[17]).
query('insert transaction_test set test=?',[18]).
query('insert transaction_test set test=?',[19]).
query('insert transaction_test set test=?',[20]);
autoCommit(false);

// transaction chain with loop!!!
var chain4 = test.chain();
Expand All @@ -114,12 +116,11 @@ on('commit', function(){
}).
on('rollback', function(err){
console.log(err);
}).
setMaxListeners(0);
});

for(var i = 0; i < 5; i+=1) {
for(var i = 19; i < 30; i+=1) {
// working good :)
chain4.query('insert transaction_test set test=?',[i*100]);
chain4.query('insert transaction_test set test=?',[i]);
}

var chain5 = test.chain();
Expand All @@ -130,12 +131,13 @@ on('commit', function(){
on('rollback', function(err){
console.log('chain5 rollback');
console.log(err);
}).
setMaxListeners(0);
});

for(var i = 0; i < 30; i+=1) {
if (i===8) { i='error maker' }
chain5.query('insert transaction_test set test=?',[i*10000]);
var k
for(var i = 30; i < 90; i+=1) {
k = i;
if (i===88) { k='error maker' }
chain5.query('insert transaction_test set test=?',[k]);
}

// */

0 comments on commit ba48320

Please sign in to comment.