Skip to content

Fix for occasional duplicate key errors on session create or update #711

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

Merged
merged 1 commit into from
Aug 26, 2021
Merged

Fix for occasional duplicate key errors on session create or update #711

merged 1 commit into from
Aug 26, 2021

Conversation

maallen
Copy link
Contributor

@maallen maallen commented Aug 18, 2021

Updates the CreateSessionAttribute query to perform an UPDATE on the attribute bytes if a duplicate session id is found, this is a workaround for an issue in spring session where concurrent requests adding the same attribute result in a duplicate key violation.

See spring-projects/spring-session#1213 for further details.

@maallen maallen marked this pull request as draft August 18, 2021 11:16
@maallen maallen marked this pull request as ready for review August 19, 2021 14:47
* to change the Create Session Attribute query to use an MySQL UPDATE command to update the ATTRIBUTE_BYTES
* of the existing row if a duplicate key is encountered.
*
* <b>NOTE:</b>This custom insert query should only be used with a MySQL database.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only be used with a MySQL database, can this be made explicit with a @Conditional?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've pushed an update containing a custom condition (MySQLDatabaseCondition) that checks the prefix of the spring.datasource.url property, if the prefix matches "jdbc:mysql:" the condition passes


@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
String datasourceUrl = context.getEnvironment().getProperty("spring.datasource.url");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could DBUtils be reused/merged in some ways? not super important but if we can avoid duplication and have a single place with that logic.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed the DBUtils class, I don't think it can be merged into the condition as the condition logic is executed before the DBUtils bean is initialised but I could remove the conditional and autowire it into the CreateSessionAttributeInsertQuery bean. I'll update to execute the check in there and if there's not a mysql db then we'll log a warning stating the custom query has not been set.

…y errors on session creation/update

Fixed startup failures

Updated implementation to extend JdbcHttpSessionConfiguration

Added MySQLDatabaseCondition class conditional

Fixing build failures

Use DBUtils to check database type

Use default table name if spring.session.jdbc.table-name is not set
@aurambaj aurambaj merged commit c73494e into box:master Aug 26, 2021
ja-openai pushed a commit to ja-openai/mojito that referenced this pull request Jun 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants