hibernate/hibernate-orm · error · HibernateException

Connection lock-timeout does not accept skip-locked

Error message

Connection lock-timeout does not accept skip-locked

What it means

On SQL Server, Hibernate applies pessimistic-lock timeouts with 'set lock_timeout N' (milliseconds) via TransactSQLLockingSupport.SQLServerImpl. SQL Server's connection lock_timeout is expressive enough for real waits and no-wait (0 ms, Level.EXTENDED), but SKIP_LOCKED (-2 ms) is purely a locking-clause concept with no session setting equivalent, so it is rejected with this HibernateException. Use 'FOR UPDATE SKIP LOCKED' / READPAST-style clause semantics instead.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/dialect/lock/internal/TransactSQLLockingSupport.java:141

						return switch ( timeoutInMilliseconds ) {
							case -1 -> Timeouts.WAIT_FOREVER;
							case 0 -> Timeouts.NO_WAIT;
							default -> Timeout.milliseconds( timeoutInMilliseconds );
						};
					},
					connection,
					factory
			);
		}

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

	public static class SybaseImpl implements ConnectionLockTimeoutStrategy {
		public static final SybaseImpl IMPL = new SybaseImpl();

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

View on GitHub (pinned to fad1729dce)

Solutions

  1. Do not pass SKIP_LOCKED as a timeout; get skip-locked behavior from the locking clause (SQL Server 'FOR UPDATE SKIP LOCKED' equivalent / READPAST) or native SQL
  2. Use a real positive timeout in ms or WAIT_FOREVER (-1) when the connection-level path applies
  3. Guard with getConnectionLockTimeoutStrategy().getSupportedLevel() and reject/adjust magic values below Level.EXTENDED semantics
  4. Remove -2 values from shared lock timeout configuration

Example fix

// before
Map<String, Object> hints = Map.of("jakarta.persistence.lock.timeout", -2); // SKIP_LOCKED -> throws
List<Job> jobs = em.createQuery(...).setLockMode(LockModeType.PESSIMISTIC_WRITE).setHints(hints).getResultList();

// after: plain short wait through 'set lock_timeout'
Map<String, Object> hints = Map.of("jakarta.persistence.lock.timeout", 1000);
Defensive patterns

Strategy: validation

Validate before calling

int millis = lockOptions.getTimeOut();
if (millis == Timeouts.SKIP_LOCKED_MILLI) {
    lockOptions.setTimeOut(1000); // 'set lock_timeout' cannot express skip-locked on SQL Server
}

Type guard

static boolean acceptsConnectionTimeout(ConnectionLockTimeoutStrategy s, int millis) {
    if (s.getSupportedLevel() == ConnectionLockTimeoutStrategy.Level.NONE) return false;
    return millis != Timeouts.SKIP_LOCKED_MILLI; // SQL Server is EXTENDED: no-wait (0) is fine
}

Try / catch

try {
    session.buildLockRequest(lockOptions).lock(entity);
} catch (HibernateException e) {
    if (e.getMessage() != null && e.getMessage().contains("does not accept skip-locked")) {
        lockOptions.setTimeout(1000);
        session.buildLockRequest(lockOptions).lock(entity);
    } else { throw e; }
}

Prevention

When it happens

Trigger: session.buildLockRequest(LockOptions.UPGRADE_SKIPLOCKED).lock(entity) / lockOptions.setTimeOut(-2); em.find(id, PESSIMISTIC_WRITE, hints) or locking queries with 'jakarta.persistence.lock.timeout' = -2 on SQL Server or Sybase dialects routed through the connection-timeout path (LockTimeoutHandler).

Common situations: Work-queue implementations using skip-locked semantics that set the timeout magic value instead of relying on the clause; global lock.timeout = -2 hints in persistence.xml; entity models/portable repos shared across SQL Server and PostgreSQL where the hint works differently; Hibernate version upgrades that moved timeout handling onto ConnectionLockTimeoutStrategy.

Understand the failure class

Related errors


AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22). Data as JSON: /api/errors/c7dfe8d119ae7547. Report an issue: GitHub.