1<?xml version="1.0" encoding="UTF-8" standalone="no"?> 2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3<html xmlns="http://www.w3.org/1999/xhtml"> 4 <head> 5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 6 <title> 7 Handling Exceptions 8 </title> 9 <link rel="stylesheet" href="gettingStarted.css" type="text/css" /> 10 <meta name="generator" content="DocBook XSL Stylesheets V1.62.4" /> 11 <link rel="home" href="index.html" title="Berkeley DB Collections Tutorial" /> 12 <link rel="up" href="BasicProgram.html" title="Chapter��2.�� 		The Basic Program 	" /> 13 <link rel="previous" href="retrievingdatabaseitems.html" title=" 		Retrieving Database Items 	" /> 14 <link rel="next" href="UsingSecondaries.html" title="Chapter��3.�� 		Using Secondary Indices 	" /> 15 </head> 16 <body> 17 <div class="navheader"> 18 <table width="100%" summary="Navigation header"> 19 <tr> 20 <th colspan="3" align="center"> 21 Handling Exceptions 22 </th> 23 </tr> 24 <tr> 25 <td width="20%" align="left"><a accesskey="p" href="retrievingdatabaseitems.html">Prev</a>��</td> 26 <th width="60%" align="center">Chapter��2.�� 27 The Basic Program 28 </th> 29 <td width="20%" align="right">��<a accesskey="n" href="UsingSecondaries.html">Next</a></td> 30 </tr> 31 </table> 32 <hr /> 33 </div> 34 <div class="sect1" lang="en" xml:lang="en"> 35 <div class="titlepage"> 36 <div> 37 <div> 38 <h2 class="title" style="clear: both"><a id="handlingexceptions"></a> 39 Handling Exceptions 40 </h2> 41 </div> 42 </div> 43 <div></div> 44 </div> 45 <p> 46 Exception handling was illustrated previously in 47 <a href="implementingmain.html"> 48 Implementing the Main Program 49 </a> 50 and 51 <a href="usingtransactions.html"> 52 Using Transactions 53 </a> 54 exception handling in a DB Java Collections API application in 55 more detail. 56</p> 57 <p> 58 There are two exceptions that must be treated specially: 59 60 <a href="../../java/com/sleepycat/db/RunRecoveryException.html" target="_top">RunRecoveryException</a> 61 62 and 63 64 <span> 65 <a href="../../java/com/sleepycat/db/DeadlockException.html" target="_top">DeadlockException</a>. 66 </span> 67</p> 68 <p> 69 70 <a href="../../java/com/sleepycat/db/RunRecoveryException.html" target="_top">RunRecoveryException</a> 71 72 is thrown when the only solution is to shut down the application 73 and run recovery. All applications must catch this exception and 74 follow the recovery procedure. 75</p> 76 <p> 77 When 78 79 <a href="../../java/com/sleepycat/db/DeadlockException.html" target="_top">DeadlockException</a> 80 81 is thrown, the application should normally retry the operation. If 82 a deadlock continues to occur for some maximum number of retries, 83 the application should give up and try again later or take other 84 corrective actions. The DB Java Collections API provides two APIs 85 for transaction execution. 86</p> 87 <div class="itemizedlist"> 88 <ul type="disc"> 89 <li> 90 <p> 91 When using the 92 <a href="../../java/com/sleepycat/collections/CurrentTransaction.html" target="_top">CurrentTransaction</a> 93 94 class directly, the application must catch 95 96 <a href="../../java/com/sleepycat/db/DeadlockException.html" target="_top">DeadlockException</a> 97 98 and follow the procedure described previously. 99 </p> 100 </li> 101 <li> 102 <p> 103 When using the 104 <a href="../../java/com/sleepycat/collections/TransactionRunner.html" target="_top">TransactionRunner</a> 105 106 class, retries are performed automatically and the application need 107 only handle the case where the maximum number of retries has been 108 reached. In that case, 109 <a href="../../java/com/sleepycat/collections/TransactionRunner.html#run(com.sleepycat.collections.TransactionWorker)" target="_top">TransactionRunner.run</a> 110 111 will throw 112 113 <span> 114 <a href="../../java/com/sleepycat/db/DeadlockException.html" target="_top">DeadlockException</a>. 115 </span> 116 </p> 117 </li> 118 </ul> 119 </div> 120 <p> 121 When using the 122 <a href="../../java/com/sleepycat/collections/TransactionRunner.html" target="_top">TransactionRunner</a> 123 124 class there are two other considerations. 125</p> 126 <div class="itemizedlist"> 127 <ul type="disc"> 128 <li> 129 <p> 130 First, if the application-defined 131 <a href="../../java/com/sleepycat/collections/TransactionWorker.html#doWork()" target="_top">TransactionWorker.doWork</a> 132 133 method throws an exception the 134 transaction will automatically be aborted, and otherwise the 135 transaction will automatically be committed. Applications should 136 design their transaction processing with this in mind. 137 </p> 138 </li> 139 <li> 140 <p> 141 Second, please be aware that 142 <a href="../../java/com/sleepycat/collections/TransactionRunner.html#run(com.sleepycat.collections.TransactionWorker)" target="_top">TransactionRunner.run</a> 143 144 unwraps exceptions in order to discover whether a nested exception is a 145 146 <span> 147 <a href="../../java/com/sleepycat/db/DeadlockException.html" target="_top">DeadlockException</a>. 148 </span> 149 This is particularly important since all Berkeley DB exceptions 150 that occur while calling a stored collection method are wrapped 151 with a 152 <a href="../../java/com/sleepycat/util/RuntimeExceptionWrapper.html" target="_top">RuntimeExceptionWrapper</a>. 153 This wrapping is necessary because Berkeley DB exceptions are 154 checked exceptions, and the Java collections API does not allow 155 such exceptions to be thrown. 156 </p> 157 </li> 158 </ul> 159 </div> 160 <p> 161 When calling 162 <a href="../../java/com/sleepycat/collections/TransactionRunner.html#run(com.sleepycat.collections.TransactionWorker)" target="_top">TransactionRunner.run</a>, 163 the unwrapped (nested) exception will be unwrapped and thrown 164 automatically. If you are not using 165 <a href="../../java/com/sleepycat/collections/TransactionRunner.html" target="_top">TransactionRunner</a> 166 167 or if you are handling exceptions directly for some other reason, 168 use the 169 <a href="../../java/com/sleepycat/util/ExceptionUnwrapper.html#unwrap(java.lang.Exception)" target="_top">ExceptionUnwrapper.unwrap</a> 170 171 method to get the nested exception. For example, this can be used 172 to discover that an exception is a 173 174 <a href="../../java/com/sleepycat/db/RunRecoveryException.html" target="_top">RunRecoveryException</a> 175 176 as shown below. 177</p> 178 <a id="cb_java_manageexceptions"></a> 179 <pre class="programlisting"><b class="userinput"><tt>import com.sleepycat.db.RunRecoveryException; 180import com.sleepycat.util.ExceptionUnwrapper; 181... 182 catch (Exception e) 183 { 184 e = ExceptionUnwrapper.unwrap(e); 185 if (e instanceof RunRecoveryException) 186 { 187 // follow recovery procedure 188 } 189 }</tt></b> </pre> 190 </div> 191 <div class="navfooter"> 192 <hr /> 193 <table width="100%" summary="Navigation footer"> 194 <tr> 195 <td width="40%" align="left"><a accesskey="p" href="retrievingdatabaseitems.html">Prev</a>��</td> 196 <td width="20%" align="center"> 197 <a accesskey="u" href="BasicProgram.html">Up</a> 198 </td> 199 <td width="40%" align="right">��<a accesskey="n" href="UsingSecondaries.html">Next</a></td> 200 </tr> 201 <tr> 202 <td width="40%" align="left" valign="top"> 203 Retrieving Database Items 204 ��</td> 205 <td width="20%" align="center"> 206 <a accesskey="h" href="index.html">Home</a> 207 </td> 208 <td width="40%" align="right" valign="top">��Chapter��3.�� 209 Using Secondary Indices 210 </td> 211 </tr> 212 </table> 213 </div> 214 </body> 215</html> 216