Permalink
Browse files

Fixes model access to transactions

  • Loading branch information...
1 parent b5a4fca commit 15ee5a00a7146b319f71378a63feeb23c820d71c @kriszyp kriszyp committed Jan 26, 2010
Showing with 30 additions and 7 deletions.
  1. +5 −4 lib/jsgi/transactional.js
  2. +25 −3 lib/model.js
@@ -5,24 +5,25 @@
*/
exports.Transactional = Transactional;
var when = require("promise").when,
- contextModule = require("../util/context");
+ contextModule = require("../util/context"),
+ model = require("../model");
function Transactional(database, nextApp){
return function(request){
if(request.jsgi.multithreaded){
print("Warning: Running in a multithreaded environment may cause non-deterministic behavior");
}
- var transaction = request.transaction = database.transaction();
+ var transaction = request.transaction = model.transaction();
var context = request.context;
if(!context){
context = contextModule.currentContext = request.context = {};
}
context.onResume = function(){
// getters and setters on the database should intercept this and trigger per-database transaction changes
- database.currentTransaction = transaction;
+ model.currentTransaction = transaction;
};
context.onSuspend = function(){
- database.currentTransaction = null;
+ model.currentTransaction = null;
};
try{
View
@@ -36,14 +36,36 @@ exports.Model = function(name, store, schema){
schema.id = name;
defineProperty(schema, "transaction", {
get: function(){
- return {
- openObjectStore: exports.openObjectStore
- };
+ return exports.currentTransaction;
}
});
return models[name] = schema;
};
+exports.transaction = function(){
+ var dbTransaction = stores.transaction();
+ return exports.currentTransaction = {
+ openObjectStore: exports.openObjectStore,
+ commit: function(){
+ try{
+ dbTransaction.commit();
+ }
+ finally{
+ exports.currentTransaction = null;
+ }
+ },
+ abort: function(){
+ try{
+ dbTransaction.abort();
+ }
+ finally{
+ exports.currentTransaction = null;
+ }
+ }
+ }
+
+};
+
exports.openObjectStore = function(name){
var model = models[name];
if(!model){

0 comments on commit 15ee5a0

Please sign in to comment.