|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.community.dialect.lock.internal; |
| 6 | + |
| 7 | +import jakarta.persistence.Timeout; |
| 8 | +import org.hibernate.dialect.RowLockStrategy; |
| 9 | +import org.hibernate.dialect.lock.spi.ConnectionLockTimeoutStrategy; |
| 10 | +import org.hibernate.dialect.lock.spi.LockTimeoutType; |
| 11 | +import org.hibernate.dialect.lock.spi.LockingSupport; |
| 12 | +import org.hibernate.dialect.lock.spi.OuterJoinLockingType; |
| 13 | + |
| 14 | + |
| 15 | +import static org.hibernate.Timeouts.NO_WAIT_MILLI; |
| 16 | +import static org.hibernate.Timeouts.SKIP_LOCKED_MILLI; |
| 17 | +import static org.hibernate.Timeouts.WAIT_FOREVER_MILLI; |
| 18 | +import static org.hibernate.dialect.lock.spi.LockTimeoutType.QUERY; |
| 19 | + |
| 20 | +/** |
| 21 | + * @author chen zhida |
| 22 | + * |
| 23 | + * Notes: Original code of this class is based on PostgreSQLLockingSupport. |
| 24 | + */ |
| 25 | +public class GaussDBLockingSupport implements LockingSupport, LockingSupport.Metadata, ConnectionLockTimeoutStrategy { |
| 26 | + public static final LockingSupport LOCKING_SUPPORT = new GaussDBLockingSupport(); |
| 27 | + private final boolean supportsNoWait; |
| 28 | + private final boolean supportsSkipLocked; |
| 29 | + |
| 30 | + public GaussDBLockingSupport() { |
| 31 | + this( true, true ); |
| 32 | + } |
| 33 | + |
| 34 | + public GaussDBLockingSupport(boolean supportsNoWait, boolean supportsSkipLocked) { |
| 35 | + this.supportsNoWait = supportsNoWait; |
| 36 | + this.supportsSkipLocked = supportsSkipLocked; |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public Metadata getMetadata() { |
| 41 | + return this; |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public RowLockStrategy getWriteRowLockStrategy() { |
| 46 | + return RowLockStrategy.TABLE; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public LockTimeoutType getLockTimeoutType(Timeout timeout) { |
| 51 | + return switch ( timeout.milliseconds() ) { |
| 52 | + case NO_WAIT_MILLI -> supportsNoWait ? QUERY : LockTimeoutType.NONE; |
| 53 | + case SKIP_LOCKED_MILLI -> supportsSkipLocked ? QUERY : LockTimeoutType.NONE; |
| 54 | + case WAIT_FOREVER_MILLI -> LockTimeoutType.NONE; |
| 55 | + // we can apply a timeout via the connection |
| 56 | + default -> LockTimeoutType.CONNECTION; |
| 57 | + }; |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public OuterJoinLockingType getOuterJoinLockingType() { |
| 62 | + return OuterJoinLockingType.UNSUPPORTED; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public ConnectionLockTimeoutStrategy getConnectionLockTimeoutStrategy() { |
| 67 | + return this; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public Level getSupportedLevel() { |
| 72 | + return Level.NONE; |
| 73 | + } |
| 74 | +} |
0 commit comments