QOS.ch JIRA

  • Log In Access more options
    • Online Help
    • Keyboard Shortcuts
    • About JIRA
    • JIRA Credits
    • What’s New
  • Dashboards Access more options (Alt+d)
  • Projects Access more options (Alt+p)
  • Issues Access more options (Alt+i)
  • logback-site
  • LBSITE-36

Example 7.5 Is Not Thread-Safe

  • Log In
  • Views
    • XML
    • Word
    • Printable

Details

  • Type: Bug Bug
  • Status: Resolved Resolved
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: unspecified
  • Fix Version/s: 0.9.19
  • Component/s: Documentation
  • Labels:
    None

Description

Example 7.5 in

http://logback.qos.ch/manual/mdc.html

is not thread-safe because

userRegistered

is declared as a global variable.

Activity

Ascending order - Click to sort in descending order
  • All
  • Comments
  • History
  • Activity
Hide
Permalink
john jimenez added a comment - 10/Feb/10 12:01 AM
I made it a major because the back drop of the example is highlighting the inherent thread-safetiness of MDC.
Show
john jimenez added a comment - 10/Feb/10 12:01 AM I made it a major because the back drop of the example is highlighting the inherent thread-safetiness of MDC.
Hide
Permalink
john jimenez added a comment - 10/Feb/10 12:18 AM
Instead of synchronizing the points of userRegistered access, one can store it in the session or in this particular implementation the request.
Show
john jimenez added a comment - 10/Feb/10 12:18 AM Instead of synchronizing the points of userRegistered access, one can store it in the session or in this particular implementation the request.
Hide
Permalink
Ralph Goers added a comment - 10/Feb/10 1:12 AM
I concur that the way userRegistered is used is not thread safe. However, in this example there is no need to store it in the session or the request. Instead, define registerUserName as

private boolean registerUsername(String username) {
   
    if (username != null && username.trim().length() > 0) {
      MDC.put(userKey, username);
      return truel
    }
    return false;
  }

then in doFilter do

String userName;
if (principal != null) {
  username = principal.getName();
} else {
  username = (String) req.getSession().getAttribute(userKey);
}
boolean userRegistered = registerUserName(userName);
Show
Ralph Goers added a comment - 10/Feb/10 1:12 AM I concur that the way userRegistered is used is not thread safe. However, in this example there is no need to store it in the session or the request. Instead, define registerUserName as private boolean registerUsername(String username) {         if (username != null && username.trim().length() > 0) {       MDC.put(userKey, username);       return truel     }     return false;   } then in doFilter do String userName; if (principal != null) {   username = principal.getName(); } else {   username = (String) req.getSession().getAttribute(userKey); } boolean userRegistered = registerUserName(userName);
Hide
Permalink
Ceki Gulcu added a comment - 18/Mar/10 7:12 PM - edited
John, thank you for reporting this issue. It was fixed along the lines suggested by Ralph. (Thanks Ralph).
Show
Ceki Gulcu added a comment - 18/Mar/10 7:12 PM - edited John, thank you for reporting this issue. It was fixed along the lines suggested by Ralph. (Thanks Ralph).

People

  • Assignee:
    Ceki Gulcu
    Reporter:
    john jimenez
Vote (0)
Watch (1)

Dates

  • Created:
    09/Feb/10 11:48 PM
    Updated:
    18/Mar/10 7:13 PM
    Resolved:
    18/Mar/10 7:12 PM
  • Atlassian JIRA (v5.0.4#731-sha1:3aa7374)
  • Report a problem
  • Powered by a free Atlassian JIRA open source license for logback. Try JIRA - bug tracking software for your team.