Advertisement
Guest User

Untitled

a guest
Jan 15th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. 'use strict';
  2.  
  3. var oracledb = require('oracledb');
  4.  
  5. console.log('start');
  6.  
  7. watch();
  8.  
  9. var numberOfTests = 0;
  10.  
  11. var repeat = process.argv[2] || 1;
  12. for (var i = 1; i <= repeat; ++i) {
  13. dbTest(i);
  14. }
  15.  
  16. setTimeout(function() {
  17. dbTest('confirm');
  18. }, 2500 * repeat);
  19.  
  20. //
  21.  
  22. function watch() {
  23. console.log('running');
  24. setTimeout(function() {
  25. if (numberOfTests === 0) {
  26. console.log('finished');
  27. return;
  28. }
  29. watch();
  30. }, 1000);
  31. }
  32.  
  33. // DB
  34.  
  35. var sqlLock = 'SELECT * FROM hr.employees WHERE employee_id = 100 FOR UPDATE';
  36. var sqlDelay = 'begin dbms_lock.sleep(1); end;';
  37.  
  38. function dbTest(id) {
  39. ++numberOfTests;
  40. connect(function(conn) {
  41. delay(conn, function() {
  42. lock(conn, function() {
  43. delay(conn, function() {
  44. release(conn, function() {
  45. --numberOfTests;
  46. }, id);
  47. }, id);
  48. }, id);
  49. }, id);
  50. }, id);
  51. }
  52.  
  53. function connect(callback, id) {
  54. console.log(id, 'connecting');
  55. oracledb.getConnection({
  56. user: 'hr',
  57. password: 'hr',
  58. connectString: '192.168.56.101',
  59. }, function(err, conn) {
  60. if (err) {
  61. console.error(id + ': ' + err.message);
  62. throw err;
  63. }
  64. callback.call({}, conn);
  65. });
  66. }
  67.  
  68. function delay(conn, callback, id) {
  69. console.log(id, 'try to delay');
  70. conn.execute(sqlDelay, {}, function(err) {
  71. if (err) {
  72. console.error(id + ': ' + err.message);
  73. throw err;
  74. }
  75. console.log(id, 'delayed');
  76. callback.call();
  77. })
  78. }
  79.  
  80. function lock(conn, callback, id) {
  81. console.log(id, 'try to lock');
  82. conn.execute(sqlLock, {}, function(err) {
  83. if (err) {
  84. console.error(id + ': ' + err.message);
  85. throw err;
  86. }
  87. console.log(id, 'locked');
  88. callback.call();
  89. })
  90. }
  91.  
  92. function release(conn, callback, id) {
  93. console.log(id, 'try to release');
  94. conn.release(function(err) {
  95. if (err) {
  96. console.error(id + ': ' + err.message);
  97. throw err;
  98. }
  99. console.log(id, 'released');
  100. callback.call();
  101. });
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement