1 21 package oracle.toplink.essentials.exceptions; 23 24 import java.io.StringWriter ; 25 import java.sql.SQLException ; 26 import oracle.toplink.essentials.queryframework.*; 27 import oracle.toplink.essentials.internal.databaseaccess.*; 28 import oracle.toplink.essentials.exceptions.i18n.ExceptionMessageGenerator; 29 import oracle.toplink.essentials.sessions.Record; 30 import oracle.toplink.essentials.internal.sessions.AbstractRecord; 31 import oracle.toplink.essentials.internal.sessions.AbstractSession; 32 33 37 public class DatabaseException extends TopLinkException { 38 protected SQLException exception; 39 protected transient Call call; 40 protected transient DatabaseQuery query; 41 protected transient AbstractRecord queryArguments; 42 protected transient Accessor accessor; 43 public static final int SQL_EXCEPTION = 4002; 44 public static final int CONFIGURATION_ERROR_CLASS_NOT_FOUND = 4003; 45 public static final int DATABASE_ACCESSOR_NOT_CONNECTED = 4005; 46 public static final int ERROR_READING_BLOB_DATA = 4006; 47 public static final int COULD_NOT_CONVERT_OBJECT_TYPE = 4007; 48 public static final int LOGOUT_WHILE_TRANSACTION_IN_PROGRESS = 4008; 49 public static final int SEQUENCE_TABLE_INFORMATION_NOT_COMPLETE = 4009; 50 public static final int ERROR_PREALLOCATING_SEQUENCE_NUMBERS = 4011; 51 public static final int CANNOT_REGISTER_SYNCHRONIZATIONLISTENER_FOR_UNITOFWORK = 4014; 52 public static final int SYNCHRONIZED_UNITOFWORK_DOES_NOT_SUPPORT_COMMITANDRESUME = 4015; 53 public static final int CONFIGURATION_ERROR_NEW_INSTANCE_INSTANTIATION_EXCEPTION = 4016; 54 public static final int CONFIGURATION_ERROR_NEW_INSTANCE_ILLEGAL_ACCESS_EXCEPTION = 4017; 55 public static final int TRANSACTION_MANAGER_NOT_SET_FOR_JTS_DRIVER = 4018; 56 public static final int ERROR_RETRIEVE_DB_METADATA_THROUGH_JDBC_CONNECTION = 4019; 57 58 62 protected DatabaseException(String message) { 63 super(message); 64 } 65 66 70 protected DatabaseException(SQLException exception) { 71 super(exception.toString(), exception); 72 } 73 74 public static DatabaseException cannotRegisterSynchronizatonListenerForUnitOfWork(Exception e) { 75 Object [] args = { e }; 76 77 DatabaseException databaseException = new DatabaseException(ExceptionMessageGenerator.buildMessage(DatabaseException.class, CANNOT_REGISTER_SYNCHRONIZATIONLISTENER_FOR_UNITOFWORK, args)); 78 databaseException.setErrorCode(CANNOT_REGISTER_SYNCHRONIZATIONLISTENER_FOR_UNITOFWORK); 79 databaseException.setInternalException(e); 80 return databaseException; 81 } 82 83 public static DatabaseException configurationErrorClassNotFound(String className) { 84 Object [] args = { className }; 85 86 DatabaseException databaseException = new DatabaseException(ExceptionMessageGenerator.buildMessage(DatabaseException.class, CONFIGURATION_ERROR_CLASS_NOT_FOUND, args)); 87 databaseException.setErrorCode(CONFIGURATION_ERROR_CLASS_NOT_FOUND); 88 return databaseException; 89 } 90 91 public static DatabaseException configurationErrorNewInstanceIllegalAccessException(IllegalAccessException exception, Class javaClass) { 92 Object [] args = { javaClass }; 93 94 DatabaseException databaseException = new DatabaseException(ExceptionMessageGenerator.buildMessage(DatabaseException.class, CONFIGURATION_ERROR_NEW_INSTANCE_ILLEGAL_ACCESS_EXCEPTION, args)); 95 databaseException.setErrorCode(CONFIGURATION_ERROR_NEW_INSTANCE_ILLEGAL_ACCESS_EXCEPTION); 96 databaseException.setInternalException(exception); 97 return databaseException; 98 } 99 100 public static DatabaseException configurationErrorNewInstanceInstantiationException(InstantiationException exception, Class javaClass) { 101 Object [] args = { javaClass }; 102 103 DatabaseException databaseException = new DatabaseException(ExceptionMessageGenerator.buildMessage(DatabaseException.class, CONFIGURATION_ERROR_NEW_INSTANCE_INSTANTIATION_EXCEPTION, args)); 104 databaseException.setErrorCode(CONFIGURATION_ERROR_NEW_INSTANCE_INSTANTIATION_EXCEPTION); 105 databaseException.setInternalException(exception); 106 return databaseException; 107 } 108 109 public static DatabaseException couldNotConvertObjectType(int type) { 110 Object [] args = { CR, new Integer (type) }; 111 112 DatabaseException databaseException = new DatabaseException(ExceptionMessageGenerator.buildMessage(DatabaseException.class, COULD_NOT_CONVERT_OBJECT_TYPE, args)); 113 databaseException.setErrorCode(COULD_NOT_CONVERT_OBJECT_TYPE); 114 return databaseException; 115 } 116 117 public static DatabaseException databaseAccessorNotConnected() { 118 Object [] args = { }; 119 String message = oracle.toplink.essentials.exceptions.i18n.ExceptionMessageGenerator.buildMessage(DatabaseException.class, DATABASE_ACCESSOR_NOT_CONNECTED, args); 120 DatabaseException databaseException = new DatabaseException(message); 121 databaseException.setErrorCode(DATABASE_ACCESSOR_NOT_CONNECTED); 122 return databaseException; 123 } 124 125 public static DatabaseException databaseAccessorNotConnected(DatabaseAccessor databaseAccessor) { 126 Object [] args = { }; 127 128 DatabaseException databaseException = new DatabaseException(ExceptionMessageGenerator.buildMessage(DatabaseException.class, DATABASE_ACCESSOR_NOT_CONNECTED, args)); 129 databaseException.setErrorCode(DATABASE_ACCESSOR_NOT_CONNECTED); 130 databaseException.setAccessor(databaseAccessor); 131 return databaseException; 132 } 133 134 public static DatabaseException errorPreallocatingSequenceNumbers() { 135 Object [] args = { }; 136 137 DatabaseException databaseException = new DatabaseException(ExceptionMessageGenerator.buildMessage(DatabaseException.class, ERROR_PREALLOCATING_SEQUENCE_NUMBERS, args)); 138 databaseException.setErrorCode(ERROR_PREALLOCATING_SEQUENCE_NUMBERS); 139 return databaseException; 140 } 141 142 public static DatabaseException errorReadingBlobData() { 143 Object [] args = { }; 144 145 DatabaseException databaseException = new DatabaseException(ExceptionMessageGenerator.buildMessage(DatabaseException.class, ERROR_READING_BLOB_DATA, args)); 146 databaseException.setErrorCode(ERROR_READING_BLOB_DATA); 147 return databaseException; 148 } 149 150 154 public Accessor getAccessor() { 155 return accessor; 156 } 157 158 163 public int getDatabaseErrorCode() { 164 if (getInternalException() == null) { 165 return super.getErrorCode(); 166 } 167 return ((SQLException )getInternalException()).getErrorCode(); 168 } 169 170 174 public String getMessage() { 175 if (getInternalException() == null) { 176 return super.getMessage(); 177 } else { 178 StringWriter writer = new StringWriter (); 179 writer.write(super.getMessage()); 180 writer.write(getIndentationString()); 181 writer.write(ExceptionMessageGenerator.getHeader("ErrorCodeHeader")); 182 if (getInternalException() instanceof SQLException ) { 183 writer.write(Integer.toString(((SQLException )getInternalException()).getErrorCode())); 184 } else { 185 writer.write("000"); 186 } 187 if (getCall() != null) { 188 writer.write(cr()); 189 writer.write(getIndentationString()); 190 writer.write("Call:"); 191 if (getAccessor() != null) { 192 writer.write(getCall().getLogString(getAccessor())); 193 } else { 194 writer.write(getCall().toString()); 195 } 196 } 197 if (getQuery() != null) { 198 writer.write(cr()); 199 writer.write(getIndentationString()); 200 writer.write("Query:"); 201 try { 202 writer.write(getQuery().toString()); 203 } catch (RuntimeException badTooString) { 204 } 205 } 206 return writer.toString(); 207 } 208 } 209 210 217 public DatabaseQuery getQuery() { 218 return query; 219 } 220 221 225 public Call getCall() { 226 return call; 227 } 228 229 233 public void setCall(Call call) { 234 this.call = call; 235 } 236 237 241 public Record getQueryArgumentsRecord() { 242 return queryArguments; 243 } 244 245 public static DatabaseException logoutWhileTransactionInProgress() { 246 Object [] args = { }; 247 248 DatabaseException databaseException = new DatabaseException(ExceptionMessageGenerator.buildMessage(DatabaseException.class, LOGOUT_WHILE_TRANSACTION_IN_PROGRESS, args)); 249 databaseException.setErrorCode(LOGOUT_WHILE_TRANSACTION_IN_PROGRESS); 250 return databaseException; 251 } 252 253 public static DatabaseException sequenceTableInformationNotComplete() { 254 Object [] args = { }; 255 256 DatabaseException databaseException = new DatabaseException(ExceptionMessageGenerator.buildMessage(DatabaseException.class, SEQUENCE_TABLE_INFORMATION_NOT_COMPLETE, args)); 257 databaseException.setErrorCode(SEQUENCE_TABLE_INFORMATION_NOT_COMPLETE); 258 return databaseException; 259 } 260 261 265 public void setAccessor(Accessor accessor) { 266 this.accessor = accessor; 267 } 268 269 276 public void setQuery(DatabaseQuery query) { 277 this.query = query; 278 } 279 280 284 public void setQueryArguments(AbstractRecord queryArguments) { 285 this.queryArguments = queryArguments; 286 } 287 288 public static DatabaseException sqlException(SQLException exception) { 289 DatabaseException databaseException = new DatabaseException(exception); 290 databaseException.setErrorCode(SQL_EXCEPTION); 291 return databaseException; 292 } 293 294 public static DatabaseException sqlException(SQLException exception, Accessor accessor, AbstractSession session) { 295 DatabaseException databaseException = new DatabaseException(exception); 296 databaseException.setErrorCode(SQL_EXCEPTION); 297 databaseException.setAccessor(accessor); 298 databaseException.setSession(session); 299 return databaseException; 300 } 301 302 public static DatabaseException sqlException(SQLException exception, Call call, Accessor accessor, AbstractSession session) { 303 DatabaseException databaseException = new DatabaseException(exception); 304 databaseException.setErrorCode(SQL_EXCEPTION); 305 databaseException.setAccessor(accessor); 306 databaseException.setCall(call); 307 return databaseException; 308 } 309 310 public static DatabaseException synchronizedUnitOfWorkDoesNotSupportCommitAndResume() { 311 Object [] args = { }; 312 313 String message = oracle.toplink.essentials.exceptions.i18n.ExceptionMessageGenerator.buildMessage(DatabaseException.class, SYNCHRONIZED_UNITOFWORK_DOES_NOT_SUPPORT_COMMITANDRESUME, args); 314 DatabaseException databaseException = new DatabaseException(message); 315 databaseException.setErrorCode(SYNCHRONIZED_UNITOFWORK_DOES_NOT_SUPPORT_COMMITANDRESUME); 316 return databaseException; 317 } 318 319 public static DatabaseException transactionManagerNotSetForJTSDriver() { 320 Object [] args = { }; 321 322 DatabaseException databaseException = new DatabaseException(ExceptionMessageGenerator.buildMessage(DatabaseException.class, TRANSACTION_MANAGER_NOT_SET_FOR_JTS_DRIVER, args)); 323 databaseException.setErrorCode(TRANSACTION_MANAGER_NOT_SET_FOR_JTS_DRIVER); 324 return databaseException; 325 } 326 327 public static DatabaseException errorRetrieveDbMetadataThroughJDBCConnection() { 328 Object [] args = { }; 329 330 DatabaseException databaseException = new DatabaseException(ExceptionMessageGenerator.buildMessage(DatabaseException.class, ERROR_RETRIEVE_DB_METADATA_THROUGH_JDBC_CONNECTION, args)); 331 databaseException.setErrorCode(ERROR_RETRIEVE_DB_METADATA_THROUGH_JDBC_CONNECTION); 332 return databaseException; 333 } 334 } 335 | Popular Tags |