Browse files

0.1.0_tested

commit, rollback, timeout, fatal error tested with set and chain method
  • Loading branch information...
1 parent 8466e10 commit ba483209cf12f719c4c76c695c9ebb90732c2b6a @HBYoon committed Sep 11, 2013
Showing with 220 additions and 427 deletions.
  1. +31 −23 README.md
  2. +2 −2 lib/chain.js
  3. +74 −33 test/chain_loop.js
  4. +26 −24 test/chain_test.js
  5. +17 −5 test/set_loop.js
  6. +70 −28 test/set_test.js
  7. +0 −312 test/test.js
View
54 README.md
@@ -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: ...,
@@ -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
@@ -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);
@@ -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();
});
});
});
@@ -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');
@@ -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.
View
4 lib/chain.js
@@ -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;
View
107 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');
@@ -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();
+ }
+ });
})();
}
@@ -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');
+ }
+ });
+};
// */
View
50 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');
@@ -24,6 +26,8 @@ var test = transaction({
});
+// result vaule -> 1 ~ 7 / 19 ~ 29 /
+
// /* //<<<<<<<<<<<<<block
// chain transaction API test
@@ -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
@@ -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(){
@@ -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();
@@ -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();
@@ -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]);
}
// */
View
22 test/set_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');
@@ -25,11 +27,13 @@ on('error', function(err){
console.error(err);
});
-// /* //<<<<<<<<<<<<<block
-
// simple loop test
var number = 0;
var to = 10000;
+
+// /* //<<<<<<<<<<<<<block
+
+
console.time('test');
for (var i = 0; i < to; i+=1) {
// transaction set, most low level queue method
@@ -39,7 +43,6 @@ for (var i = 0; i < to; i+=1) {
if (!(num%2)) {
// I don't want any odd number!
safeCon.commit();
-
}
// when after commit...oops!
@@ -48,7 +51,6 @@ for (var i = 0; i < to; i+=1) {
if (num === to) {
console.timeEnd('test');
-
// time per connectionNumber in 10000 loop in my pc with mariaDB 5.5
// better speed == more cpu usage
// 0.0.31
@@ -77,9 +79,15 @@ for (var i = 0; i < to; i+=1) {
/* //<<<<<<<<<<<<<block
+var testCon = mysql.createConnection({
+ user: 'test',
+ password: 'test',
+ database: 'test'
+});
+var count = 0;
// odd number test
for (var i = 0; i < to; i+=1) {
- test.query('select test from transaction_test where num=?',[i+1],function(err,result){
+ testCon.query('select test from transaction_test where num=?',[i+1],function(err,result){
if (result) {
if (result[0]) {
if (result[0].test) {
@@ -89,6 +97,10 @@ for (var i = 0; i < to; i+=1) {
}
}
}
+ count += 1;
+ if (count === to) {
+ console.log('test end');
+ }
});
};
// */
View
98 test/set_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');
@@ -27,9 +29,12 @@ on('error', function(err){
console.log('on top level error');
console.log(err);
});
-
+// this type error will occur to the top level error.
test.set('what?');
+// ** request work concurrently. so, result order can different.
+// test result vaule -> 1, 2, 7, 8
+
// /* //<<<<<<<<<<<<<block
// connection.set
@@ -45,16 +50,16 @@ test.set(function(err, safeCon){
return console.error(err);
}
safeCon.on('commit', function(){
- console.log('79 / 71 commit, after several event loop');
+ console.log('1 / 2 commit, after several event loop');
});
safeCon.on('rollback', function(err){
console.log(err);
});
- safeCon.query('insert transaction_test set test=?',[79],function(err,result){
+ safeCon.query('insert transaction_test set test=?',[1],function(err,result){
if (err) {
safeCon.rollback();
}
- safeCon.query('insert transaction_test set test=?',[71],function(err,result){
+ safeCon.query('insert transaction_test set test=?',[2],function(err,result){
if (err) {
safeCon.rollback(err);
}
@@ -69,6 +74,35 @@ test.set(function(err, safeCon){
});
});
+// timeout rollback
+test.set(function(err, safeCon){
+ if (err) {
+ return console.error(err);
+ }
+ safeCon.on('commit', function(){
+ console.log('3 / 4 commit, after several event loop');
+ });
+ safeCon.on('rollback', function(err){
+ console.log('3 / 4 rollback');
+ console.log(err);
+ });
+ safeCon.query('insert transaction_test set test=?',[3],function(err,result){
+ if (err) {
+ safeCon.rollback();
+ }
+ safeCon.query('insert transaction_test set test=?',[4],function(err,result){
+ if (err) {
+ safeCon.rollback(err);
+ }
+ setTimeout(function(){
+ // .set transaction can work after several event loop.
+ safeCon.commit();
+ // if you forget commit or rollback, connection will be leak.
+ },1000);
+ });
+ });
+});
+
// /*
// event query
@@ -84,17 +118,17 @@ test.set(function(err, safeCon){
console.log(err);
});
- reqQuery1 = safeCon.query('insert transaction_test set test=23');
- reqQuery2 = safeCon.query('insert transaction_test set test=11');
+ reqQuery1 = safeCon.query('insert transaction_test set test=5');
+ reqQuery2 = safeCon.query('insert transaction_test set test=6');
reqQuery1.
on('result', function(result){
- // console.log(result);
+ console.log(result);
});
reqQuery2.
on('result', function(result){
- safeCon.rollback('23 / 11 rollback yeap!')
+ safeCon.rollback('5 / 6 rollback!')
});
});
@@ -104,23 +138,33 @@ test.set(function(err, safeCon){
}
safeCon.
on('commit', function(){
- console.log('23371 commit!');
+ console.log('7 / 8 commit!');
}).
on('rollback', function(err){
console.log(err);
});
- reqQuery1 = safeCon.query('insert transaction_test set test=23371');
- reqQuery2 = safeCon.query('insert transaction_test set test=23371');
+ reqQuery1 = safeCon.query('insert transaction_test set test=7');
+ reqQuery2 = safeCon.query('insert transaction_test set test=8');
+
+ var insertNumber = 2;
+ var resultCount = 0;
+
+ function resultOn (result) {
+ resultCount += 1;
+ if (resultCount === insertNumber) {
+ safeCon.commit();
+ }
+ };
reqQuery1.
on('result', function(result){
- console.log('23371: ' + result.insertId);
- });
+ console.log('7: ' + result.insertId);
+ }).
+ on('result', resultOn);
+
reqQuery2.
- on('result', function(result){
- safeCon.commit()
- });
+ on('result', resultOn);
});
// error handling with event query
@@ -129,20 +173,20 @@ test.set(function(err, safeCon){
return console.error(err);
}
safeCon.on('commit', function(){
- console.log('23731 commit!');
+ console.log('9 / errrrrr commit!');
}).
on('rollback', function(err){
- console.log('23731 rollback');
+ console.log('9 / errrrrr rollback');
console.log(err);
});
// input type error value
- reqQuery1 = safeCon.query('insert transaction_test set test=23731');
+ reqQuery1 = safeCon.query('insert transaction_test set test=9');
reqQuery2 = safeCon.query('insert transaction_test set test=?',['errrrrr']);
reqQuery1.
on('result', function(result){
- console.log('23731: ' + result.insertId);
+ console.log('9: ' + result.insertId);
}).
on('error', function(err){
safeCon.rollback(err);
@@ -152,28 +196,26 @@ test.set(function(err, safeCon){
on('result', function(result){
safeCon.commit()
}).
- on('error', function(err){
- safeCon.rollback(err);
- });
+ on('error', safeCon.rollback.bind(safeCon));
});
test.set(function(err, safeCon){
if (err) {
return console.error(err);
}
safeCon.on('commit', function(){
- console.log('37301 commit!');
+ console.log('11 / 12 commit!');
});
safeCon.on('rollback', function(err){
- console.log('37301 rollback');
+ console.log('11 / 12 rollback');
console.log(err);
});
- reqQuery1 = safeCon.query('insert transaction_test set test=?',[37301]);
- reqQuery2 = safeCon.query('insert transaction_test set test=?',[37301]);
+ reqQuery1 = safeCon.query('insert transaction_test set test=?',[11]);
+ reqQuery2 = safeCon.query('insert transaction_test set test=?',[12]);
reqQuery1.
on('result', function(result){
- console.log('37301: ' + result.insertId);
+ console.log('11: ' + result.insertId);
});
// time out rollback
View
312 test/test.js
@@ -1,312 +0,0 @@
-// transaction test - bind all test
-// tested with mysql 2.0 alpha
-
-// 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');
-
-var transaction = require('../index.js');
-var test = transaction({
- connection: [mysql.createConnection,{
- user: 'test',
- password: 'test',
- database: 'test'
- }],
- // static parallel connection queue number
- staticConnection:10,
-
- // when queue length increase or queue length is longer than connectionNumber * 32,
- // make temporary connection for increased volume of async work.
- dynamicConnection:1,
-
- // auto time out rollback in ms
- timeOut:600
-});
-
-setTimeout(function(){
- console.log('end to end');
- test.end();
-},10000)
-
-/* //<<<<<<<<<<<<<block
-
-// simple loop test
-setTimeout(function(){
- var number = 0;
- var to = 10000;
- console.time('test');
- for (var i = 0; i < to; i+=1) {
- // transaction set, most low level queue method
- test.set(function(err, safeCon){
- var num = number+=1;
- safeCon.query('insert transaction_test set test=?',[num],function(err,result){
- if (!(num%2)) {
- // I don't want any odd number!
- safeCon.commit();
-
- }
-
- // when after commit...oops!
- // even code has a logical error, it works well
- safeCon.rollback();
- if (num === to) {
- console.timeEnd('test');
- }
- });
- });
- }
-},1000);
-//*/
-
-/* //<<<<<<<<<<<<<block
-
-// chain transaction API test
-var chain = test.chain();
-
-chain.
-on('commit', function(){
- console.log('chain1 commit');
-}).
-// if you don't make error event handler for query
-// each query take auto error event handler to the auto rollback
-on('rollback', function(err){
- console.log(err);
-});
-
-chain.
-query('insert transaction_test set test=?',[1]).
-on('result', function(result){
- chain.
- query('insert transaction_test set test=?',[2]).
- on('result', function(result){
- // you can make event listener for each query like this
- console.log('lol');
- }).
- query('insert transaction_test set test=?',[3]).
- on('result',function(result){
- console.log('3: '+result.insertId);
- // chain rush
- 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);
-
- // 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
- }).autoCommit(false);
-}).autoCommit(false);
-
-//chain error test
-var chain2 = test.chain();
-
-chain2.
-on('commit', function(){
- console.log('chain2 commit');
-}).
-on('rollback', function(err){
- console.log('chain2 rollback');
- console.log(err);
-}).
-query('insert transaction_test set test=?',[10]).
-on('result', function(result){
- chain2.
- query('insert transaction_test set test=?',[11]).
- query('insert transaction_test set test=?',[12]).
- on('result',function(result){
- // chain with error
- chain2.
- query('insert transaction_test set test=?',['err']).
- query('insert transaction_test set test=?',[14])
- }).autoCommit(false);
-}).autoCommit(false);
-
-var chain3 = test.chain();
-chain3.
-on('commit', function(){
- console.log('chain3 commit');
-}).
-on('rollback', function(err){
- console.log(err);
-}).
-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]);
-
-// transaction chain with loop!!!
-var chain4 = test.chain();
-chain4.
-on('commit', function(){
- console.log('chain4 commit');
-}).
-on('rollback', function(err){
- console.log(err);
-}).
-setMaxListeners(0);
-
-for(var i = 0; i < 5; i+=1) {
- // working good :)
- chain4.query('insert transaction_test set test=?',[i*100]);
-}
-
-var chain5 = test.chain();
-chain5.
-on('commit', function(){
- console.log('chain5 commit');
-}).
-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]);
-}
-
-// */
-
-
-/* //<<<<<<<<<<<<<block
-
-// connection.set
-// connection.set is the base API for queue reservation
-// connection.chain is wrapper of .set
-// simply connection.set doesn't have any transaction helper
-// in real world, you must to check error for every query request.
-// and you must to select rollback or commit for each transaction capsule.
-
-// commit after event loop
-test.set(function(err, safeCon){
- safeCon.on('commit', function(){
- console.log('79 / 71 commit, after several event loop');
- });
- safeCon.on('rollback', function(err){
- console.log(err);
- });
- safeCon.query('insert transaction_test set test=?',[79],function(err,result){
- if (err) {
- safeCon.rollback(err);
- }
- safeCon.query('insert transaction_test set test=?',[71],function(err,result){
- if (err) {
- safeCon.rollback(err);
- }
- setTimeout(function(){
- setTimeout(function(){
- // .set transaction can work after several event loop.
- safeCon.commit();
- // if you forget commit or rollback work with only set, connection will be leak.
- },0);
- },0);
- });
- });
-});
-
-// event query
-test.set(function(err, safeCon){
- safeCon.
- on('commit', function(){
- console.log('commit!');
- }).
- on('rollback', function(err){
- console.log(err);
- });
-
- reqQuery1 = safeCon.query('insert transaction_test set test=23');
- reqQuery2 = safeCon.query('insert transaction_test set test=11');
-
- reqQuery1.
- on('result', function(result){
- // console.log(result);
- });
-
- reqQuery2.
- on('result', function(result){
- safeCon.rollback('23 / 11 rollback yeap!')
- });
-});
-
-test.set(function(err, safeCon){
- safeCon.
- on('commit', function(){
- console.log('23371 commit!');
- }).
- on('rollback', function(err){
- console.log(err);
- });
-
- reqQuery1 = safeCon.query('insert transaction_test set test=23371');
- reqQuery2 = safeCon.query('insert transaction_test set test=23371');
-
- reqQuery1.
- on('result', function(result){
- console.log('23371: ' + result.insertId);
- });
- reqQuery2.
- on('result', function(result){
- safeCon.commit()
- });
-});
-
-// error handling with event query
-test.set(function(err, safeCon){
- safeCon.on('commit', function(){
- console.log('23731 commit!');
- }).
- on('rollback', function(err){
- console.log('23731 rollback');
- console.log(err);
- });
-
- // input type error value
- reqQuery1 = safeCon.query('insert transaction_test set test=23731');
- reqQuery2 = safeCon.query('insert transaction_test set test=?',['errrrrr']);
-
- reqQuery1.
- on('result', function(result){
- console.log('23731: ' + result.insertId);
- });
-
- reqQuery2.
- on('result', function(result){
- safeCon.commit()
- }).
- on('error', function(err){
- safeCon.rollback(err);
- });
-});
-
-test.set(function(err, safeCon){
- safeCon.on('commit', function(){
- console.log('37301 commit!');
- });
- safeCon.on('rollback', function(err){
- console.log('37301 rollback');
- console.log(err);
- });
- reqQuery1 = safeCon.query('insert transaction_test set test=?',[37301]);
- reqQuery2 = safeCon.query('insert transaction_test set test=?',[37301]);
-
- reqQuery1.
- on('result', function(result){
- console.log('37301: ' + result.insertId);
- });
-
- // time out rollback
- reqQuery2.
- on('result', function(result){
- setTimeout(function(){
- safeCon.commit()
- },1000);
- });
-});
-//*/

0 comments on commit ba48320

Please sign in to comment.