[GODRIVER-1104] Provide imperative transaction API Created: 03/Jun/19  Updated: 28/Oct/23  Resolved: 13/Apr/20

Status: Closed
Project: Go Driver
Component/s: CRUD
Affects Version/s: None
Fix Version/s: 1.4.0

Type: New Feature Priority: Minor - P4
Reporter: Ammar Bandukwala Assignee: Divjot Arora (Inactive)
Resolution: Fixed Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
is duplicated by GODRIVER-1523 Expose SessionContext of transactions Closed

 Description   

This issue follows from the GitHub discussion here: https://github.com/mongodb/mongo-go-driver/pull/161.

Basically, I believe that we should provide a transaction API similar to `database/sql` for these reasons:

The callback API is unfortunate to work with in real-world code. Even if there are concurrency problems, I would much prefer this as an option with clear documentation than be forced to use callbacks.

Within the callback, it's more difficult to return the outer function or initialize outer variables. This leads to variable shadowing, interface type assertions, and duplicated error handling. All of which significantly increase the volume of code, and the probability of bugs.

Lastly, Go packages should take inspiration from the stdlib as much as possible for a consistent user experience. The Go authors decided to avoid the callback API in database/sql.



 Comments   
Comment by Githook User [ 13/Apr/20 ]

Author:

{'name': 'Divjot Arora', 'email': 'divjot.arora@10gen.com', 'username': 'divjotarora'}

Message: GODRIVER-1104 Add imperative sessions API (#364)
Branch: master
https://github.com/mongodb/mongo-go-driver/commit/6a555e4b692695080afb33ebeefe5aee9be58ea6

Comment by Divjot Arora (Inactive) [ 10/Apr/20 ]

https://github.com/mongodb/mongo-go-driver/pull/364

Comment by Divjot Arora (Inactive) [ 03/Feb/20 ]

Putting this back into "Needs Triage" because we've discussed this internally and think that this may be a good idea to have, as a callback-based API isn't always a usable solution for applications.

Comment by Kristofer Brandow (Inactive) [ 10/Jun/19 ]

Hi ammarb,

Thanks for filing this ticket. We understand that being forced to use a closure or callback to use the MongoDB Sessions API isn't ideal. We try to build flexible APIs but in this circumstance there is a high chance of misuse and that misuse is difficult to detect. This can result in a user thinking they've protected themselves by using a transaction or session and not find out they didn't until something catastrophic happens. In other words, adding this API makes it too easy for users to use the API in a manner that defeats the main benefits of the API. In the future we will be redesigning the API of all the drivers to fix issues like this with sessions, so instead of adding this API now we will wait until that redesign happens.

Thanks,

Kris

Generated at Thu Feb 08 08:35:41 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.