1 16 17 package org.springframework.jdbc.support; 18 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 import org.springframework.dao.ConcurrencyFailureException; 22 import org.springframework.dao.DataAccessException; 23 import org.springframework.dao.DataAccessResourceFailureException; 24 import org.springframework.dao.DataIntegrityViolationException; 25 import org.springframework.jdbc.BadSqlGrammarException; 26 import org.springframework.jdbc.UncategorizedSQLException; 27 import org.springframework.util.Assert; 28 29 import java.sql.SQLException ; 30 import java.util.HashSet ; 31 import java.util.Set ; 32 33 46 public class SQLStateSQLExceptionTranslator implements SQLExceptionTranslator { 47 48 51 private static final Set BAD_SQL_CODES = new HashSet (6); 52 53 56 private static final Set INTEGRITY_VIOLATION_CODES = new HashSet (4); 57 58 61 private static final Set RESOURCE_FAILURE_CODES = new HashSet (3); 62 63 66 private static final Set CONCURRENCY_CODES = new HashSet (1); 67 68 69 static { 71 BAD_SQL_CODES.add("07"); 72 BAD_SQL_CODES.add("37"); 73 BAD_SQL_CODES.add("42"); 74 BAD_SQL_CODES.add("2A"); 75 BAD_SQL_CODES.add("65"); BAD_SQL_CODES.add("S0"); 78 INTEGRITY_VIOLATION_CODES.add("22"); INTEGRITY_VIOLATION_CODES.add("23"); INTEGRITY_VIOLATION_CODES.add("27"); INTEGRITY_VIOLATION_CODES.add("44"); 83 CONCURRENCY_CODES.add("40"); 85 RESOURCE_FAILURE_CODES.add("08"); RESOURCE_FAILURE_CODES.add("53"); RESOURCE_FAILURE_CODES.add("54"); } 89 90 91 92 protected final Log logger = LogFactory.getLog(getClass()); 93 94 95 public DataAccessException translate(String task, String sql, SQLException ex) { 96 Assert.notNull(ex, "Cannot translate a null SQLException."); 97 if (task == null) { 98 task = ""; 99 } 100 if (sql == null) { 101 sql = ""; 102 } 103 String sqlState = getSqlState(ex); 104 if (sqlState != null && sqlState.length() >= 2) { 105 String classCode = sqlState.substring(0, 2); 106 if (BAD_SQL_CODES.contains(classCode)) { 107 return new BadSqlGrammarException(task, sql, ex); 108 } 109 else if (INTEGRITY_VIOLATION_CODES.contains(classCode)) { 110 return new DataIntegrityViolationException(buildMessage(task, sql, ex), ex); 111 } 112 else if (RESOURCE_FAILURE_CODES.contains(classCode)) { 113 return new DataAccessResourceFailureException(buildMessage(task, sql, ex), ex); 114 } 115 else if (CONCURRENCY_CODES.contains(classCode)) { 116 return new ConcurrencyFailureException(buildMessage(task, sql, ex), ex); 117 } 118 } 119 return new UncategorizedSQLException(task, sql, ex); 121 } 122 123 124 133 protected String buildMessage(String task, String sql, SQLException ex) { 134 return task + "; SQL [" + sql + "]; " + ex.getMessage(); 135 } 136 137 138 145 private String getSqlState(SQLException ex) { 146 String sqlState = ex.getSQLState(); 147 if (sqlState == null) { 148 SQLException nestedEx = ex.getNextException(); 149 if (nestedEx != null) { 150 sqlState = nestedEx.getSQLState(); 151 } 152 } 153 return sqlState; 154 } 155 156 } 157 | Popular Tags |