Skip to content

Commit 475fa48

Browse files
committed
Add HsqldbJdbcIndexedSessionRepositoryCustomizer
Similar to other relational databases, this configures the session attribute create query to perform an upsert in order not to fail in the case of concurrent requests modifying the same session attributes Fixes: #3626
1 parent 4126d9e commit 475fa48

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2014-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.session.jdbc;
18+
19+
import org.junit.jupiter.api.extension.ExtendWith;
20+
21+
import org.springframework.context.annotation.Bean;
22+
import org.springframework.context.annotation.Configuration;
23+
import org.springframework.test.context.ContextConfiguration;
24+
import org.springframework.test.context.junit.jupiter.SpringExtension;
25+
import org.springframework.test.context.web.WebAppConfiguration;
26+
27+
/**
28+
* Integration tests for {@link JdbcIndexedSessionRepository} using HsqlDB
29+
* with {@link HsqldbJdbcIndexedSessionRepositoryCustomizer}.
30+
*
31+
* @author Martin Ashby
32+
*/
33+
@ExtendWith(SpringExtension.class)
34+
@WebAppConfiguration
35+
@ContextConfiguration
36+
class HsqldbJdbcIndexedSessionRepositoryCustomizerITests extends HsqldbJdbcIndexedSessionRepositoryITests {
37+
38+
@Configuration
39+
static class CustomizerConfig extends Config {
40+
41+
@Bean
42+
HsqldbJdbcIndexedSessionRepositoryCustomizer postgreSqlJdbcIndexedSessionRepositoryCustomizer() {
43+
return new PostgreSqlJdbcIndexedSessionRepositoryCustomizer();
44+
}
45+
46+
}
47+
48+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2014-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.session.jdbc;
18+
19+
import org.springframework.session.config.SessionRepositoryCustomizer;
20+
21+
/**
22+
* A {@link SessionRepositoryCustomizer} implementation that applies HsqlDB specific
23+
* optimized SQL statements to {@link JdbcIndexedSessionRepository}.
24+
*
25+
* @author Martin Ashby
26+
* @since 3.5.5
27+
*/
28+
public class HsqldbSqlJdbcIndexedSessionRepositoryCustomizer
29+
implements SessionRepositoryCustomizer<JdbcIndexedSessionRepository> {
30+
31+
private static final String CREATE_SESSION_ATTRIBUTE_QUERY = """
32+
MERGE INTO %TABLE_NAME%_ATTRIBUTES AS T
33+
USING (VALUES (?, ?, CAST (? AS LONGVARBINARY))) AS I (SESSION_PRIMARY_ID, ATTRIBUTE_NAME, ATTRIBUTE_BYTES)
34+
ON (T.SESSION_PRIMARY_ID = I.SESSION_PRIMARY_ID AND T.ATTRIBUTE_NAME = I.ATTRIBUTE_NAME)
35+
WHEN MATCHED THEN UPDATE SET T.ATTRIBUTE_BYTES = I.ATTRIBUTE_BYTES
36+
WHEN NOT MATCHED THEN INSERT VALUES I.SESSION_PRIMARY_ID, I.ATTRIBUTE_NAME, I.ATTRIBUTE_BYTES
37+
""";
38+
39+
@Override
40+
public void customize(JdbcIndexedSessionRepository sessionRepository) {
41+
sessionRepository.setCreateSessionAttributeQuery(CREATE_SESSION_ATTRIBUTE_QUERY);
42+
}
43+
44+
}

0 commit comments

Comments
 (0)