|
| 1 | +package com.box.l10n.mojito.service.security.user.session; |
| 2 | + |
| 3 | +import com.box.l10n.mojito.service.DBUtils; |
| 4 | +import org.slf4j.Logger; |
| 5 | +import org.slf4j.LoggerFactory; |
| 6 | +import org.springframework.beans.factory.annotation.Autowired; |
| 7 | +import org.springframework.beans.factory.annotation.Value; |
| 8 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 9 | +import org.springframework.context.annotation.Bean; |
| 10 | +import org.springframework.context.annotation.Conditional; |
| 11 | +import org.springframework.context.annotation.Configuration; |
| 12 | +import org.springframework.session.jdbc.JdbcIndexedSessionRepository; |
| 13 | +import org.springframework.session.jdbc.config.annotation.web.http.JdbcHttpSessionConfiguration; |
| 14 | +import org.springframework.util.StringUtils; |
| 15 | + |
| 16 | + |
| 17 | +/** |
| 18 | + * Workaround for issue <a href="https://github.com/spring-projects/spring-session/issues/1213">Concurrent requests adding the same session attribute result in duplicate key constraint violation</a> |
| 19 | + * to change the Create Session Attribute query to use an MySQL UPDATE command to update the ATTRIBUTE_BYTES |
| 20 | + * of the existing row if a duplicate key is encountered. |
| 21 | + * |
| 22 | + * <b>NOTE:</b>This custom insert query should only be used with a MySQL database. |
| 23 | + */ |
| 24 | +@Configuration |
| 25 | +@ConditionalOnProperty(value = "l10n.spring.session.use-custom-mysql-create-session-attribute-query", havingValue = "true") |
| 26 | +public class CustomCreateSessionAttributeInsertQueryConfiguration extends JdbcHttpSessionConfiguration { |
| 27 | + |
| 28 | + static Logger logger = LoggerFactory.getLogger(CustomCreateSessionAttributeInsertQueryConfiguration.class); |
| 29 | + |
| 30 | + private static final String CREATE_SESSION_ATTRIBUTE_QUERY_ON_DUPLICATE_KEY_UPDATE = |
| 31 | + "INSERT INTO %TABLE_NAME%_ATTRIBUTES(SESSION_PRIMARY_ID, ATTRIBUTE_NAME, ATTRIBUTE_BYTES) " |
| 32 | + + "SELECT PRIMARY_ID, ?, ? " |
| 33 | + + "FROM %TABLE_NAME% " |
| 34 | + + "WHERE SESSION_ID = ? ON DUPLICATE KEY UPDATE ATTRIBUTE_BYTES=VALUES(ATTRIBUTE_BYTES)"; |
| 35 | + |
| 36 | + @Autowired |
| 37 | + DBUtils dbUtils; |
| 38 | + |
| 39 | + @Value("${spring.session.jdbc.table-name}") |
| 40 | + private String customTableName; |
| 41 | + |
| 42 | + @Bean |
| 43 | + @Override |
| 44 | + public JdbcIndexedSessionRepository sessionRepository() { |
| 45 | + logger.debug("Setting Spring Session custom session attribute query."); |
| 46 | + JdbcIndexedSessionRepository sessionRepository = super.sessionRepository(); |
| 47 | + sessionRepository.setTableName(getTableName()); |
| 48 | + updateCreateSessionAttributeQuery(sessionRepository); |
| 49 | + return sessionRepository; |
| 50 | + } |
| 51 | + |
| 52 | + private void updateCreateSessionAttributeQuery(JdbcIndexedSessionRepository sessionRepository) { |
| 53 | + logger.debug("Updating the Create Session Attribute query"); |
| 54 | + if (dbUtils.isMysql()) { |
| 55 | + sessionRepository.setCreateSessionAttributeQuery(getCustomCreateSessionAttributeQuery()); |
| 56 | + } else { |
| 57 | + logger.warn("The database is not MySQL, skipping query update."); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + private String getCustomCreateSessionAttributeQuery() { |
| 62 | + return StringUtils.replace( |
| 63 | + CREATE_SESSION_ATTRIBUTE_QUERY_ON_DUPLICATE_KEY_UPDATE, |
| 64 | + "%TABLE_NAME%", |
| 65 | + getTableName()); |
| 66 | + } |
| 67 | + |
| 68 | + private String getTableName() { |
| 69 | + return customTableName != null ? customTableName : JdbcIndexedSessionRepository.DEFAULT_TABLE_NAME; |
| 70 | + } |
| 71 | + |
| 72 | +} |
0 commit comments