[JAVA-4733] Log warning on possible incorrect use of sessions Created: 22/Sep/22  Updated: 26/Sep/22

Status: Backlog
Project: Java Driver
Component/s: Logging
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Jeffrey Yemin Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

It's too easy to neglect passing in the ClientSession to a CRUD operation even when the session is started in the current lexical scope. Consider adding a warning to the logs if this possibly incorrect usage is detected. Pseudocode would be something like:

        private void checkSession(@Nullable ClientSession session) {
            ClientSession threadLocalActiveTransactionSession = ClientSessionImpl.ACTIVE_TRANSACTION_SESSION.get();
            if (session == null && threadLocalActiveTransactionSession != null) {
                String msg = "There is a session with a transaction active on the current thread, but the session was not passed as an "
                        + "argument to the operation";
                LOGGER.warn(msg);
                // possibly throw an exception if some option is set
            } else if (session != threadLocalActiveTransactionSession) {
                String msg = "There is a session with a transaction active on the current thread, but a different session was passed "
                        + "as an argument to the operation";
                LOGGER.warn(msg);
            }
        }

Also consider adding an option that would cause the driver to throw a MongoClientException instead of logging a warning.

To opt-out of the behavior on a per-call basis, consider adding some sort of "null" ClientSession to indicate that you really meant to leave it out.

Also consider whether this behavior should be restricted to sessions with active transactions, or just any open session.

Also consider whether there should be a global opt-out for the entire new behavior. For example, this wouldn't play well with the unified tests.

Finally, consider whether this should be promoted to a DRIVERS ticket.


Generated at Thu Feb 08 09:02:50 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.