Skip to content

Commit e41ab64

Browse files
committed
Fix lock handling for HANA
1 parent 4894a7e commit e41ab64

File tree

4 files changed

+48
-97
lines changed

4 files changed

+48
-97
lines changed

hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/HANALegacyDialect.java

Lines changed: 15 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public HANALegacyDialect(HANAServerConfiguration configuration, boolean defaultT
205205
this.maxLobPrefetchSize = configuration.getMaxLobPrefetchSize();
206206
this.useUnicodeStringTypes = useUnicodeStringTypesDefault();
207207

208-
this.lockingSupport = buildLockingSupport();
208+
this.lockingSupport = HANALockingSupport.forDialectVersion( configuration.getFullVersion() );
209209
}
210210

211211
private LockingSupport buildLockingSupport() {
@@ -998,34 +998,18 @@ public String getReadLockString(Timeout timeout) {
998998
}
999999

10001000
@Override
1001-
public String getReadLockString(String aliases, Timeout timeout) {
1002-
return getWriteLockString( aliases, timeout );
1001+
public String getForUpdateString(Timeout timeout) {
1002+
return withTimeout( getForUpdateString(), timeout.milliseconds() );
10031003
}
10041004

10051005
@Override
1006-
public String getWriteLockString(Timeout timeout) {
1007-
if ( Timeouts.isRealTimeout( timeout ) ) {
1008-
return getForUpdateString() + " wait " + getTimeoutInSeconds( timeout.milliseconds() );
1009-
}
1010-
else if ( timeout.milliseconds() == Timeouts.NO_WAIT_MILLI ) {
1011-
return getForUpdateNowaitString();
1012-
}
1013-
else {
1014-
return getForUpdateString();
1015-
}
1006+
public String getReadLockString(String aliases, Timeout timeout) {
1007+
return getWriteLockString( aliases, timeout );
10161008
}
10171009

10181010
@Override
10191011
public String getWriteLockString(String aliases, Timeout timeout) {
1020-
if ( Timeouts.isRealTimeout( timeout ) ) {
1021-
return getForUpdateString( aliases ) + " wait " + getTimeoutInSeconds( timeout.milliseconds() );
1022-
}
1023-
else if ( timeout.milliseconds() == Timeouts.NO_WAIT_MILLI ) {
1024-
return getForUpdateNowaitString( aliases );
1025-
}
1026-
else {
1027-
return getForUpdateString( aliases );
1028-
}
1012+
return withTimeout( getForUpdateString( aliases ), timeout.milliseconds() );
10291013
}
10301014

10311015
@Override
@@ -1039,29 +1023,17 @@ public String getReadLockString(String aliases, int timeout) {
10391023
}
10401024

10411025
@Override
1042-
public String getWriteLockString(int timeout) {
1043-
if ( Timeouts.isRealTimeout( timeout ) ) {
1044-
return getForUpdateString() + " wait " + Timeouts.getTimeoutInSeconds( timeout );
1045-
}
1046-
else if ( timeout == Timeouts.NO_WAIT_MILLI ) {
1047-
return getForUpdateNowaitString();
1048-
}
1049-
else {
1050-
return getForUpdateString();
1051-
}
1026+
public String getWriteLockString(String aliases, int timeout) {
1027+
return withTimeout( getForUpdateString( aliases ), timeout );
10521028
}
10531029

1054-
@Override
1055-
public String getWriteLockString(String aliases, int timeout) {
1056-
if ( timeout > 0 ) {
1057-
return getForUpdateString( aliases ) + " wait " + getTimeoutInSeconds( timeout );
1058-
}
1059-
else if ( timeout == 0 ) {
1060-
return getForUpdateNowaitString( aliases );
1061-
}
1062-
else {
1063-
return getForUpdateString( aliases );
1064-
}
1030+
private String withTimeout(String lockString, int timeout) {
1031+
return switch (timeout) {
1032+
case Timeouts.NO_WAIT_MILLI -> supportsNoWait() ? lockString + " nowait" : lockString;
1033+
case Timeouts.SKIP_LOCKED_MILLI -> supportsSkipLocked() ? lockString + SQL_IGNORE_LOCKED : lockString;
1034+
case Timeouts.WAIT_FOREVER_MILLI -> lockString;
1035+
default -> supportsWait() ? lockString + " wait " + getTimeoutInSeconds( timeout ) : lockString;
1036+
};
10651037
}
10661038

10671039
@Override
@@ -2006,11 +1978,6 @@ public String getForUpdateSkipLockedString(String aliases) {
20061978
getForUpdateString(aliases) + SQL_IGNORE_LOCKED : getForUpdateString(aliases);
20071979
}
20081980

2009-
@Override
2010-
public String getForUpdateString(LockMode lockMode) {
2011-
return super.getForUpdateString(lockMode);
2012-
}
2013-
20141981
@Override
20151982
public String getDual() {
20161983
return "sys.dummy";

hibernate-core/src/main/java/org/hibernate/dialect/HANADialect.java

Lines changed: 14 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,34 +1003,18 @@ public String getReadLockString(Timeout timeout) {
10031003
}
10041004

10051005
@Override
1006-
public String getReadLockString(String aliases, Timeout timeout) {
1007-
return getWriteLockString( aliases, timeout );
1006+
public String getForUpdateString(Timeout timeout) {
1007+
return withTimeout( getForUpdateString(), timeout.milliseconds() );
10081008
}
10091009

10101010
@Override
1011-
public String getWriteLockString(Timeout timeout) {
1012-
if ( Timeouts.isRealTimeout( timeout ) ) {
1013-
return getForUpdateString() + " wait " + Timeouts.getTimeoutInSeconds( timeout.milliseconds() );
1014-
}
1015-
else if ( timeout.milliseconds() == Timeouts.NO_WAIT_MILLI ) {
1016-
return getForUpdateNowaitString();
1017-
}
1018-
else {
1019-
return getForUpdateString();
1020-
}
1011+
public String getReadLockString(String aliases, Timeout timeout) {
1012+
return getWriteLockString( aliases, timeout );
10211013
}
10221014

10231015
@Override
10241016
public String getWriteLockString(String aliases, Timeout timeout) {
1025-
if ( Timeouts.isRealTimeout( timeout ) ) {
1026-
return getForUpdateString( aliases ) + " wait " + getTimeoutInSeconds( timeout.milliseconds() );
1027-
}
1028-
else if ( timeout.milliseconds() == Timeouts.NO_WAIT_MILLI ) {
1029-
return getForUpdateNowaitString( aliases );
1030-
}
1031-
else {
1032-
return getForUpdateString( aliases );
1033-
}
1017+
return withTimeout( getForUpdateString( aliases ), timeout.milliseconds() );
10341018
}
10351019

10361020
@Override
@@ -1044,29 +1028,17 @@ public String getReadLockString(String aliases, int timeout) {
10441028
}
10451029

10461030
@Override
1047-
public String getWriteLockString(int timeout) {
1048-
if ( timeout > 0 ) {
1049-
return getForUpdateString() + " wait " + getTimeoutInSeconds( timeout );
1050-
}
1051-
else if ( timeout == Timeouts.NO_WAIT_MILLI ) {
1052-
return getForUpdateNowaitString();
1053-
}
1054-
else {
1055-
return getForUpdateString();
1056-
}
1031+
public String getWriteLockString(String aliases, int timeout) {
1032+
return withTimeout( getForUpdateString( aliases ), timeout );
10571033
}
10581034

1059-
@Override
1060-
public String getWriteLockString(String aliases, int timeout) {
1061-
if ( timeout > 0 ) {
1062-
return getForUpdateString( aliases ) + " wait " + getTimeoutInSeconds( timeout );
1063-
}
1064-
else if ( timeout == 0 ) {
1065-
return getForUpdateNowaitString( aliases );
1066-
}
1067-
else {
1068-
return getForUpdateString( aliases );
1069-
}
1035+
private String withTimeout(String lockString, int timeout) {
1036+
return switch (timeout) {
1037+
case Timeouts.NO_WAIT_MILLI -> supportsNoWait() ? lockString + " nowait" : lockString;
1038+
case Timeouts.SKIP_LOCKED_MILLI -> supportsSkipLocked() ? lockString + SQL_IGNORE_LOCKED : lockString;
1039+
case Timeouts.WAIT_FOREVER_MILLI -> lockString;
1040+
default -> supportsWait() ? lockString + " wait " + getTimeoutInSeconds( timeout ) : lockString;
1041+
};
10701042
}
10711043

10721044
@Override

hibernate-core/src/main/java/org/hibernate/dialect/lock/internal/HANALockingSupport.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
*/
55
package org.hibernate.dialect.lock.internal;
66

7+
import org.hibernate.dialect.DatabaseVersion;
78
import org.hibernate.dialect.RowLockStrategy;
89
import org.hibernate.dialect.lock.PessimisticLockStyle;
910
import org.hibernate.dialect.lock.spi.ConnectionLockTimeoutStrategy;
11+
import org.hibernate.dialect.lock.spi.LockingSupport;
1012
import org.hibernate.dialect.lock.spi.OuterJoinLockingType;
1113

1214
/**
@@ -15,14 +17,24 @@
1517
* @author Steve Ebersole
1618
*/
1719
public class HANALockingSupport extends LockingSupportParameterized {
18-
public static final HANALockingSupport HANA_LOCKING_SUPPORT = new HANALockingSupport( true );
20+
public static final HANALockingSupport HANA_LOCKING_SUPPORT = new HANALockingSupport( true, true );
21+
22+
public static LockingSupport forDialectVersion(DatabaseVersion version) {
23+
final boolean supportsWait = version.isSameOrAfter( 2, 0, 10 );
24+
final boolean supportsSkipLocked = version.isSameOrAfter(2, 0, 30);
25+
return new HANALockingSupport( supportsWait, supportsSkipLocked );
26+
}
1927

2028
public HANALockingSupport(boolean supportsSkipLocked) {
29+
this( false, supportsSkipLocked );
30+
}
31+
32+
private HANALockingSupport(boolean supportsWait, boolean supportsSkipLocked) {
2133
super(
2234
PessimisticLockStyle.CLAUSE,
2335
RowLockStrategy.COLUMN,
24-
false,
25-
false,
36+
supportsWait,
37+
supportsWait,
2638
supportsSkipLocked,
2739
OuterJoinLockingType.IDENTIFIED
2840
);

hibernate-core/src/test/java/org/hibernate/orm/test/dialect/unit/locktimeout/HANALockTimeoutTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ public void testLockTimeoutNoAliasNoWait() {
5555
@Test
5656
public void testLockTimeoutNoAliasSkipLocked() {
5757
assertEquals(
58-
" for update",
58+
" for update ignore locked",
5959
dialect.getForUpdateString( new LockOptions( LockMode.PESSIMISTIC_READ ).setTimeout( SKIP_LOCKED ) )
6060
);
6161
assertEquals(
62-
" for update",
62+
" for update ignore locked",
6363
dialect.getForUpdateString( new LockOptions( LockMode.PESSIMISTIC_WRITE ).setTimeout( SKIP_LOCKED ) )
6464
);
6565
}
@@ -100,15 +100,15 @@ public void testLockTimeoutAliasNoWait() {
100100
public void testLockTimeoutAliasSkipLocked() {
101101
String alias = "a";
102102
assertEquals(
103-
" for update of a",
103+
" for update of a ignore locked",
104104
dialect.getForUpdateString(
105105
alias,
106106
new LockOptions( LockMode.PESSIMISTIC_READ )
107107
.setTimeout( SKIP_LOCKED )
108108
)
109109
);
110110
assertEquals(
111-
" for update of a",
111+
" for update of a ignore locked",
112112
dialect.getForUpdateString(
113113
alias,
114114
new LockOptions( LockMode.PESSIMISTIC_WRITE )

0 commit comments

Comments
 (0)