1 21 22 package org.apache.derby.impl.jdbc; 23 24 import org.apache.derby.jdbc.InternalDriver; 25 26 import org.apache.derby.iapi.services.context.Context; 27 import org.apache.derby.iapi.services.context.ContextService; 28 import org.apache.derby.iapi.services.context.ContextManager; 29 import org.apache.derby.iapi.services.monitor.Monitor; 30 import org.apache.derby.iapi.services.sanity.SanityManager; 31 32 import org.apache.derby.iapi.db.Database; 33 import org.apache.derby.iapi.error.StandardException; 34 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext; 35 import org.apache.derby.iapi.error.ExceptionSeverity; 36 37 import org.apache.derby.iapi.reference.Attribute; 38 import org.apache.derby.iapi.reference.SQLState; 39 import org.apache.derby.iapi.reference.Property; 40 import org.apache.derby.iapi.util.StringUtil; 41 import org.apache.derby.iapi.util.IdUtil; 42 43 import java.util.Properties ; 44 import java.sql.SQLException ; 45 46 111 public final class TransactionResourceImpl 112 { 113 116 protected ContextManager cm; 118 protected ContextService csf; 119 protected String username; 120 121 private String dbname; 122 private InternalDriver driver; 123 private String url; 124 private String drdaID; 125 126 protected Database database; 128 protected LanguageConnectionContext lcc; 129 130 133 TransactionResourceImpl( 134 InternalDriver driver, 135 String url, 136 Properties info) throws SQLException 137 { 138 this.driver = driver; 139 csf = driver.getContextServiceFactory(); 140 dbname = InternalDriver.getDatabaseName(url, info); 141 this.url = url; 142 143 username = IdUtil.getUserNameFromURLProps(info); 148 149 drdaID = info.getProperty(Attribute.DRDAID_ATTR, null); 150 151 153 cm = csf.newContextManager(); 160 } 161 162 167 void setDatabase(Database db) 168 { 169 if (SanityManager.DEBUG) 170 SanityManager.ASSERT(database == null, 171 "setting database when it is not null"); 172 173 database = db; 174 } 175 176 184 void startTransaction() throws StandardException, SQLException 185 { 186 lcc = database.setupConnection(cm, username, drdaID, dbname); 188 } 189 190 195 InternalDriver getDriver() { 196 return driver; 197 } 198 ContextService getCsf() { 199 return csf; 200 } 201 202 205 ContextManager getContextManager() { 206 return cm; 207 } 208 209 LanguageConnectionContext getLcc() { 210 return lcc; 211 } 212 String getDBName() { 213 return dbname; 214 } 215 String getUrl() { 216 return url; 217 } 218 Database getDatabase() { 219 return database; 220 } 221 222 StandardException shutdownDatabaseException() { 223 StandardException se = StandardException.newException(SQLState.SHUTDOWN_DATABASE, getDBName()); 224 se.setReport(StandardException.REPORT_NEVER); 225 return se; 226 } 227 228 234 void commit() throws StandardException 235 { 236 lcc.userCommit(); 237 } 238 239 void rollback() throws StandardException 240 { 241 if (lcc != null) 243 lcc.userRollback(); 244 } 245 246 249 250 253 void clearContextInError() 254 { 255 csf.resetCurrentContextManager(cm); 256 cm = null; 257 } 258 259 262 void clearLcc() 263 { 264 lcc = null; 265 } 266 267 final void setupContextStack() 268 { 269 if (SanityManager.DEBUG) { 270 SanityManager.ASSERT(cm != null, "setting up null context manager stack"); 271 } 272 273 csf.setCurrentContextManager(cm); 274 } 275 276 final void restoreContextStack() { 277 278 if ((csf == null) || (cm == null)) 279 return; 280 csf.resetCurrentContextManager(cm); 281 } 282 283 286 287 290 final SQLException handleException(Throwable thrownException, 291 boolean autoCommit, 292 boolean rollbackOnAutoCommit) 293 throws SQLException 294 { 295 try { 296 if (SanityManager.DEBUG) 297 SanityManager.ASSERT(thrownException != null); 298 299 304 if (thrownException instanceof SQLException ) { 305 306 return (SQLException ) thrownException; 307 308 } 309 310 boolean checkForShutdown = false; 311 if (thrownException instanceof StandardException) 312 { 313 StandardException se = (StandardException) thrownException; 314 int severity = se.getSeverity(); 315 if (severity <= ExceptionSeverity.STATEMENT_SEVERITY) 316 { 317 323 if (autoCommit && rollbackOnAutoCommit) 324 { 325 se.setSeverity(ExceptionSeverity.TRANSACTION_SEVERITY); 326 } 327 } else if (SQLState.CONN_INTERRUPT.equals(se.getMessageId())) { 328 checkForShutdown = true; 330 } 331 } 332 if (cm!=null) { 336 boolean isShutdown = cleanupOnError(thrownException); 337 if (checkForShutdown && isShutdown) { 338 thrownException = shutdownDatabaseException(); 340 } 341 } 342 343 344 345 return wrapInSQLException((SQLException ) null, thrownException); 346 347 } catch (Throwable t) { 348 349 if (cm!=null) { cm.cleanupOnError(t); 351 } 352 358 throw wrapInSQLException((SQLException ) null, t); 359 } 361 362 } 363 364 public static final SQLException wrapInSQLException(SQLException sqlException, Throwable thrownException) { 365 366 if (thrownException == null) 367 return sqlException; 368 369 SQLException nextSQLException; 370 371 if (thrownException instanceof SQLException ) { 372 373 nextSQLException = (SQLException ) thrownException; 375 } 376 else if (thrownException instanceof StandardException) { 377 378 StandardException se = (StandardException) thrownException; 379 380 nextSQLException = Util.generateCsSQLException(se); 381 382 wrapInSQLException(nextSQLException, se.getNestedException()); 383 384 } else { 385 386 nextSQLException = Util.javaException(thrownException); 387 388 Throwable nestedByJVM = null; 390 if (thrownException instanceof ExceptionInInitializerError ) { 391 nestedByJVM = ((ExceptionInInitializerError ) thrownException).getException(); 392 } else if (thrownException instanceof java.lang.reflect.InvocationTargetException ) { 393 nestedByJVM = ((java.lang.reflect.InvocationTargetException ) thrownException).getTargetException(); 394 } 395 396 if (nestedByJVM != null) { 397 wrapInSQLException(nextSQLException, nestedByJVM); 398 } 399 400 } 401 402 if (sqlException != null) { 403 sqlException.setNextException(nextSQLException); 404 } 405 406 return nextSQLException; 407 } 408 409 412 413 String getUserName() { 414 return username; 415 } 416 417 boolean cleanupOnError(Throwable e) 418 { 419 if (SanityManager.DEBUG) 420 SanityManager.ASSERT(cm != null, "cannot cleanup on error with null context manager"); 421 422 return cm.cleanupOnError(e); 423 } 424 425 boolean isIdle() 426 { 427 return (lcc == null || lcc.getTransactionExecute().isIdle()); 429 } 430 431 432 435 436 437 440 boolean isActive() 441 { 442 return (driver.isActive() && ((database == null) || database.isActive())); 444 } 445 446 } 447 448 449 | Popular Tags |