From 87827c768c2d54cc5e90ba0ecb2342463bce787b Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Fri, 4 Jun 2010 15:40:17 +0000 Subject: Added deadlock detection for JDBC. --- .../repo/jdbc/JDBCExceptionTransformer.java | 37 ++++++++++++++++++++++ .../repo/jdbc/OracleExceptionTransformer.java | 14 ++++++++ 2 files changed, 51 insertions(+) (limited to 'src/main/java/com') diff --git a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCExceptionTransformer.java b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCExceptionTransformer.java index 9863a69..f72c717 100644 --- a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCExceptionTransformer.java +++ b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCExceptionTransformer.java @@ -21,6 +21,9 @@ package com.amazon.carbonado.repo.jdbc; import java.sql.SQLException; import com.amazon.carbonado.ConstraintException; +import com.amazon.carbonado.FetchDeadlockException; +import com.amazon.carbonado.FetchException; +import com.amazon.carbonado.PersistDeadlockException; import com.amazon.carbonado.PersistDeniedException; import com.amazon.carbonado.PersistException; import com.amazon.carbonado.UniqueConstraintException; @@ -50,6 +53,12 @@ class JDBCExceptionTransformer extends ExceptionTransformer { */ public static String SQLSTATE_UNIQUE_CONSTRAINT_VIOLATION = "23505"; + /** + * Five digit SQLSTATE code for "Deadlock or timeout with automatic + * rollback occurred" + */ + public static String SQLSTATE_DEADLOCK_WITH_ROLLBACK = "40001"; + /** * Examines the SQLSTATE code of the given SQL exception and determines if * it is a generic constaint violation. @@ -84,9 +93,34 @@ class JDBCExceptionTransformer extends ExceptionTransformer { return false; } + public boolean isDeadlockError(SQLException e) { + if (e != null) { + String sqlstate = e.getSQLState(); + if (sqlstate != null) { + return sqlstate.startsWith(SQLSTATE_DEADLOCK_WITH_ROLLBACK); + } + } + return false; + } + JDBCExceptionTransformer() { } + @Override + protected FetchException transformIntoFetchException(Throwable e) { + FetchException fe = super.transformIntoFetchException(e); + if (fe != null) { + return fe; + } + if (e instanceof SQLException) { + SQLException se = (SQLException) e; + if (isDeadlockError(se)) { + return new FetchDeadlockException(e); + } + } + return null; + } + @Override protected PersistException transformIntoPersistException(Throwable e) { PersistException pe = super.transformIntoPersistException(e); @@ -104,6 +138,9 @@ class JDBCExceptionTransformer extends ExceptionTransformer { if (isInsufficientPrivilegesError(se)) { return new PersistDeniedException(e); } + if (isDeadlockError(se)) { + return new PersistDeadlockException(e); + } } return null; } diff --git a/src/main/java/com/amazon/carbonado/repo/jdbc/OracleExceptionTransformer.java b/src/main/java/com/amazon/carbonado/repo/jdbc/OracleExceptionTransformer.java index ccd1011..ad63941 100644 --- a/src/main/java/com/amazon/carbonado/repo/jdbc/OracleExceptionTransformer.java +++ b/src/main/java/com/amazon/carbonado/repo/jdbc/OracleExceptionTransformer.java @@ -30,6 +30,8 @@ class OracleExceptionTransformer extends JDBCExceptionTransformer { public static int INSUFFICIENT_PRIVILEGES = 1031; + public static int DEADLOCK_DETECTED = 60; + @Override public boolean isUniqueConstraintError(SQLException e) { if (isConstraintError(e)) { @@ -49,4 +51,16 @@ class OracleExceptionTransformer extends JDBCExceptionTransformer { } return false; } + + @Override + public boolean isDeadlockError(SQLException e) { + if (super.isDeadlockError(e)) { + return true; + } + if (e != null) { + int errorCode = e.getErrorCode(); + return DEADLOCK_DETECTED == errorCode; + } + return false; + } } -- cgit v1.2.3