Skip to content

Commit 7769ded

Browse files
committed
Merge branch '5.3.x'
2 parents aaa10e9 + 75e1811 commit 7769ded

File tree

8 files changed

+109
-8
lines changed

8 files changed

+109
-8
lines changed

spring-jdbc/src/main/java/org/springframework/jdbc/support/incrementer/H2SequenceMaxValueIncrementer.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@
2323
* of a given H2 sequence.
2424
*
2525
* @author Thomas Risberg
26+
* @author Henning Pöttker
2627
* @since 2.5
2728
*/
2829
public class H2SequenceMaxValueIncrementer extends AbstractSequenceMaxValueIncrementer {
@@ -47,7 +48,7 @@ public H2SequenceMaxValueIncrementer(DataSource dataSource, String incrementerNa
4748

4849
@Override
4950
protected String getSequenceQuery() {
50-
return "select " + getIncrementerName() + ".nextval from dual";
51+
return "values next value for " + getIncrementerName();
5152
}
5253

5354
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright 2002-2022 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.jdbc.support.incrementer;
18+
19+
import java.util.UUID;
20+
21+
import javax.sql.DataSource;
22+
23+
import org.junit.jupiter.api.Test;
24+
import org.junit.jupiter.params.ParameterizedTest;
25+
import org.junit.jupiter.params.provider.ValueSource;
26+
27+
import org.springframework.jdbc.core.JdbcTemplate;
28+
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
29+
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
30+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
31+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
32+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
33+
import org.springframework.transaction.PlatformTransactionManager;
34+
import org.springframework.transaction.support.TransactionTemplate;
35+
36+
import static org.assertj.core.api.Assertions.assertThat;
37+
38+
/**
39+
* Integration tests for {@link H2SequenceMaxValueIncrementer}.
40+
*
41+
* @author Henning Pöttker
42+
* @author Sam Brannen
43+
* @since 5.3.15
44+
*/
45+
class H2SequenceMaxValueIncrementerTests {
46+
47+
/**
48+
* Tests that the incrementer works when using the JDBC connection URL used
49+
* in the {@code H2EmbeddedDatabaseConfigurer} which is used transparently
50+
* when using Spring's {@link EmbeddedDatabaseBuilder}.
51+
*
52+
* <p>In other words, this tests compatibility with the default H2
53+
* <em>compatibility mode</em>.
54+
*/
55+
@Test
56+
void incrementsSequenceUsingH2EmbeddedDatabaseConfigurer() {
57+
EmbeddedDatabase database = new EmbeddedDatabaseBuilder()
58+
.setType(EmbeddedDatabaseType.H2)
59+
.generateUniqueName(true)
60+
.addScript("classpath:/org/springframework/jdbc/support/incrementer/schema.sql")
61+
.build();
62+
63+
JdbcTemplate jdbcTemplate = new JdbcTemplate(database);
64+
assertThat(jdbcTemplate.queryForObject("values next value for SEQ", int.class)).isEqualTo(1);
65+
66+
H2SequenceMaxValueIncrementer incrementer = new H2SequenceMaxValueIncrementer(database, "SEQ");
67+
assertThat(incrementer.nextIntValue()).isEqualTo(2);
68+
assertThat(incrementer.nextStringValue()).isEqualTo("3");
69+
70+
database.shutdown();
71+
}
72+
73+
/**
74+
* Tests that the incrementer works when using all supported H2 <em>compatibility modes</em>.
75+
*
76+
* <p>The following modes are only supported with H2 2.x or higher: STRICT, LEGACY, MariaDB
77+
*/
78+
@ParameterizedTest
79+
@ValueSource(strings = { "DB2", "Derby", "HSQLDB", "MSSQLServer", "MySQL", "Oracle", "PostgreSQL" })
80+
void incrementsSequenceWithExplicitH2CompatibilityMode(String compatibilityMode) {
81+
String connectionUrl = String.format("jdbc:h2:mem:%s;MODE=%s", UUID.randomUUID().toString(), compatibilityMode);
82+
DataSource dataSource = new SimpleDriverDataSource(new org.h2.Driver(), connectionUrl, "sa", "");
83+
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
84+
PlatformTransactionManager transactionManager = new DataSourceTransactionManager(dataSource);
85+
TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
86+
87+
transactionTemplate.executeWithoutResult(status -> {
88+
jdbcTemplate.execute("CREATE SEQUENCE SEQ");
89+
assertThat(jdbcTemplate.queryForObject("values next value for SEQ", int.class)).isEqualTo(1);
90+
91+
H2SequenceMaxValueIncrementer incrementer = new H2SequenceMaxValueIncrementer(dataSource, "SEQ");
92+
assertThat(incrementer.nextIntValue()).isEqualTo(2);
93+
assertThat(incrementer.nextStringValue()).isEqualTo("3");
94+
});
95+
96+
jdbcTemplate.execute("SHUTDOWN");
97+
}
98+
99+
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
CREATE TABLE users (
2-
id INTEGER NOT NULL IDENTITY,
2+
id INTEGER GENERATED BY DEFAULT AS IDENTITY,
33
first_name VARCHAR(50) NOT NULL,
44
last_name VARCHAR(50) NOT NULL
55
)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
DROP TABLE users IF EXISTS;
22

33
CREATE TABLE users (
4-
id INTEGER NOT NULL IDENTITY,
4+
id INTEGER GENERATED BY DEFAULT AS IDENTITY,
55
first_name VARCHAR(50) NOT NULL,
66
last_name VARCHAR(50) NOT NULL
77
);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE SEQUENCE SEQ;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
DROP TABLE users IF EXISTS;
22

33
CREATE TABLE users (
4-
id INTEGER NOT NULL IDENTITY,
4+
id INTEGER GENERATED BY DEFAULT AS IDENTITY,
55
first_name VARCHAR(50) NOT NULL,
66
last_name VARCHAR(50) NOT NULL
77
);

spring-test/src/test/resources/org/springframework/test/context/junit4/orm/db-schema.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ DROP TABLE drivers_license IF EXISTS;
22
DROP TABLE person IF EXISTS;
33

44
CREATE TABLE person (
5-
id INTEGER NOT NULL IDENTITY,
5+
id INTEGER GENERATED BY DEFAULT AS IDENTITY,
66
name VARCHAR(50) NOT NULL,
77
drivers_license_id INTEGER NOT NULL
88
);
99
CREATE UNIQUE INDEX person_name ON person(name);
1010
CREATE UNIQUE INDEX person_drivers_license_id ON person(drivers_license_id);
1111

1212
CREATE TABLE drivers_license (
13-
id INTEGER NOT NULL IDENTITY,
13+
id INTEGER GENERATED BY DEFAULT AS IDENTITY,
1414
license_number INTEGER NOT NULL
1515
);
1616
CREATE UNIQUE INDEX drivers_license_license_number ON drivers_license(license_number);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
CREATE TABLE enigma (
2-
id INTEGER NOT NULL IDENTITY
2+
id INTEGER GENERATED BY DEFAULT AS IDENTITY
33
);

0 commit comments

Comments
 (0)