Skip to content

Prevent accessing the session in ServletRequestAttributes.updateAccessedSessionAttributes() if no session attributes need to be updated [SPR-13950] #18522

@spring-projects-issues

Description

@spring-projects-issues

Mathieu Lachance opened SPR-13950 and commented

Hi,

we are currently using Wildfly with pessimistic web cache and we are running into contention issue in ServletRequestAttributes.updateAccessedSessionAttributes() due to the session access attempt:

this.session = this.request.getSession(false);

Could we add a simple guard to make sure we do not try to access the session if there's nothing in the sessionAttributesToUpdate map? i.e.:

	/**
	 * Update all accessed session attributes through {@code session.setAttribute}
	 * calls, explicitly indicating to the container that they might have been modified.
	 */
	@Override
	protected void updateAccessedSessionAttributes() {
                // proposed improvement request: skip if nothing has changed
                if (this.sessionAttributesToUpdate.isEmpty()) {
                    // nothing to do
                    return;
                }

		// Store session reference for access after request completion.
		this.session = this.request.getSession(false);
		// Update all affected session attributes.
		if (this.session != null) {
			try {
				for (Map.Entry<String, Object> entry : this.sessionAttributesToUpdate.entrySet()) {
					String name = entry.getKey();
					Object newValue = entry.getValue();
					Object oldValue = this.session.getAttribute(name);
					if (oldValue == newValue && !isImmutableSessionAttribute(name, newValue)) {
						this.session.setAttribute(name, newValue);
					}
				}
			}
			catch (IllegalStateException ex) {
				// Session invalidated - shouldn't usually happen.
			}
		}
		this.sessionAttributesToUpdate.clear();
	}

Let me know if I'm missing something.

Thanks,


Affects: 4.2.4

Referenced from: commits 8495fcf

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions