Skip to content

Commit

Permalink
fix bug of transation
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterMu committed Aug 4, 2016
1 parent 178d135 commit da77eb9
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 51 deletions.
104 changes: 65 additions & 39 deletions dist/nodebatis.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,27 +85,53 @@ var NodeBatis = function () {
}, {
key: 'beginTransation',
value: function () {
var _ref2 = _asyncToGenerator(regeneratorRuntime.mark(function _callee2() {
var conn;
return regeneratorRuntime.wrap(function _callee2$(_context2) {
var _ref2 = _asyncToGenerator(regeneratorRuntime.mark(function _callee3() {
var _this = this;

var that, conn;
return regeneratorRuntime.wrap(function _callee3$(_context3) {
while (1) {
switch (_context2.prev = _context2.next) {
switch (_context3.prev = _context3.next) {
case 0:
_context2.next = 2;
that = this;
_context3.next = 3;
return this.pool.beginTransation();

case 2:
conn = _context2.sent;

conn.query = this.query.bind(this);
return _context2.abrupt('return', conn);
case 3:
conn = _context3.sent;

conn.query = function () {
var _ref3 = _asyncToGenerator(regeneratorRuntime.mark(function _callee2(key, data) {
return regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
_context2.next = 2;
return that.query.apply(that, [key, data, conn]);

case 2:
return _context2.abrupt('return', _context2.sent);

case 3:
case 'end':
return _context2.stop();
}
}
}, _callee2, _this);
}));

return function (_x4, _x5) {
return _ref3.apply(this, arguments);
};
}();
return _context3.abrupt('return', conn);

case 5:
case 6:
case 'end':
return _context2.stop();
return _context3.stop();
}
}
}, _callee2, this);
}, _callee3, this);
}));

function beginTransation() {
Expand All @@ -117,80 +143,80 @@ var NodeBatis = function () {
}, {
key: 'commit',
value: function () {
var _ref3 = _asyncToGenerator(regeneratorRuntime.mark(function _callee3(conn) {
return regeneratorRuntime.wrap(function _callee3$(_context3) {
var _ref4 = _asyncToGenerator(regeneratorRuntime.mark(function _callee4(conn) {
return regeneratorRuntime.wrap(function _callee4$(_context4) {
while (1) {
switch (_context3.prev = _context3.next) {
switch (_context4.prev = _context4.next) {
case 0:
_context3.next = 2;
_context4.next = 2;
return this.pool.commit(conn);

case 2:
return _context3.abrupt('return', _context3.sent);
return _context4.abrupt('return', _context4.sent);

case 3:
case 'end':
return _context3.stop();
return _context4.stop();
}
}
}, _callee3, this);
}, _callee4, this);
}));

function commit(_x4) {
return _ref3.apply(this, arguments);
function commit(_x6) {
return _ref4.apply(this, arguments);
}

return commit;
}()
}, {
key: 'rollback',
value: function () {
var _ref4 = _asyncToGenerator(regeneratorRuntime.mark(function _callee4(conn) {
return regeneratorRuntime.wrap(function _callee4$(_context4) {
var _ref5 = _asyncToGenerator(regeneratorRuntime.mark(function _callee5(conn) {
return regeneratorRuntime.wrap(function _callee5$(_context5) {
while (1) {
switch (_context4.prev = _context4.next) {
switch (_context5.prev = _context5.next) {
case 0:
_context4.next = 2;
_context5.next = 2;
return this.pool.rollback(conn);

case 2:
case 'end':
return _context4.stop();
return _context5.stop();
}
}
}, _callee4, this);
}, _callee5, this);
}));

function rollback(_x5) {
return _ref4.apply(this, arguments);
function rollback(_x7) {
return _ref5.apply(this, arguments);
}

return rollback;
}()
}, {
key: 'releaseConn',
value: function () {
var _ref5 = _asyncToGenerator(regeneratorRuntime.mark(function _callee5(connection) {
return regeneratorRuntime.wrap(function _callee5$(_context5) {
var _ref6 = _asyncToGenerator(regeneratorRuntime.mark(function _callee6(connection) {
return regeneratorRuntime.wrap(function _callee6$(_context6) {
while (1) {
switch (_context5.prev = _context5.next) {
switch (_context6.prev = _context6.next) {
case 0:
_context5.next = 2;
_context6.next = 2;
return this.pool.releaseConn(connection);

case 2:
return _context5.abrupt('return', _context5.sent);
return _context6.abrupt('return', _context6.sent);

case 3:
case 'end':
return _context5.stop();
return _context6.stop();
}
}
}, _callee5, this);
}, _callee6, this);
}));

function releaseConn(_x6) {
return _ref5.apply(this, arguments);
function releaseConn(_x8) {
return _ref6.apply(this, arguments);
}

return releaseConn;
Expand Down
5 changes: 4 additions & 1 deletion src/nodebatis.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ class NodeBatis {
}

async beginTransation() {
let that = this
let conn = await this.pool.beginTransation()
conn.query = this.query.bind(this)
conn.query = async (key, data) => {
return await that.query.apply(that, [key, data, conn])
}
return conn
}

Expand Down
18 changes: 10 additions & 8 deletions test/dist/transationTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,32 +59,34 @@ var transationTest = function () {
console.log('end insert');
console.log('begin find ...');
_context.next = 12;
return conn.query('test.findAll');
return conn.query('test.findTest');

case 12:
result = _context.sent;

console.log(result);
nodebatis.commit(conn);
return _context.abrupt('return', result);

case 17:
_context.prev = 17;
case 18:
_context.prev = 18;
_context.t0 = _context['catch'](1);

console.log(_context.t0);
nodebatis.rollback(conn);

case 20:
_context.prev = 20;
case 22:
_context.prev = 22;

conn && nodebatis.releaseConn(conn);
return _context.finish(20);
return _context.finish(22);

case 23:
case 25:
case 'end':
return _context.stop();
}
}
}, _callee, undefined, [[1, 17, 20, 23]]);
}, _callee, undefined, [[1, 18, 22, 25]]);
}));

return function transationTest() {
Expand Down
4 changes: 3 additions & 1 deletion test/src/transationTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ let transationTest = async () => {
})
console.log('end insert')
console.log('begin find ...')
let result = await conn.query('test.findAll')
let result = await conn.query('test.findTest')
console.log(result)
nodebatis.commit(conn)
return result
} catch (e) {
console.log(e)
nodebatis.rollback(conn)
} finally {
conn && nodebatis.releaseConn(conn)
}
Expand Down
7 changes: 5 additions & 2 deletions test/yaml/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ findByAge:
sql: and name = 'name1'

insertOne:
- insert into test(name, _age)
values(:name, :age)
- insert into test(name, age)
- values(:name, :age)

findTest:
- select * from test where _age = 123



Expand Down

0 comments on commit da77eb9

Please sign in to comment.