Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-4733

Log warning on possible incorrect use of sessions

    • Type: Icon: Improvement Improvement
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: Logging
    • None

      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.

            Assignee:
            Unassigned Unassigned
            Reporter:
            jeff.yemin@mongodb.com Jeffrey Yemin
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: