-
Notifications
You must be signed in to change notification settings - Fork 236
/
TestManagedDataSourceInTx.java
405 lines (333 loc) · 14.5 KB
/
TestManagedDataSourceInTx.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/**
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.dbcp2.managed;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.apache.commons.dbcp2.DelegatingConnection;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import javax.transaction.Synchronization;
import javax.transaction.Transaction;
/**
* TestSuite for ManagedDataSource with an active transaction in progress.
*/
public class TestManagedDataSourceInTx extends TestManagedDataSource {
@Override
@Before
public void setUp() throws Exception {
super.setUp();
transactionManager.begin();
}
@Override
@After
public void tearDown() throws Exception {
if (transactionManager.getTransaction() != null) {
transactionManager.commit();
}
super.tearDown();
}
@Test
public void testGetConnectionInAfterCompletion() throws Exception {
final DelegatingConnection<?> connection = (DelegatingConnection<?>) newConnection();
// Don't close so we can check it for warnings in afterCompletion
transactionManager.getTransaction().registerSynchronization(new Synchronization() {
@Override
public void beforeCompletion() {
}
@Override
public void afterCompletion(int i) {
try {
Connection connection1 = ds.getConnection();
try {
connection1.getWarnings();
fail("Could operate on closed connection");
} catch (SQLException e) {
// This is expected
}
} catch (SQLException e) {
fail("Should have been able to get connection");
}
}
});
connection.close();
transactionManager.commit();
}
/**
* @see #testSharedConnection()
*/
@Override
@Test
public void testManagedConnectionEqualsFail() throws Exception {
// this test is invalid for managed connections since because
// two connections to the same datasource are supposed to share
// a single connection
}
@Override
@Test
public void testNestedConnections() {
// Not supported
}
@Override
@Test
public void testConnectionsAreDistinct() throws Exception {
final Connection[] conn = new Connection[getMaxTotal()];
for(int i=0;i<conn.length;i++) {
conn[i] = newConnection();
for(int j=0;j<i;j++) {
// two connections should be distinct instances
Assert.assertNotSame(conn[j], conn[i]);
// neither should they should be equivalent even though they are
// sharing the same underlying connection
Assert.assertNotEquals(conn[j], conn[i]);
// Check underlying connection is the same
Assert.assertEquals(((DelegatingConnection<?>) conn[j]).getInnermostDelegateInternal(),
((DelegatingConnection<?>) conn[i]).getInnermostDelegateInternal());
}
}
for (final Connection element : conn) {
element.close();
}
}
@Override
@Test
public void testHashCode() throws Exception {
final Connection conn1 = newConnection();
assertNotNull(conn1);
final Connection conn2 = newConnection();
assertNotNull(conn2);
// shared connections should not have the same hashcode
Assert.assertNotEquals(conn1.hashCode(), conn2.hashCode());
}
@Override
@Test
public void testMaxTotal() throws Exception {
final Transaction[] transactions = new Transaction[getMaxTotal()];
final Connection[] c = new Connection[getMaxTotal()];
for (int i = 0; i < c.length; i++) {
// create a new connection in the current transaction
c[i] = newConnection();
assertNotNull(c[i]);
// suspend the current transaction and start a new one
transactions[i] = transactionManager.suspend();
assertNotNull(transactions[i]);
transactionManager.begin();
}
try {
newConnection();
fail("Allowed to open more than DefaultMaxTotal connections.");
} catch (final java.sql.SQLException e) {
// should only be able to open 10 connections, so this test should
// throw an exception
} finally {
transactionManager.commit();
for (int i = 0; i < c.length; i++) {
transactionManager.resume(transactions[i]);
c[i].close();
transactionManager.commit();
}
}
}
@Override
@Test
public void testClearWarnings() throws Exception {
// open a connection
Connection connection = newConnection();
assertNotNull(connection);
// generate SQLWarning on connection
final CallableStatement statement = connection.prepareCall("warning");
assertNotNull(connection.getWarnings());
// create a new shared connection
final Connection sharedConnection = newConnection();
// shared connection should see warning
assertNotNull(sharedConnection.getWarnings());
// close and allocate a new (original) connection
connection.close();
connection = newConnection();
// warnings should not have been cleared by closing the connection
assertNotNull(connection.getWarnings());
assertNotNull(sharedConnection.getWarnings());
statement.close();
connection.close();
sharedConnection.close();
}
@Override
@Test
public void testSharedConnection() throws Exception {
final DelegatingConnection<?> connectionA = (DelegatingConnection<?>) newConnection();
final DelegatingConnection<?> connectionB = (DelegatingConnection<?>) newConnection();
assertFalse(connectionA.equals(connectionB));
assertFalse(connectionB.equals(connectionA));
assertTrue(connectionA.innermostDelegateEquals(connectionB.getInnermostDelegate()));
assertTrue(connectionB.innermostDelegateEquals(connectionA.getInnermostDelegate()));
connectionA.close();
connectionB.close();
}
@Test
public void testSharedTransactionConversion() throws Exception {
final DelegatingConnection<?> connectionA = (DelegatingConnection<?>) newConnection();
final DelegatingConnection<?> connectionB = (DelegatingConnection<?>) newConnection();
// in a transaction the inner connections should be equal
assertFalse(connectionA.equals(connectionB));
assertFalse(connectionB.equals(connectionA));
assertTrue(connectionA.innermostDelegateEquals(connectionB.getInnermostDelegate()));
assertTrue(connectionB.innermostDelegateEquals(connectionA.getInnermostDelegate()));
transactionManager.commit();
// use the connection so it adjusts to the completed transaction
connectionA.getAutoCommit();
connectionB.getAutoCommit();
// no there is no transaction so inner connections should not be equal
assertFalse(connectionA.equals(connectionB));
assertFalse(connectionB.equals(connectionA));
assertFalse(connectionA.innermostDelegateEquals(connectionB.getInnermostDelegate()));
assertFalse(connectionB.innermostDelegateEquals(connectionA.getInnermostDelegate()));
transactionManager.begin();
// use the connection so it adjusts to the new transaction
connectionA.getAutoCommit();
connectionB.getAutoCommit();
// back in a transaction so inner connections should be equal again
assertFalse(connectionA.equals(connectionB));
assertFalse(connectionB.equals(connectionA));
assertTrue(connectionA.innermostDelegateEquals(connectionB.getInnermostDelegate()));
assertTrue(connectionB.innermostDelegateEquals(connectionA.getInnermostDelegate()));
connectionA.close();
connectionB.close();
}
@Test
public void testCloseInTransaction() throws Exception {
final DelegatingConnection<?> connectionA = (DelegatingConnection<?>) newConnection();
final DelegatingConnection<?> connectionB = (DelegatingConnection<?>) newConnection();
assertFalse(connectionA.equals(connectionB));
assertFalse(connectionB.equals(connectionA));
assertTrue(connectionA.innermostDelegateEquals(connectionB.getInnermostDelegate()));
assertTrue(connectionB.innermostDelegateEquals(connectionA.getInnermostDelegate()));
connectionA.close();
connectionB.close();
final Connection connection = newConnection();
assertFalse("Connection should be open", connection.isClosed());
connection.close();
assertTrue("Connection should be closed", connection.isClosed());
}
@Override
@Test
public void testAutoCommitBehavior() throws Exception {
final Connection connection = newConnection();
// auto commit should be off
assertFalse("Auto-commit should be disabled", connection.getAutoCommit());
// attempt to set auto commit
try {
connection.setAutoCommit(true);
fail("setAutoCommit method should be disabled while enlisted in a transaction");
} catch (final SQLException e) {
// expected
}
// make sure it is still disabled
assertFalse("Auto-commit should be disabled", connection.getAutoCommit());
// close connection
connection.close();
}
@Test
public void testCommit() throws Exception {
final Connection connection = newConnection();
// connection should be open
assertFalse("Connection should be open", connection.isClosed());
// attempt commit directly
try {
connection.commit();
fail("commit method should be disabled while enlisted in a transaction");
} catch (final SQLException e) {
// expected
}
// make sure it is still open
assertFalse("Connection should be open", connection.isClosed());
// close connection
connection.close();
}
@Test
public void testReadOnly() throws Exception {
final Connection connection = newConnection();
// NOTE: This test class uses connections that are read-only by default
// connection should be read only
assertTrue("Connection be read-only", connection.isReadOnly());
// attempt to setReadOnly
try {
connection.setReadOnly(true);
fail("setReadOnly method should be disabled while enlisted in a transaction");
} catch (final SQLException e) {
// expected
}
// make sure it is still read-only
assertTrue("Connection be read-only", connection.isReadOnly());
// attempt to setReadonly
try {
connection.setReadOnly(false);
fail("setReadOnly method should be disabled while enlisted in a transaction");
} catch (final SQLException e) {
// expected
}
// make sure it is still read-only
assertTrue("Connection be read-only", connection.isReadOnly());
// close connection
connection.close();
}
// can't actually test close in a transaction
@Override
protected void assertBackPointers(final Connection conn, final Statement statement) throws SQLException {
assertFalse(conn.isClosed());
assertFalse(isClosed(statement));
assertSame("statement.getConnection() should return the exact same connection instance that was used to create the statement",
conn, statement.getConnection());
final ResultSet resultSet = statement.getResultSet();
assertFalse(isClosed(resultSet));
assertSame("resultSet.getStatement() should return the exact same statement instance that was used to create the result set",
statement, resultSet.getStatement());
final ResultSet executeResultSet = statement.executeQuery("select * from dual");
assertFalse(isClosed(executeResultSet));
assertSame("resultSet.getStatement() should return the exact same statement instance that was used to create the result set",
statement, executeResultSet.getStatement());
final ResultSet keysResultSet = statement.getGeneratedKeys();
assertFalse(isClosed(keysResultSet));
assertSame("resultSet.getStatement() should return the exact same statement instance that was used to create the result set",
statement, keysResultSet.getStatement());
ResultSet preparedResultSet = null;
if (statement instanceof PreparedStatement) {
final PreparedStatement preparedStatement = (PreparedStatement) statement;
preparedResultSet = preparedStatement.executeQuery();
assertFalse(isClosed(preparedResultSet));
assertSame("resultSet.getStatement() should return the exact same statement instance that was used to create the result set",
statement, preparedResultSet.getStatement());
}
resultSet.getStatement().getConnection().close();
}
@Override
@Test
public void testConnectionReturnOnCommit() throws Exception {
// override with no-op test
return;
}
}