Skip to content

Commit aa05b79

Browse files
committed
DATAJDBC-493 - Polishing.
Using `execute` instead of `query` since we are not interested in the results. Refactoring of the concurrency tests. Make the concurrency tests run with all databases. Added support for DB2. Moved AnsiDialect to the main sources so it can be referenced in other Dialects. Original pull request: #196.
1 parent 04c29f4 commit aa05b79

24 files changed

+174
-261
lines changed

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/JdbcAggregateTemplate.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
* @author Thomas Lang
5151
* @author Christoph Strobl
5252
* @author Milan Milanov
53-
* @author Myeonghyeon Lee
5453
*/
5554
public class JdbcAggregateTemplate implements JdbcAggregateOperations {
5655

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/DataAccessStrategy.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public interface DataAccessStrategy extends RelationResolver {
132132
void deleteAll(PersistentPropertyPath<RelationalPersistentProperty> propertyPath);
133133

134134
/**
135-
* Acquire Lock
135+
* Acquire a lock on the aggregate specified by id.
136136
*
137137
* @param id the id of the entity to load. Must not be {@code null}.
138138
* @param lockMode the lock mode for select. Must not be {@code null}.
@@ -141,7 +141,7 @@ public interface DataAccessStrategy extends RelationResolver {
141141
<T> void acquireLockById(Object id, LockMode lockMode, Class<T> domainType);
142142

143143
/**
144-
* Acquire Lock entities of the given domain type.
144+
* Acquire a lock on all aggregates of the given domain type.
145145
*
146146
* @param lockMode the lock mode for select. Must not be {@code null}.
147147
* @param domainType the domain type of the entity. Must not be {@code null}.

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/DefaultDataAccessStrategy.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static org.springframework.data.jdbc.core.convert.SqlGenerator.*;
1919

2020
import java.sql.JDBCType;
21+
import java.sql.PreparedStatement;
2122
import java.sql.ResultSet;
2223
import java.sql.SQLException;
2324
import java.util.ArrayList;
@@ -27,7 +28,11 @@
2728
import java.util.Map;
2829
import java.util.function.Predicate;
2930

30-
import org.springframework.dao.*;
31+
import org.springframework.dao.DataAccessException;
32+
import org.springframework.dao.DataRetrievalFailureException;
33+
import org.springframework.dao.EmptyResultDataAccessException;
34+
import org.springframework.dao.InvalidDataAccessApiUsageException;
35+
import org.springframework.dao.OptimisticLockingFailureException;
3136
import org.springframework.data.domain.Pageable;
3237
import org.springframework.data.domain.Sort;
3338
import org.springframework.data.jdbc.support.JdbcUtil;
@@ -42,6 +47,7 @@
4247
import org.springframework.data.relational.core.sql.IdentifierProcessing;
4348
import org.springframework.data.relational.core.sql.LockMode;
4449
import org.springframework.data.relational.core.sql.SqlIdentifier;
50+
import org.springframework.jdbc.core.PreparedStatementCallback;
4551
import org.springframework.jdbc.core.ResultSetExtractor;
4652
import org.springframework.jdbc.core.RowMapper;
4753
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
@@ -245,9 +251,10 @@ public void deleteAll(PersistentPropertyPath<RelationalPersistentProperty> prope
245251
*/
246252
@Override
247253
public <T> void acquireLockById(Object id, LockMode lockMode, Class<T> domainType) {
254+
248255
String acquireLockByIdSql = sql(domainType).getAcquireLockById(lockMode);
249256
SqlIdentifierParameterSource parameter = createIdParameterSource(id, domainType);
250-
operations.queryForObject(acquireLockByIdSql, parameter, Object.class);
257+
operations.execute(acquireLockByIdSql, parameter, ps -> {ps.execute(); return null;});
251258
}
252259

253260
/*
@@ -256,8 +263,9 @@ public <T> void acquireLockById(Object id, LockMode lockMode, Class<T> domainTyp
256263
*/
257264
@Override
258265
public <T> void acquireLockAll(LockMode lockMode, Class<T> domainType) {
266+
259267
String acquireLockAllSql = sql(domainType).getAcquireLockAll(lockMode);
260-
operations.query(acquireLockAllSql, Collections.emptyMap(), new NoMappingResultSetExtractor());
268+
operations.getJdbcOperations().execute(acquireLockAllSql);
261269
}
262270

263271
/*
@@ -605,14 +613,4 @@ public T getBean() {
605613
return null;
606614
}
607615
}
608-
609-
/**
610-
* The type No mapping result set extractor.
611-
*/
612-
static class NoMappingResultSetExtractor implements ResultSetExtractor<Object> {
613-
@Override
614-
public Object extractData(ResultSet resultSet) throws SQLException, DataAccessException {
615-
return null;
616-
}
617-
}
618616
}

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/mybatis/MyBatisDataAccessStrategy.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,13 +257,15 @@ public void deleteAll(PersistentPropertyPath<RelationalPersistentProperty> prope
257257
*/
258258
@Override
259259
public <T> void acquireLockById(Object id, LockMode lockMode, Class<T> domainType) {
260+
260261
String statement = namespace(domainType) + ".acquireLockById";
261262
MyBatisContext parameter = new MyBatisContext(id, null, domainType, Collections.emptyMap());
262263

263264
long result = sqlSession().selectOne(statement, parameter);
264265
if (result < 1) {
265-
throw new EmptyResultDataAccessException(
266-
String.format("The lock target does not exist. id: %s, statement: %s", id, statement), 1);
266+
267+
String message = String.format("The lock target does not exist. id: %s, statement: %s", id, statement);
268+
throw new EmptyResultDataAccessException(message, 1);
267269
}
268270
}
269271

@@ -273,6 +275,7 @@ public <T> void acquireLockById(Object id, LockMode lockMode, Class<T> domainTyp
273275
*/
274276
@Override
275277
public <T> void acquireLockAll(LockMode lockMode, Class<T> domainType) {
278+
276279
String statement = namespace(domainType) + ".acquireLockAll";
277280
MyBatisContext parameter = new MyBatisContext(null, null, domainType, Collections.emptyMap());
278281

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/convert/SqlGeneratorFixedNamingStrategyUnitTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@
2323
import org.springframework.data.annotation.Id;
2424
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
2525
import org.springframework.data.jdbc.core.mapping.PersistentPropertyPathTestUtils;
26-
import org.springframework.data.jdbc.testing.AnsiDialect;
26+
import org.springframework.data.relational.core.dialect.AnsiDialect;
2727
import org.springframework.data.mapping.PersistentPropertyPath;
28-
import org.springframework.data.relational.core.dialect.HsqlDbDialect;
2928
import org.springframework.data.relational.core.mapping.NamingStrategy;
3029
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
3130
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/convert/SqlGeneratorUnitTests.java

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.assertj.core.api.SoftAssertions;
2626
import org.junit.Before;
2727
import org.junit.Test;
28-
2928
import org.springframework.data.annotation.Id;
3029
import org.springframework.data.annotation.ReadOnlyProperty;
3130
import org.springframework.data.annotation.Version;
@@ -36,7 +35,7 @@
3635
import org.springframework.data.jdbc.core.mapping.AggregateReference;
3736
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
3837
import org.springframework.data.jdbc.core.mapping.PersistentPropertyPathTestUtils;
39-
import org.springframework.data.jdbc.testing.AnsiDialect;
38+
import org.springframework.data.relational.core.dialect.AnsiDialect;
4039
import org.springframework.data.mapping.PersistentPropertyPath;
4140
import org.springframework.data.relational.core.dialect.Dialect;
4241
import org.springframework.data.relational.core.mapping.Column;
@@ -95,48 +94,45 @@ public void findOne() {
9594

9695
String sql = sqlGenerator.getFindOne();
9796

98-
SoftAssertions softAssertions = new SoftAssertions();
99-
softAssertions.assertThat(sql) //
100-
.startsWith("SELECT") //
101-
.contains("dummy_entity.id1 AS id1,") //
102-
.contains("dummy_entity.x_name AS x_name,") //
103-
.contains("dummy_entity.x_other AS x_other,") //
104-
.contains("ref.x_l1id AS ref_x_l1id") //
105-
.contains("ref.x_content AS ref_x_content").contains(" FROM dummy_entity") //
106-
.contains("ON ref.dummy_entity = dummy_entity.id1") //
107-
.contains("WHERE dummy_entity.id1 = :id") //
108-
// 1-N relationships do not get loaded via join
109-
.doesNotContain("Element AS elements");
110-
softAssertions.assertAll();
97+
SoftAssertions.assertSoftly(softly -> softly //
98+
.assertThat(sql) //
99+
.startsWith("SELECT") //
100+
.contains("dummy_entity.id1 AS id1,") //
101+
.contains("dummy_entity.x_name AS x_name,") //
102+
.contains("dummy_entity.x_other AS x_other,") //
103+
.contains("ref.x_l1id AS ref_x_l1id") //
104+
.contains("ref.x_content AS ref_x_content").contains(" FROM dummy_entity") //
105+
.contains("ON ref.dummy_entity = dummy_entity.id1") //
106+
.contains("WHERE dummy_entity.id1 = :id") //
107+
// 1-N relationships do not get loaded via join
108+
.doesNotContain("Element AS elements"));
111109
}
112110

113111
@Test // DATAJDBC-493
114112
public void getAcquireLockById() {
115113

116114
String sql = sqlGenerator.getAcquireLockById(LockMode.PESSIMISTIC_WRITE);
117115

118-
SoftAssertions softAssertions = new SoftAssertions();
119-
softAssertions.assertThat(sql) //
120-
.startsWith("SELECT") //
121-
.contains("dummy_entity.id1") //
122-
.contains("WHERE dummy_entity.id1 = :id") //
123-
.contains("FOR UPDATE") //
124-
.doesNotContain("Element AS elements");
125-
softAssertions.assertAll();
116+
SoftAssertions.assertSoftly(softly -> softly //
117+
.assertThat(sql) //
118+
.startsWith("SELECT") //
119+
.contains("dummy_entity.id1") //
120+
.contains("WHERE dummy_entity.id1 = :id") //
121+
.contains("FOR UPDATE") //
122+
.doesNotContain("Element AS elements"));
126123
}
127124

128125
@Test // DATAJDBC-493
129126
public void getAcquireLockAll() {
130127

131128
String sql = sqlGenerator.getAcquireLockAll(LockMode.PESSIMISTIC_WRITE);
132129

133-
SoftAssertions softAssertions = new SoftAssertions();
134-
softAssertions.assertThat(sql) //
135-
.startsWith("SELECT") //
136-
.contains("dummy_entity.id1") //
137-
.contains("FOR UPDATE") //
138-
.doesNotContain("Element AS elements");
139-
softAssertions.assertAll();
130+
SoftAssertions.assertSoftly(softly -> softly //
131+
.assertThat(sql) //
132+
.startsWith("SELECT") //
133+
.contains("dummy_entity.id1") //
134+
.contains("FOR UPDATE") //
135+
.doesNotContain("Element AS elements"));
140136
}
141137

142138
@Test // DATAJDBC-112

0 commit comments

Comments
 (0)