Skip to content

Allow for transaction use within the extension? #33

@ravenlost

Description

@ravenlost

Hello,

This would be more of an enhancement suggestion, but thought it be worth documenting here (not sure where else?)

It seems that the current version of the extension doesn't allow to use transactions. From my readings we need to attach the session to all operations (inserts, updates, etc.), by passing the session as an argument to operation functions: insertOne, etc.

I am really not sure, but I think this is due to the fact that the extension uses getDB(), which is now deprecated instead of getDatabase().

The latter's getCollection() function seems to return a MongoCollection<Document> object (as opposed to a DBCollection), which in turn, has the operation methods that can receive the client's session (insertOne, etc)...

Now, I am not 100% sure of all this. I am no wiz in neither Mongo, CFML or Java.... But, looking at the issue with a colleague of mine, this is somewhat what we've came up with.

I wish I could just quickly modify the extension for this, but not sure if I'm up to the challenge... yet. lol.

p.s.: here's a code snippet of how I tried to do a basic transaction using the extension (at least, how I thought it should of worked):

var db = MongoDBConnect("testdb","localhost", 27017);
var clientSession = db.getMongo().startSession();
var jobsCollection = db["jobs"];

clientSession.startTransaction();
try {	
	// ...do some inserts,, updates, etc..
	jobsCollection.insert({"somekey": 1});
	jobsCollection.insert({"somekey": 3});

	// just cause an error
	jobsCollection.foobar();

	// another update...
	jobsCollection.update({"somekey": 1}, {"$set": {"somekey": "finished"}});

	// if we get here, commit transaction
	clientSession.commitTransaction();
}

catch(any e) {
	clientSession.abortTransaction();
	dump("got an error!");
}

clientSession.close();

Thanks a million for reading this.

Cheers,
Pat

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions