1 package org.apache.ojb.broker.util; 2 3 17 18 import java.sql.SQLException ; 19 import java.sql.BatchUpdateException ; 20 import java.sql.SQLWarning ; 21 22 import org.apache.commons.lang.SystemUtils; 23 import org.apache.commons.lang.exception.ExceptionUtils; 24 import org.apache.ojb.broker.KeyConstraintViolatedException; 25 import org.apache.ojb.broker.PersistenceBrokerSQLException; 26 import org.apache.ojb.broker.core.ValueContainer; 27 import org.apache.ojb.broker.metadata.ClassDescriptor; 28 import org.apache.ojb.broker.metadata.FieldDescriptor; 29 import org.apache.ojb.broker.metadata.JdbcTypesHelper; 30 import org.apache.ojb.broker.util.logging.Logger; 31 32 37 abstract public class ExceptionHelper 38 { 39 51 public static PersistenceBrokerSQLException generateException(String message, SQLException ex, String sql, Logger logger) 52 { 53 return generateException(message, ex, sql, null, null, logger, null); 54 } 55 56 69 public static PersistenceBrokerSQLException generateException(SQLException ex, String sql, ClassDescriptor cld, Logger logger, Object obj) 70 { 71 return generateException(ex, sql, cld, null, logger, obj); 72 } 73 74 88 public static PersistenceBrokerSQLException generateException(SQLException ex, String sql, ClassDescriptor cld, ValueContainer[] values, Logger logger, Object obj) 89 { 90 return generateException(null, ex, sql, cld, values, logger, obj); 91 } 92 93 108 public static PersistenceBrokerSQLException generateException(String message, SQLException ex, String sql, ClassDescriptor cld, ValueContainer[] values, Logger logger, Object obj) 109 { 110 119 String eol = SystemUtils.LINE_SEPARATOR; 120 StringBuffer msg = new StringBuffer (eol); 121 eol += "* "; 122 123 if(ex instanceof BatchUpdateException ) 124 { 125 BatchUpdateException tmp = (BatchUpdateException ) ex; 126 if(message != null) 127 { 128 msg.append("* ").append(message); 129 } 130 else 131 { 132 msg.append("* BatchUpdateException during execution of sql-statement:"); 133 } 134 msg.append(eol).append("Batch update count is '").append(tmp.getUpdateCounts()).append("'"); 135 } 136 else if(ex instanceof SQLWarning ) 137 { 138 if(message != null) 139 { 140 msg.append("* ").append(message); 141 } 142 else 143 { 144 msg.append("* SQLWarning during execution of sql-statement:"); 145 } 146 } 147 else 148 { 149 if(message != null) 150 { 151 msg.append("* ").append(message); 152 } 153 else 154 { 155 msg.append("* SQLException during execution of sql-statement:"); 156 } 157 } 158 159 if(sql != null) 160 { 161 msg.append(eol).append("sql statement was '").append(sql).append("'"); 162 } 163 String stateCode = null; 164 if(ex != null) 165 { 166 msg.append(eol).append("Exception message is [").append(ex.getMessage()).append("]"); 167 msg.append(eol).append("Vendor error code [").append(ex.getErrorCode()).append("]"); 168 msg.append(eol).append("SQL state code ["); 169 170 stateCode = ex.getSQLState(); 171 if("23000".equalsIgnoreCase(stateCode)) msg.append(stateCode).append("=INTEGRITY CONSTRAINT VIOLATION"); 172 else if("23001".equalsIgnoreCase(stateCode)) msg.append(stateCode).append("=RESTRICT VIOLATION"); 173 else if("23502".equalsIgnoreCase(stateCode)) msg.append(stateCode).append("=NOT NULL VIOLATION"); 174 else if("23503".equalsIgnoreCase(stateCode)) msg.append(stateCode).append("=FOREIGN KEY VIOLATION"); 175 else if("23505".equalsIgnoreCase(stateCode)) msg.append(stateCode).append("=UNIQUE VIOLATION"); 176 else if("23514".equalsIgnoreCase(stateCode)) msg.append(stateCode).append("=CHECK VIOLATION"); 177 else msg.append(stateCode); 178 msg.append("]"); 179 } 180 181 if(cld != null) 182 { 183 msg.append(eol).append("Target class is '") 184 .append(cld.getClassNameOfObject()) 185 .append("'"); 186 FieldDescriptor[] fields = cld.getPkFields(); 187 msg.append(eol).append("PK of the target object is ["); 188 for(int i = 0; i < fields.length; i++) 189 { 190 try 191 { 192 if(i > 0) msg.append(", "); 193 msg.append(fields[i].getPersistentField().getName()); 194 if(obj != null) 195 { 196 msg.append("="); 197 msg.append(fields[i].getPersistentField().get(obj)); 198 } 199 } 200 catch(Exception ignore) 201 { 202 msg.append(" PK field build FAILED! "); 203 } 204 } 205 msg.append("]"); 206 } 207 if(values != null) 208 { 209 msg.append(eol).append(values.length).append(" values performed in statement: ").append(eol); 210 for(int i = 0; i < values.length; i++) 211 { 212 ValueContainer value = values[i]; 213 msg.append("["); 214 msg.append("jdbcType=").append(JdbcTypesHelper.getSqlTypeAsString(value.getJdbcType().getType())); 215 msg.append(", value=").append(value.getValue()); 216 msg.append("]"); 217 } 218 } 219 if(obj != null) 220 { 221 msg.append(eol).append("Source object: "); 222 try 223 { 224 msg.append(obj.toString()); 225 } 226 catch(Exception e) 227 { 228 msg.append(obj.getClass()); 229 } 230 } 231 232 String shortMsg = msg.toString(); 234 235 if(ex != null) 236 { 237 Throwable rootCause = ExceptionUtils.getRootCause(ex); 239 if(rootCause == null) rootCause = ex; 240 msg.append(eol).append("The root stack trace is --> "); 241 String rootStack = ExceptionUtils.getStackTrace(rootCause); 242 msg.append(eol).append(rootStack); 243 } 244 msg.append(SystemUtils.LINE_SEPARATOR).append("**"); 245 246 if(logger != null) logger.error(msg.toString()); 248 249 if("23000".equals(stateCode) || "23505".equals(stateCode)) 251 { 252 throw new KeyConstraintViolatedException(shortMsg, ex); 253 } 254 else 255 { 256 throw new PersistenceBrokerSQLException(shortMsg, ex); 257 } 258 } 259 } 260 | Popular Tags |