[GODRIVER-1703] Change Session examples to use transactions Created: 24/Jul/20 Updated: 28/Oct/23 Resolved: 13/Aug/20 |
|
| Status: | Closed |
| Project: | Go Driver |
| Component/s: | Documentation |
| Affects Version/s: | 1.3.5 |
| Fix Version/s: | 1.4.1 |
| Type: | Improvement | Priority: | Major - P3 |
| Reporter: | D V | Assignee: | Divjot Arora (Inactive) |
| Resolution: | Fixed | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Environment: |
go 1.14 on mac os x |
||
| Attachments: |
|
||||||||
| Issue Links: |
|
||||||||
| Description |
|
Updated Description https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo@v1.3.5?tab=doc#example-Client.UseSessionWithOptions uses the DefaultReadConcern option, which only applies to transactions started within a session, but does not start a transaction. This is confusing, so we should change the example to start a transaction. We should also verify that other sessions-related examples don't do the same thing.
Previous Description Using a session with readpref=Secondary results in queries against the primary. Please see the attached test case that demonstrates the issue. My setup:
Debugging FindOne() it seems like the read preference setting for the session is ignored if not inside a transaction. Setting the read preference on the Collection makes the test pass. I'm roughly following the example from https://godoc.org/go.mongodb.org/mongo-driver/mongo#Client.UseSession . Am I using the API incorrectly? |
| Comments |
| Comment by Githook User [ 13/Aug/20 ] |
|
Author: {'name': 'Divjot Arora', 'email': 'divjot.arora@10gen.com', 'username': 'divjotarora'}Message: |
| Comment by Githook User [ 13/Aug/20 ] |
|
Author: {'name': 'Divjot Arora', 'email': 'divjot.arora@10gen.com', 'username': 'divjotarora'}Message: |
| Comment by Divjot Arora (Inactive) [ 12/Aug/20 ] |
| Comment by Divjot Arora (Inactive) [ 28/Jul/20 ] |
|
Thanks for the documentation suggestions. I agree these would be helpful. The driver can't panic when the session is created because a user could be connected to a mixed-version deployment, where some subset of servers supports transactions, or the cluster could be upgraded/downgraded. To support such dynamic behavior, configurations like this have to be allowed. We can definitely clarify some of this in our docs, though. I've put this ticket back into "Investigating" and will figure out what changes need to be made over the next few days. – Divjot |
| Comment by D V [ 28/Jul/20 ] |
|
Thanks, Divjot! That explains the behaviour. Do you think the documentation could be clarified a little? The example I was looking at (https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo@v1.3.5?tab=doc#example-Client.UseSessionWithOptions) doesn't use a transaction, so that made it seem like it was enough to set the read preference and read concern on the Session. I think it would be clearer if the example included the use of a transaction make it explicitly mandatory. Also, maybe a sentence in https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo@v1.3.5?tab=doc#Session about this could help guide users in the right direction? Something like "Setting read/write preference and concern on the Session requires the use of a Transaction to take effect"? Personally, I would prefer the driver to panic if I tried to set anything on the Session that wasn't supported in a non-transactional context instead of silently ignoring it due to the difficulty of detecting the problem. This would have made it a lot easier for me to notice my misunderstanding of the API. |
| Comment by Divjot Arora (Inactive) [ 28/Jul/20 ] |
|
Hi dv@glyphy.com, As documented at https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo/options?tab=doc#SessionOptions, the DefaultReadPreference option for a session controls the read preference used for transactions started within the session. Transactions are only supported on MongoDB servers 4.0 and higher. The only way to control the read preference for non-transactional operations is to set it on the Collection instance. I'm going to close this ticket out as "Works as Designed" but feel free to leave another comment or open a new ticket if you have further questions. – Divjot |
| Comment by D V [ 24/Jul/20 ] |
|
Forgot to mention that I'm using MongoDB 3.6.18. |