Skip to content

HHH-19682 - Add Support for GaussDB Lock Timeout (lockwait_timeout) #10693

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

Merged
merged 2 commits into from
Aug 6, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,23 @@
*/
package org.hibernate.community.dialect.lock.internal;

import jakarta.persistence.Timeout;
import org.hibernate.HibernateException;
import org.hibernate.Timeouts;
import org.hibernate.dialect.RowLockStrategy;
import org.hibernate.dialect.lock.internal.Helper;
import org.hibernate.dialect.lock.spi.ConnectionLockTimeoutStrategy;
import org.hibernate.dialect.lock.spi.LockTimeoutType;
import org.hibernate.dialect.lock.spi.LockingSupport;
import org.hibernate.dialect.lock.spi.OuterJoinLockingType;
import org.hibernate.engine.spi.SessionFactoryImplementor;

import java.sql.Connection;

import static org.hibernate.Timeouts.NO_WAIT_MILLI;
import static org.hibernate.Timeouts.SKIP_LOCKED_MILLI;
import static org.hibernate.Timeouts.WAIT_FOREVER_MILLI;
import static org.hibernate.dialect.lock.spi.LockTimeoutType.QUERY;


/**
Expand All @@ -18,7 +30,17 @@
*/
public class GaussDBLockingSupport implements LockingSupport, LockingSupport.Metadata, ConnectionLockTimeoutStrategy {
public static final LockingSupport LOCKING_SUPPORT = new GaussDBLockingSupport();
private final boolean supportsNoWait;
private final boolean supportsSkipLocked;

public GaussDBLockingSupport() {
this( true, true );
}

public GaussDBLockingSupport(boolean supportsNoWait, boolean supportsSkipLocked) {
this.supportsNoWait = supportsNoWait;
this.supportsSkipLocked = supportsSkipLocked;
}

@Override
public Metadata getMetadata() {
Expand All @@ -42,6 +64,82 @@ public ConnectionLockTimeoutStrategy getConnectionLockTimeoutStrategy() {

@Override
public Level getSupportedLevel() {
return Level.NONE;
return Level.SUPPORTED;
}

@Override
public LockTimeoutType getLockTimeoutType(Timeout timeout) {
return switch ( timeout.milliseconds() ) {
case NO_WAIT_MILLI -> supportsNoWait ? QUERY : LockTimeoutType.NONE;
case SKIP_LOCKED_MILLI -> supportsSkipLocked ? QUERY : LockTimeoutType.NONE;
case WAIT_FOREVER_MILLI -> LockTimeoutType.NONE;
// we can apply a timeout via the connection
default -> LockTimeoutType.CONNECTION;
};
}

@Override
public Timeout getLockTimeout(Connection connection, SessionFactoryImplementor factory) {
return Helper.getLockTimeout(
"select current_setting('lockwait_timeout')",
(resultSet) -> {
// even though lock_timeout is "in milliseconds", `current_setting`
// returns a String form which unfortunately varies depending on
// the actual value:
// * for zero (no timeout), "0" is returned
// * for non-zero, `{timeout-in-seconds}s` is returned (e.g. "4s")
// so we need to "parse" that form here
final String value = resultSet.getString( 1 );
if ( "0".equals( value ) ) {
return Timeouts.WAIT_FOREVER;
}
if ( value.endsWith( "min" ) ) {
final int minute = getTimeout( value, 3 );
return Timeout.milliseconds( minute * 60 * 1000);
}
else if ( value.endsWith( "s" ) ) {
final int seconds = getTimeout( value, 1 );
return Timeout.seconds(seconds);
}
final int milliseconds = getTimeout( value, 2 );
return Timeout.milliseconds(milliseconds);
},
connection,
factory
);
}

private static int getTimeout(String value, int unitLength) {
final int number;
try {
number = Integer.parseInt( value.substring( 0, value.length() - unitLength ) );
}
catch (NumberFormatException e) {
throw new RuntimeException( e );
}
return number;
}

@Override
public void setLockTimeout(Timeout timeout, Connection connection, SessionFactoryImplementor factory) {
Helper.setLockTimeout(
timeout,
(t) -> {
final int milliseconds = timeout.milliseconds();
if ( milliseconds == SKIP_LOCKED_MILLI ) {
throw new HibernateException( "Connection lock-timeout does not accept skip-locked" );
}

if ( milliseconds == NO_WAIT_MILLI ) {
throw new HibernateException( "Connection lock-timeout does not accept no-wait" );
}
return milliseconds == WAIT_FOREVER_MILLI
? 0
: milliseconds;
},
"set local lockwait_timeout = %s",
connection,
factory
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import jakarta.persistence.Timeout;
import org.hibernate.Timeouts;
import org.hibernate.community.dialect.GaussDBDialect;
import org.hibernate.dialect.MySQLDialect;
import org.hibernate.dialect.lock.spi.ConnectionLockTimeoutStrategy;
import org.hibernate.dialect.lock.spi.LockingSupport;
Expand Down Expand Up @@ -35,6 +36,9 @@ void testSimpleUsage(SessionFactoryScope factoryScope) {
if ( session.getDialect() instanceof MySQLDialect ) {
expectedInitialValue = 50;
}
else if ( session.getDialect() instanceof GaussDBDialect ) {
expectedInitialValue = 20 * 60 * 1000;
}
else {
expectedInitialValue = Timeouts.WAIT_FOREVER_MILLI;
}
Expand Down