Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for transaction use within the extension? #33

Open
ravenlost opened this issue Jan 20, 2022 · 2 comments
Open

Allow for transaction use within the extension? #33

ravenlost opened this issue Jan 20, 2022 · 2 comments

Comments

@ravenlost
Copy link

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

@sjdaniels
Copy link
Contributor

Hi Pat, I'll see if I can compile a list of what would need to be done to support this. Thanks for the thorough analysis. I agree it would be a good feature to add to the extension if we can support this. Bear in mind that I am mainly a CF developer and really just a hack at Java. I have only maintained this repo with minor fixes but I'm pretty familiar with the code at this point. Hopefully we can make it happen.

@ravenlost
Copy link
Author

ravenlost commented Jan 21, 2022

@sjdaniels what ever you can do is super appreciated. One thing also worth mentioning though is that if ever such a change should be brought to the extension, might as well try to update the java driver to most recent (4.4.1) as to properly support newest Mongo Server.

As per Mongo's transaction documentation: ...Use the MongoDB driver updated for the version of your MongoDB deployment....

See compatibility sheet: https://docs.mongodb.com/drivers/java/sync/current/compatibility/

So, theoretically, if one wants to use Mongo Server 5.x, they really should be using the java driver 4.4... Maybe it would still work with older driver, but might not be a good idea... Just food for thought ;)

Anyhow, thanks a million for your time with all this, even for just reading out the issue/recommandation!! Let me know if I can be of any help (testing, etc), even if I'm just learning it all right now ;)

UPDATE: just a quick note regarding transactions in Mongo, because I searched quite a bit for this one - Transactions will not work with standalone servers! You absolutely need to convert standalone servers to a replicaset (even if he's alone!) in order to have transactions work.

Refs.:
https://www.mongodb.com/community/forums/t/why-replica-set-is-mandatory-for-transactions-in-mongodb/9533
https://docs.mongodb.com/manual/tutorial/convert-standalone-to-replica-set/

Cheers! Pat

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants