Skip to content

Using JDBC, setting new attribute multiple times results in an UPDATE not an INSERT #1069

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
candrews opened this issue May 10, 2018 · 3 comments
Assignees
Labels
status: duplicate A duplicate of another issue

Comments

@candrews
Copy link
Contributor

When using JDBC, setting a new attribute multiple times results in an SQL UPDATE not an INSERT. Because there is no row to update, the session attribute is not actually saved to the table.

For example, in a request handler (a Spring MVC controller, servlet, filter, etc):

Assert.isTrue(request.getSession().getAttribute("test")==null);
request.getSession().setAttribute("test", "someValue");
request.getSession().setAttribute("test", "someValue");

I would expect a row to be added to the SPRING_SESSION_ATTRIBUTES table with the ATTRIBUTE_NAME of "test" and the ATTRIBUTE_VALUE of "someValue"

However, what actually happens is that an UPDATE statement is executed resulted in no row being added to SPRING_SESSION_ATTRIBUTES.

The problem appears to be at https://github.com/spring-projects/spring-session/blob/2.0.3.RELEASE/spring-session-jdbc/src/main/java/org/springframework/session/jdbc/JdbcOperationsSessionRepository.java#L738 - I believe the line should be:

else if (this.delta.get(attributeName) != DeltaValue.ADDED && this.delegate.getAttribute(attributeName) != null) {
@candrews
Copy link
Contributor Author

Opened a pull request: #1070

@candrews
Copy link
Contributor Author

An alternative solution would be to use an UPSERT for the update SQL instead of an UPDATE... but there's no standard UPSERT support in the SQL standard, so that's probably not a workable approach.

@vpavic vpavic self-assigned this May 10, 2018
@vpavic
Copy link
Contributor

vpavic commented May 10, 2018

Thanks for the report @candrews! Closing in favor of PR #1070.

@vpavic vpavic closed this as completed May 10, 2018
@vpavic vpavic added the status: duplicate A duplicate of another issue label May 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

2 participants