|
10 | 10 | * |
11 | 11 | * PS: Do you know how difficult it is to type on a train? ;-) |
12 | 12 | * |
13 | | - * $Id: ConnectionTest.java,v 1.3 2001/09/07 22:17:48 momjian Exp $ |
| 13 | + * $Id: ConnectionTest.java,v 1.4 2001/09/10 14:54:22 momjian Exp $ |
14 | 14 | */ |
15 | 15 |
|
16 | 16 | publicclassConnectionTestextendsTestCase { |
@@ -203,36 +203,94 @@ public void testWarnings() { |
203 | 203 | } |
204 | 204 | } |
205 | 205 |
|
206 | | -/** |
207 | | - * Transaction Isolation Levels |
208 | | - */ |
209 | | -publicvoidtestTransactionIsolation() { |
210 | | -try { |
211 | | -Connectioncon =JDBC2Tests.openDB(); |
212 | | - |
213 | | -con.setAutoCommit(false); |
214 | | - |
215 | | -// These are the currently available ones |
216 | | -con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE); |
217 | | -assert(con.getTransactionIsolation()==Connection.TRANSACTION_SERIALIZABLE); |
218 | | - |
219 | | -con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); |
220 | | -assert(con.getTransactionIsolation()==Connection.TRANSACTION_READ_COMMITTED); |
221 | | - |
222 | | -// Now turn on AutoCommit. Transaction Isolation doesn't work outside of |
223 | | -// a transaction, so they should return READ_COMMITTED at all times! |
224 | | -con.setAutoCommit(true); |
225 | | -con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE); |
226 | | -assert(con.getTransactionIsolation()==Connection.TRANSACTION_READ_COMMITTED); |
227 | | - |
228 | | -con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); |
229 | | -assert(con.getTransactionIsolation()==Connection.TRANSACTION_READ_COMMITTED); |
230 | | - |
231 | | -JDBC2Tests.closeDB(con); |
232 | | - }catch(SQLExceptionex) { |
233 | | -assert(ex.getMessage(),false); |
234 | | - } |
235 | | - } |
| 206 | +/** |
| 207 | + * Transaction Isolation Levels |
| 208 | + */ |
| 209 | +publicvoidtestTransactionIsolation() |
| 210 | +{ |
| 211 | +try |
| 212 | +{ |
| 213 | +Connectioncon =JDBC2Tests.openDB(); |
| 214 | + |
| 215 | +// PostgreSQL defaults to READ COMMITTED |
| 216 | +assertEquals(con.getTransactionIsolation(), |
| 217 | +Connection.TRANSACTION_READ_COMMITTED ); |
| 218 | + |
| 219 | +// Begin a transaction |
| 220 | +con.setAutoCommit(false); |
| 221 | + |
| 222 | +// The isolation level should not have changed |
| 223 | +assertEquals(con.getTransactionIsolation(), |
| 224 | +Connection.TRANSACTION_READ_COMMITTED ); |
| 225 | + |
| 226 | +// Now change the default for future transactions |
| 227 | +con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE ); |
| 228 | + |
| 229 | +// Since the call to setTransactionIsolation() above was made |
| 230 | +// inside the transaction, the isolation level of the current |
| 231 | +// transaction did not change. It affects only future transactions. |
| 232 | +// This behaviour is recommended by the JDBC spec. |
| 233 | +assertEquals(con.getTransactionIsolation(), |
| 234 | +Connection.TRANSACTION_READ_COMMITTED ); |
| 235 | + |
| 236 | +// Begin a new transaction |
| 237 | +con.commit(); |
| 238 | + |
| 239 | +// Now we should see the new isolation level |
| 240 | +assertEquals(con.getTransactionIsolation(), |
| 241 | +Connection.TRANSACTION_SERIALIZABLE ); |
| 242 | + |
| 243 | +// Repeat the steps above with the transition back to |
| 244 | +// READ COMMITTED. |
| 245 | +con.setTransactionIsolation( |
| 246 | +Connection.TRANSACTION_READ_COMMITTED ); |
| 247 | +assertEquals(con.getTransactionIsolation(), |
| 248 | +Connection.TRANSACTION_SERIALIZABLE ); |
| 249 | +con.commit(); |
| 250 | +assertEquals(con.getTransactionIsolation(), |
| 251 | +Connection.TRANSACTION_READ_COMMITTED ); |
| 252 | + |
| 253 | +// Now run some tests with autocommit enabled. |
| 254 | +con.setAutoCommit(true); |
| 255 | + |
| 256 | +assertEquals(con.getTransactionIsolation(), |
| 257 | +Connection.TRANSACTION_READ_COMMITTED ); |
| 258 | + |
| 259 | +con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE ); |
| 260 | +assertEquals(con.getTransactionIsolation(), |
| 261 | +Connection.TRANSACTION_SERIALIZABLE ); |
| 262 | + |
| 263 | +con.setTransactionIsolation( |
| 264 | +Connection.TRANSACTION_READ_COMMITTED ); |
| 265 | +assertEquals(con.getTransactionIsolation(), |
| 266 | +Connection.TRANSACTION_READ_COMMITTED ); |
| 267 | + |
| 268 | +// Test if a change of isolation level before beginning the |
| 269 | +// transaction affects the isolation level inside the transaction. |
| 270 | +con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE ); |
| 271 | +assertEquals(con.getTransactionIsolation(), |
| 272 | +Connection.TRANSACTION_SERIALIZABLE ); |
| 273 | +con.setAutoCommit(false); |
| 274 | +assertEquals(con.getTransactionIsolation(), |
| 275 | +Connection.TRANSACTION_SERIALIZABLE ); |
| 276 | +con.setAutoCommit(true); |
| 277 | +assertEquals(con.getTransactionIsolation(), |
| 278 | +Connection.TRANSACTION_SERIALIZABLE ); |
| 279 | +con.setTransactionIsolation( |
| 280 | +Connection.TRANSACTION_READ_COMMITTED ); |
| 281 | +assertEquals(con.getTransactionIsolation(), |
| 282 | +Connection.TRANSACTION_READ_COMMITTED ); |
| 283 | +con.setAutoCommit(false); |
| 284 | +assertEquals(con.getTransactionIsolation(), |
| 285 | +Connection.TRANSACTION_READ_COMMITTED ); |
| 286 | + |
| 287 | +JDBC2Tests.closeDB(con); |
| 288 | +} |
| 289 | +catch (SQLExceptionex ) |
| 290 | +{ |
| 291 | +fail(ex.getMessage() ); |
| 292 | +} |
| 293 | +} |
236 | 294 |
|
237 | 295 | /** |
238 | 296 | * JDBC2 Type mappings |
|