Skip to content

DATAJDBC-538 - Fixes lock acquisition for DB2. #216

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
<version>2.0.0.DATAJDBC-538-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data Relational Parent</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-jdbc-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
<version>2.0.0.DATAJDBC-538-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
4 changes: 2 additions & 2 deletions spring-data-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-jdbc</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
<version>2.0.0.DATAJDBC-538-SNAPSHOT</version>

<name>Spring Data JDBC</name>
<description>Spring Data module for JDBC repositories.</description>
Expand All @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
<version>2.0.0.DATAJDBC-538-SNAPSHOT</version>
</parent>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,14 @@
import static org.springframework.data.jdbc.core.convert.SqlGenerator.*;

import java.sql.JDBCType;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;

import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataRetrievalFailureException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
Expand All @@ -40,15 +37,14 @@
import org.springframework.data.mapping.PersistentPropertyAccessor;
import org.springframework.data.mapping.PersistentPropertyPath;
import org.springframework.data.mapping.PropertyHandler;
import org.springframework.data.relational.core.dialect.LockClause;
import org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
import org.springframework.data.relational.core.sql.IdentifierProcessing;
import org.springframework.data.relational.core.sql.LockMode;
import org.springframework.data.relational.core.sql.SqlIdentifier;
import org.springframework.jdbc.core.PreparedStatementCallback;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
Expand Down Expand Up @@ -254,7 +250,8 @@ public <T> void acquireLockById(Object id, LockMode lockMode, Class<T> domainTyp

String acquireLockByIdSql = sql(domainType).getAcquireLockById(lockMode);
SqlIdentifierParameterSource parameter = createIdParameterSource(id, domainType);
operations.execute(acquireLockByIdSql, parameter, ps -> {ps.execute(); return null;});

operations.query(acquireLockByIdSql, parameter, ResultSet::next);
}

/*
Expand All @@ -265,7 +262,7 @@ public <T> void acquireLockById(Object id, LockMode lockMode, Class<T> domainTyp
public <T> void acquireLockAll(LockMode lockMode, Class<T> domainType) {

String acquireLockAllSql = sql(domainType).getAcquireLockAll(lockMode);
operations.getJdbcOperations().execute(acquireLockAllSql);
operations.getJdbcOperations().query(acquireLockAllSql, ResultSet::next);
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,18 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringJoiner;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.function.UnaryOperator;

import org.assertj.core.api.Assertions;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.platform.commons.util.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand Down Expand Up @@ -86,6 +90,35 @@ DummyEntityRepository dummyEntityRepository() {
TransactionTemplate transactionTemplate;
List<Exception> exceptions;

@BeforeClass
public static void beforeClass() {

Assertions.registerFormatterForType(CopyOnWriteArrayList.class, l -> {

StringJoiner joiner = new StringJoiner(", ", "List(", ")");
l.forEach(e -> {

if (e instanceof Throwable) {
printThrowable(joiner,(Throwable) e);
} else {
joiner.add(e.toString());
}
});

return joiner.toString();
});
}

private static void printThrowable(StringJoiner joiner, Throwable t) {

joiner.add(t.toString() + ExceptionUtils.readStackTrace(t));
if (t.getCause() != null) {

joiner.add("\ncaused by:\n");
printThrowable(joiner, t.getCause());
}
}

@Before
public void before() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ protected DataSource createDataSource() {
if (DB_2_CONTAINER == null) {

LOG.info("DB2 starting...");
Db2Container container = new Db2Container();
Db2Container container = new Db2Container().withReuse(true);
container.start();
LOG.info("DB2 started");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,41 @@
DROP TABLE MANUAL;
DROP TABLE LEGO_SET;

DROP TABLE Child_No_Id;
DROP TABLE ONE_TO_ONE_PARENT;

DROP TABLE ELEMENT_NO_ID;
DROP TABLE LIST_PARENT;

DROP TABLE BYTE_ARRAY_OWNER;

DROP TABLE CHAIN0;
DROP TABLE CHAIN1;
DROP TABLE CHAIN2;
DROP TABLE CHAIN3;
DROP TABLE CHAIN4;

DROP TABLE NO_ID_CHAIN0;
DROP TABLE NO_ID_CHAIN1;
DROP TABLE NO_ID_CHAIN2;
DROP TABLE NO_ID_CHAIN3;
DROP TABLE NO_ID_CHAIN4;

DROP TABLE NO_ID_MAP_CHAIN0;
DROP TABLE NO_ID_MAP_CHAIN1;
DROP TABLE NO_ID_MAP_CHAIN2;
DROP TABLE NO_ID_MAP_CHAIN3;
DROP TABLE NO_ID_MAP_CHAIN4;

DROP TABLE NO_ID_LIST_CHAIN0;
DROP TABLE NO_ID_LIST_CHAIN1;
DROP TABLE NO_ID_LIST_CHAIN2;
DROP TABLE NO_ID_LIST_CHAIN3;
DROP TABLE NO_ID_LIST_CHAIN4;

DROP TABLE WITH_READ_ONLY;
DROP TABLE VERSIONED_AGGREGATE;

CREATE TABLE LEGO_SET
(
"id1" BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH 1) PRIMARY KEY,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
DROP TABLE element;
DROP TABLE dummy_entity;

CREATE TABLE dummy_entity ( id BIGINT GENERATED BY DEFAULT AS IDENTITY ( START WITH 1 ) NOT NULL PRIMARY KEY, NAME VARCHAR(100));
CREATE TABLE element (id BIGINT GENERATED BY DEFAULT AS IDENTITY ( START WITH 1 ) NOT NULL PRIMARY KEY, content BIGINT, Dummy_Entity_key BIGINT,dummy_entity BIGINT);
4 changes: 2 additions & 2 deletions spring-data-relational/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-relational</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
<version>2.0.0.DATAJDBC-538-SNAPSHOT</version>

<name>Spring Data Relational</name>
<description>Spring Data Relational support</description>

<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
<version>2.0.0.DATAJDBC-538-SNAPSHOT</version>
</parent>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protected Db2Dialect() {}
*/
@Override
public String getLimit(long limit) {
return "FIRST " + limit + " ROWS ONLY";
return "FETCH FIRST " + limit + " ROWS ONLY";
}

/*
Expand Down Expand Up @@ -83,7 +83,18 @@ public LimitClause limit() {

@Override
public LockClause lock() {
return AnsiDialect.LOCK_CLAUSE;

return new LockClause() {
@Override
public String getLock(LockOptions lockOptions) {
return "FOR UPDATE WITH RS USE AND KEEP EXCLUSIVE LOCKS";
}

@Override
public Position getClausePosition() {
return Position.AFTER_ORDER_BY;
}
};
}

/*
Expand Down