Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*/
package org.hibernate.community.dialect;

import java.lang.invoke.MethodHandles;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
Expand Down Expand Up @@ -47,7 +46,6 @@
import org.hibernate.exception.spi.SQLExceptionConversionDelegate;
import org.hibernate.exception.spi.TemplatedViolatedConstraintNameExtractor;
import org.hibernate.exception.spi.ViolatedConstraintNameExtractor;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.JdbcExceptionHelper;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.query.SemanticException;
Expand Down Expand Up @@ -76,7 +74,6 @@
import org.hibernate.type.descriptor.sql.spi.DdlTypeRegistry;
import org.hibernate.type.spi.TypeConfiguration;

import org.jboss.logging.Logger;

import jakarta.persistence.GenerationType;
import jakarta.persistence.TemporalType;
Expand Down Expand Up @@ -124,7 +121,6 @@
*/
public class CockroachLegacyDialect extends Dialect {

private static final CoreMessageLogger LOG = Logger.getMessageLogger( MethodHandles.lookup(), CoreMessageLogger.class, CockroachLegacyDialect.class.getName() );
// KNOWN LIMITATIONS:
// * no support for java.sql.Clob

Expand Down Expand Up @@ -188,11 +184,6 @@ protected static DatabaseVersion parseVersion(String versionString ) {
}
if ( databaseVersion == null ) {
// Recur to the default version of the no-args constructor
LOG.unableToDetermineCockroachDatabaseVersion(
DEFAULT_VERSION.getDatabaseMajorVersion() + "." +
DEFAULT_VERSION.getDatabaseMinorVersion() + "." +
DEFAULT_VERSION.getDatabaseMicroVersion()
);
databaseVersion = DEFAULT_VERSION;
}
return databaseVersion;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*
* @see jakarta.persistence.Query#getSingleResult
* @see org.hibernate.query.SelectionQuery#getSingleResult
* @see jakarta.persistence.NonUniqueResultException
*/
public class NonUniqueResultException extends HibernateException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@
/**
* Thrown when a pessimistic locking conflict occurs.
*
* @apiNote When a conflict is detected while acquiring a database-level lock,
* {@link org.hibernate.exception.LockAcquisitionException} is preferred.
*
* @author Scott Marlow
*
* @see jakarta.persistence.PessimisticLockException
* @see org.hibernate.exception.LockAcquisitionException
*/
public class PessimisticLockException extends JDBCException {
/**
Expand All @@ -22,4 +28,14 @@ public class PessimisticLockException extends JDBCException {
public PessimisticLockException(String message, SQLException sqlException, String sql) {
super( message, sqlException, sql );
}
/**
* Constructs a {@code PessimisticLockException} using the specified information.
*
* @param message A message explaining the exception condition
* @param sqlException The underlying SQL exception
*/
public PessimisticLockException(String message, SQLException sqlException) {
super( message, sqlException );

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*
* @see jakarta.persistence.Query#setTimeout
* @see org.hibernate.query.CommonQueryContract#setTimeout
* @see jakarta.persistence.QueryTimeoutException
*/
public class QueryTimeoutException extends JDBCException {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
* does not exist.
*
* @author Gavin King
*
* @see jakarta.persistence.OptimisticLockException
*/
public class StaleStateException extends HibernateException {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*/
package org.hibernate.dialect;

import java.lang.invoke.MethodHandles;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
Expand Down Expand Up @@ -48,7 +47,6 @@
import org.hibernate.exception.spi.SQLExceptionConversionDelegate;
import org.hibernate.exception.spi.TemplatedViolatedConstraintNameExtractor;
import org.hibernate.exception.spi.ViolatedConstraintNameExtractor;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.internal.util.config.ConfigurationHelper;
import org.hibernate.query.SemanticException;
Expand Down Expand Up @@ -77,8 +75,6 @@
import org.hibernate.type.descriptor.sql.spi.DdlTypeRegistry;
import org.hibernate.type.spi.TypeConfiguration;

import org.jboss.logging.Logger;

import jakarta.persistence.GenerationType;
import jakarta.persistence.TemporalType;

Expand Down Expand Up @@ -128,7 +124,6 @@
*/
public class CockroachDialect extends Dialect {

private static final CoreMessageLogger LOG = Logger.getMessageLogger( MethodHandles.lookup(), CoreMessageLogger.class, CockroachDialect.class.getName() );
// KNOWN LIMITATIONS:
// * no support for java.sql.Clob

Expand Down Expand Up @@ -204,11 +199,6 @@ public static DatabaseVersion parseVersion( String versionString ) {
databaseVersion= new SimpleDatabaseVersion( majorVersion, minorVersion, microVersion);
}
if ( databaseVersion == null ) {
LOG.unableToDetermineCockroachDatabaseVersion(
MINIMUM_VERSION.getDatabaseMajorVersion() + "." +
MINIMUM_VERSION.getDatabaseMinorVersion() + "." +
MINIMUM_VERSION.getDatabaseMicroVersion()
);
databaseVersion = MINIMUM_VERSION;
}
return databaseVersion;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.util.TimeZone;

import org.checkerframework.checker.nullness.qual.Nullable;
import org.hibernate.PessimisticLockException;
import org.hibernate.QueryTimeoutException;
import org.hibernate.boot.model.FunctionContributions;
import org.hibernate.boot.model.TypeContributions;
Expand All @@ -38,6 +37,7 @@
import org.hibernate.exception.ConstraintViolationException;
import org.hibernate.exception.ConstraintViolationException.ConstraintKind;
import org.hibernate.exception.LockAcquisitionException;
import org.hibernate.exception.LockTimeoutException;
import org.hibernate.exception.spi.SQLExceptionConversionDelegate;
import org.hibernate.exception.spi.TemplatedViolatedConstraintNameExtractor;
import org.hibernate.exception.spi.ViolatedConstraintNameExtractor;
Expand Down Expand Up @@ -810,7 +810,7 @@ public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
new LockAcquisitionException(message, sqlException, sql);
case 50200 ->
// LOCK NOT AVAILABLE
new PessimisticLockException(message, sqlException, sql);
new LockTimeoutException(message, sqlException, sql);
case 23505 ->
// Unique index or primary key violation
new ConstraintViolationException( message, sqlException, sql, ConstraintKind.UNIQUE,
Expand All @@ -828,6 +828,7 @@ public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
new ConstraintViolationException( message, sqlException, sql, ConstraintKind.CHECK,
getViolatedConstraintNameExtractor().extractConstraintName( sqlException ) );
case 57014 ->
// QUERY CANCELLED
new QueryTimeoutException( message, sqlException, sql );
default -> null;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import java.sql.Types;
import java.util.Set;

import org.hibernate.PessimisticLockException;
import org.hibernate.QueryTimeoutException;
import org.hibernate.boot.model.FunctionContributions;
import org.hibernate.boot.model.TypeContributions;
import org.hibernate.dialect.aggregate.AggregateSupport;
Expand All @@ -27,6 +27,7 @@
import org.hibernate.exception.ConstraintViolationException;
import org.hibernate.exception.ConstraintViolationException.ConstraintKind;
import org.hibernate.exception.LockAcquisitionException;
import org.hibernate.exception.SnapshotIsolationException;
import org.hibernate.exception.LockTimeoutException;
import org.hibernate.exception.spi.SQLExceptionConversionDelegate;
import org.hibernate.exception.spi.TemplatedViolatedConstraintNameExtractor;
Expand Down Expand Up @@ -348,21 +349,24 @@ public ViolatedConstraintNameExtractor getViolatedConstraintNameExtractor() {
case 1048 -> extractUsingTemplate( "Column '", "'", sqle.getMessage() );
default -> null;
} );

@Override
public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
return (sqlException, message, sql) -> {
switch ( sqlException.getErrorCode() ) {
// If @@innodb_snapshot_isolation is set (default since 11.6.2),
// if an attempt to acquire a lock on a record that does not exist in the current read view is made,
// an error DB_RECORD_CHANGED will be raised.
case 1205: // ER_LOCK_WAIT_TIMEOUT
return new LockTimeoutException( message, sqlException, sql );
case 1020:
// If @@innodb_snapshot_isolation is set (default since 11.6.2),
// and an attempt to acquire a lock on a record that does not exist
// in the current read view is made, error DB_RECORD_CHANGED is raised
return new SnapshotIsolationException( message, sqlException, sql );
case 3572: // ER_LOCK_NOWAIT
case 1207: // ER_READ_ONLY_TRANSACTION
case 1206: // ER_LOCK_TABLE_FULL
return new LockAcquisitionException( message, sqlException, sql );
case 1205:
case 3572:
return new PessimisticLockException( message, sqlException, sql );
case 1207:
case 1206:
return new LockAcquisitionException( message, sqlException, sql );
case 3024: // ER_QUERY_TIMEOUT
return new QueryTimeoutException( message, sqlException, sql );
case 1062:
// Unique constraint violation
return new ConstraintViolationException( message, sqlException, sql, ConstraintKind.UNIQUE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import org.hibernate.Length;
import org.hibernate.LockOptions;
import org.hibernate.PessimisticLockException;
import org.hibernate.QueryTimeoutException;
import org.hibernate.boot.model.FunctionContributions;
import org.hibernate.boot.model.TypeContributions;
import org.hibernate.cfg.AvailableSettings;
Expand Down Expand Up @@ -1243,12 +1243,15 @@ public boolean supportsLockTimeouts() {
public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
return (sqlException, message, sql) -> {
switch ( sqlException.getErrorCode() ) {
case 1205:
case 3572:
return new PessimisticLockException( message, sqlException, sql );
case 1207:
case 1206:
case 1205: // ER_LOCK_WAIT_TIMEOUT
return new LockTimeoutException( message, sqlException, sql );
case 3572: // ER_LOCK_NOWAIT
case 1207: // ER_READ_ONLY_TRANSACTION
case 1206: // ER_LOCK_TABLE_FULL
return new LockAcquisitionException( message, sqlException, sql );
case 3024: // ER_QUERY_TIMEOUT
case 1317: // ER_QUERY_INTERRUPTED
return new QueryTimeoutException( message, sqlException, sql );
case 1062:
// Unique constraint violation
return new ConstraintViolationException( message, sqlException, sql, ConstraintKind.UNIQUE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.hibernate.Length;
import org.hibernate.LockMode;
import org.hibernate.LockOptions;
import org.hibernate.PessimisticLockException;
import org.hibernate.QueryTimeoutException;
import org.hibernate.boot.model.FunctionContributions;
import org.hibernate.boot.model.TypeContributions;
Expand All @@ -46,6 +45,7 @@
import org.hibernate.engine.jdbc.env.spi.NameQualifierSupport;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.exception.LockAcquisitionException;
import org.hibernate.exception.LockTimeoutException;
import org.hibernate.exception.spi.SQLExceptionConversionDelegate;
import org.hibernate.exception.spi.TemplatedViolatedConstraintNameExtractor;
import org.hibernate.exception.spi.ViolatedConstraintNameExtractor;
Expand Down Expand Up @@ -1041,13 +1041,13 @@ public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
final String sqlState = extractSqlState( sqlException );
if ( sqlState != null ) {
switch ( sqlState ) {
case "40P01":
// DEADLOCK DETECTED
case "40P01": // DEADLOCK DETECTED
return new LockAcquisitionException( message, sqlException, sql );
case "55P03":
// LOCK NOT AVAILABLE
return new PessimisticLockException( message, sqlException, sql );
case "57014":
case "55P03": // LOCK NOT AVAILABLE
//TODO: should we check that the message is "canceling statement due to lock timeout"
// and return LockAcquisitionException if it is not?
return new LockTimeoutException( message, sqlException, sql );
case "57014": // QUERY CANCELLED
return new QueryTimeoutException( message, sqlException, sql );
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
* Represents an error trying to apply an optimistic {@link LockingStrategy} to an entity
*
* @author Steve Ebersole
*
* @see jakarta.persistence.OptimisticLockException
*/
public class OptimisticEntityLockException extends LockingStrategyException {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
* Represents an error trying to apply a pessimistic {@link LockingStrategy} to an entity
*
* @author Steve Ebersole
*
* @see jakarta.persistence.PessimisticLockException
*/
public class PessimisticEntityLockException extends LockingStrategyException {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import java.sql.SQLException;

import org.hibernate.PessimisticLockException;
import org.hibernate.JDBCException;

/**
Expand All @@ -14,7 +15,7 @@
*
* @author Steve Ebersole
*/
public class LockAcquisitionException extends JDBCException {
public class LockAcquisitionException extends PessimisticLockException {
public LockAcquisitionException(String string, SQLException root) {
super( string, root );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*
* @see org.hibernate.LockOptions#getTimeOut
* @see org.hibernate.LockOptions#setTimeOut
* @see jakarta.persistence.LockTimeoutException
*/
public class LockTimeoutException extends LockAcquisitionException {
public LockTimeoutException(String string, SQLException root) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.exception;

import org.hibernate.Incubating;
import org.hibernate.JDBCException;

import java.sql.SQLException;

/**
* A {@link org.hibernate.JDBCException} indicating that a request failed due to snapshot isolation.
* This is a condition which indicates an <em>optimistic</em> failure.
*
* @apiNote At present, this is only used to represent {@code DB_RECORD_CHANGED} on MariaDB.
*
* @see jakarta.persistence.OptimisticLockException
*/
@Incubating
public class SnapshotIsolationException extends JDBCException {
public SnapshotIsolationException(String string, SQLException root, String sql) {
super( string, root, sql );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import java.sql.SQLException;

import org.hibernate.JDBCException;
import org.hibernate.PessimisticLockException;
import org.hibernate.QueryTimeoutException;
import org.hibernate.exception.AuthException;
import org.hibernate.exception.ConstraintViolationException;
import org.hibernate.exception.ConstraintViolationException.ConstraintKind;
Expand All @@ -22,7 +20,6 @@
import org.checkerframework.checker.nullness.qual.Nullable;

import static org.hibernate.internal.util.JdbcExceptionHelper.determineSqlStateClassCode;
import static org.hibernate.internal.util.JdbcExceptionHelper.extractErrorCode;
import static org.hibernate.internal.util.JdbcExceptionHelper.extractSqlState;

/**
Expand Down Expand Up @@ -53,17 +50,6 @@ public SQLStateConversionDelegate(ConversionContext conversionContext) {
return new AuthException( message, sqlException, sql );
case "40001":
return new LockAcquisitionException( message, sqlException, sql );
case "40XL1", "40XL2":
// Derby "A lock could not be obtained within the time requested."
return new PessimisticLockException( message, sqlException, sql );
case "70100":
// MySQL Query execution was interrupted
return new QueryTimeoutException( message, sqlException, sql );
case "72000":
if ( extractErrorCode( sqlException ) == 1013 ) {
// Oracle user requested cancel of current operation
return new QueryTimeoutException( message, sqlException, sql );
}
}
switch ( determineSqlStateClassCode( sqlState ) ) {
case
Expand Down
Loading