Skip to content

Commit

Permalink
Export the testing mechanism #676
Browse files Browse the repository at this point in the history
  • Loading branch information
vladmihalcea committed Nov 11, 2023
1 parent cc1c801 commit ac939f0
Show file tree
Hide file tree
Showing 157 changed files with 2,994 additions and 2,389 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.hypersistence.utils.hibernate.util;

import io.hypersistence.utils.hibernate.util.logging.InlineQueryLogEntryCreator;
import io.hypersistence.utils.logging.InlineQueryLogEntryCreator;
import net.ttddyy.dsproxy.listener.ChainListener;
import net.ttddyy.dsproxy.listener.DataSourceQueryCountListener;
import net.ttddyy.dsproxy.listener.logging.SLF4JQueryLoggingListener;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.hypersistence.utils.hibernate.util.logging;
package io.hypersistence.utils.logging;

import net.ttddyy.dsproxy.ExecutionInfo;
import net.ttddyy.dsproxy.QueryInfo;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.hypersistence.utils.hibernate.util;

import io.hypersistence.utils.hibernate.util.logging.InlineQueryLogEntryCreator;
import io.hypersistence.utils.logging.InlineQueryLogEntryCreator;
import net.ttddyy.dsproxy.listener.ChainListener;
import net.ttddyy.dsproxy.listener.DataSourceQueryCountListener;
import net.ttddyy.dsproxy.listener.logging.SLF4JQueryLoggingListener;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.hypersistence.utils.hibernate.util.logging;
package io.hypersistence.utils.logging;

import net.ttddyy.dsproxy.ExecutionInfo;
import net.ttddyy.dsproxy.QueryInfo;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.hypersistence.utils.spring.config;

import io.hypersistence.utils.hibernate.util.logging.InlineQueryLogEntryCreator;
import io.hypersistence.utils.logging.InlineQueryLogEntryCreator;
import io.hypersistence.utils.hibernate.util.providers.DataSourceProvider;
import io.hypersistence.utils.hibernate.util.providers.PostgreSQLDataSourceProvider;
import com.zaxxer.hikari.HikariConfig;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.hypersistence.utils.hibernate.util;

import io.hypersistence.utils.hibernate.util.logging.InlineQueryLogEntryCreator;
import io.hypersistence.utils.logging.InlineQueryLogEntryCreator;
import net.ttddyy.dsproxy.listener.ChainListener;
import net.ttddyy.dsproxy.listener.DataSourceQueryCountListener;
import net.ttddyy.dsproxy.listener.logging.SLF4JQueryLoggingListener;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.hypersistence.utils.hibernate.util.logging;
package io.hypersistence.utils.logging;

import net.ttddyy.dsproxy.ExecutionInfo;
import net.ttddyy.dsproxy.QueryInfo;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.hypersistence.utils.spring.config;

import io.hypersistence.utils.hibernate.util.logging.InlineQueryLogEntryCreator;
import io.hypersistence.utils.logging.InlineQueryLogEntryCreator;
import io.hypersistence.utils.hibernate.util.providers.DataSourceProvider;
import io.hypersistence.utils.hibernate.util.providers.PostgreSQLDataSourceProvider;
import com.zaxxer.hikari.HikariConfig;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.hypersistence.utils.hibernate.util;
package io.hypersistence.utils.common;

import java.io.InputStream;
import java.net.URL;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package io.hypersistence.utils.common;

import jakarta.persistence.LockTimeoutException;
import org.hibernate.PessimisticLockException;
import org.hibernate.exception.LockAcquisitionException;

import java.sql.SQLTimeoutException;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

/**
* @author Vlad Mihalcea
*/
public interface ExceptionUtil {

List<Class<? extends Exception>> LOCK_TIMEOUT_EXCEPTIONS = Arrays.asList(
LockAcquisitionException.class,
LockTimeoutException.class,
PessimisticLockException.class,
jakarta.persistence.PessimisticLockException.class,
SQLTimeoutException.class
);

/**
* Get the root cause of a particular {@code Throwable}
*
* @param t exception
* @return exception root cause
*/
static <T extends Throwable> T rootCause(Throwable t) {
Throwable cause = t.getCause();
if (cause != null && cause != t) {
return rootCause(cause);
}
return (T) t;
}

/**
* Is the given throwable caused by a database lock timeout?
*
* @param e exception
* @return is caused by a database lock timeout
*/
static boolean isLockTimeout(Throwable e) {
AtomicReference<Throwable> causeHolder = new AtomicReference<>(e);
do {
final Throwable cause = causeHolder.get();
final String failureMessage = cause.getMessage().toLowerCase();
if (LOCK_TIMEOUT_EXCEPTIONS.stream().anyMatch(c -> c.isInstance(cause)) ||
failureMessage.contains("timeout") ||
failureMessage.contains("timed out") ||
failureMessage.contains("time out") ||
failureMessage.contains("closed connection") ||
failureMessage.contains("link failure") ||
failureMessage.contains("expired or aborted by a conflict")
) {
return true;
} else {
if (cause.getCause() == null || cause.getCause() == cause) {
break;
} else {
causeHolder.set(cause.getCause());
}
}
}
while (true);
return false;
}

/**
* Is the given throwable caused by the following exception type?
*
* @param e exception
* @param exceptionType exception type
* @return is caused by the given exception type
*/
static boolean isCausedBy(Throwable e, Class<? extends Throwable> exceptionType) {
AtomicReference<Throwable> causeHolder = new AtomicReference<>(e);
do {
final Throwable cause = causeHolder.get();
if (exceptionType.isInstance(cause)) {
return true;
} else {
if (cause.getCause() == null || cause.getCause() == cause) {
break;
} else {
causeHolder.set(cause.getCause());
}
}
}
while (true);
return false;
}

/**
* Is the given throwable caused by a database MVCC anomaly detection?
*
* @param e exception
* @return is caused by a database lock MVCC anomaly detection
*/
static boolean isMVCCAnomalyDetection(Throwable e) {
AtomicReference<Throwable> causeHolder = new AtomicReference<>(e);
do {
final Throwable cause = causeHolder.get();
String lowerCaseMessage = cause.getMessage().toLowerCase();
if (
cause.getMessage().contains("ORA-08177: can't serialize access for this transaction") //Oracle
|| lowerCaseMessage.contains("could not serialize access due to concurrent update") //PSQLException
|| lowerCaseMessage.contains("ould not serialize access due to read/write dependencies among transactions") //PSQLException
|| lowerCaseMessage.contains("snapshot isolation transaction aborted due to update conflict") //SQLServerException
|| lowerCaseMessage.contains("kconflict") //YugabyteDB
|| lowerCaseMessage.contains("unknown transaction, could be recently aborted") //YugabyteDB
|| lowerCaseMessage.contains("conflicts with higher priority transaction") //YugabyteDB
) {
return true;
} else {
if (cause.getCause() == null || cause.getCause() == cause) {
break;
} else {
causeHolder.set(cause.getCause());
}
}
}
while (true);
return false;
}

/**
* Was the given exception caused by a SQL connection close
*
* @param e exception
* @return is caused by a SQL connection close
*/
static boolean isConnectionClose(Exception e) {
Throwable cause = e;
do {
if (cause.getMessage().toLowerCase().contains("connection is close")
|| cause.getMessage().toLowerCase().contains("closed connection")
|| cause.getMessage().toLowerCase().contains("link failure")
|| cause.getMessage().toLowerCase().contains("closed")
) {
return true;
} else {
if (cause.getCause() == null || cause.getCause() == cause) {
break;
} else {
cause = cause.getCause();
}
}
}
while (true);
return false;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.hypersistence.utils.hibernate.util;
package io.hypersistence.utils.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down
Loading

0 comments on commit ac939f0

Please sign in to comment.