/
MariaDbPoolDataSourceTest.java
550 lines (474 loc) · 19.6 KB
/
MariaDbPoolDataSourceTest.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
/*
*
* MariaDB Client for Java
*
* Copyright (c) 2012-2014 Monty Program Ab.
* Copyright (c) 2015-2020 MariaDB Corporation Ab.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License along
* with this library; if not, write to Monty Program Ab info@montyprogram.com.
*
*/
package org.mariadb.jdbc;
import static org.junit.Assert.*;
import java.lang.management.ManagementFactory;
import java.sql.*;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import javax.management.MBeanInfo;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import org.junit.AfterClass;
import org.junit.Assume;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mariadb.jdbc.internal.util.pool.Pools;
import org.mariadb.jdbc.internal.util.scheduler.MariaDbThreadFactory;
public class MariaDbPoolDataSourceTest extends BaseTest {
@BeforeClass()
public static void initClass() throws SQLException {
afterClass();
try (Statement stmt = sharedConnection.createStatement()) {
stmt.execute(
"CREATE TABLE testResetRollback(id int not null primary key auto_increment, test varchar(20))");
stmt.execute("FLUSH TABLES");
}
}
@AfterClass
public static void afterClass() throws SQLException {
try (Statement stmt = sharedConnection.createStatement()) {
stmt.execute("DROP TABLE IF EXISTS testResetRollback");
}
}
@Test
public void testResetDatabase() throws SQLException {
try (MariaDbPoolDataSource pool = new MariaDbPoolDataSource(connUri + "&maxPoolSize=1")) {
try (Connection connection = pool.getConnection()) {
Statement statement = connection.createStatement();
statement.execute("CREATE DATABASE IF NOT EXISTS testingReset");
connection.setCatalog("testingReset");
}
try (Connection connection = pool.getConnection()) {
assertEquals(database, connection.getCatalog());
Statement statement = connection.createStatement();
statement.execute("DROP DATABASE testingReset");
}
}
}
@Test
public void testResetSessionVariable() throws SQLException {
testResetSessionVariable(false);
if (isMariadbServer() && minVersion(10, 2)) {
testResetSessionVariable(true);
}
}
private void testResetSessionVariable(boolean useResetConnection) throws SQLException {
try (MariaDbPoolDataSource pool =
new MariaDbPoolDataSource(
connUri + "&maxPoolSize=1&useResetConnection=" + useResetConnection)) {
long nowMillis;
int initialWaitTimeout;
try (Connection connection = pool.getConnection()) {
Statement statement = connection.createStatement();
nowMillis = getNowTime(statement);
initialWaitTimeout = getWaitTimeout(statement);
statement.execute(
"SET @@timestamp=UNIX_TIMESTAMP('1970-10-01 01:00:00'), @@wait_timeout=2000");
long newNowMillis = getNowTime(statement);
int waitTimeout = getWaitTimeout(statement);
assertTrue(nowMillis - newNowMillis > 23_587_200_000L);
assertEquals(2_000, waitTimeout);
}
try (Connection connection = pool.getConnection()) {
Statement statement = connection.createStatement();
long newNowMillis = getNowTime(statement);
int waitTimeout = getWaitTimeout(statement);
if (useResetConnection) {
assertTrue(nowMillis - newNowMillis < 10L);
assertEquals(initialWaitTimeout, waitTimeout);
} else {
assertTrue(nowMillis - newNowMillis > 23_587_200_000L);
assertEquals(2_000, waitTimeout);
}
}
}
}
private long getNowTime(Statement statement) throws SQLException {
ResultSet rs = statement.executeQuery("SELECT NOW()");
assertTrue(rs.next());
return rs.getTimestamp(1).getTime();
}
private int getWaitTimeout(Statement statement) throws SQLException {
ResultSet rs = statement.executeQuery("SELECT @@wait_timeout");
assertTrue(rs.next());
return rs.getInt(1);
}
@Test
public void testResetUserVariable() throws SQLException {
testResetUserVariable(false);
if (isMariadbServer() && minVersion(10, 2)) {
testResetUserVariable(true);
}
}
private void testResetUserVariable(boolean useResetConnection) throws SQLException {
try (MariaDbPoolDataSource pool =
new MariaDbPoolDataSource(
connUri + "&maxPoolSize=1&useResetConnection=" + useResetConnection)) {
long nowMillis;
try (Connection connection = pool.getConnection()) {
Statement statement = connection.createStatement();
assertNull(getUserVariableStr(statement));
statement.execute("SET @str = '123'");
assertEquals("123", getUserVariableStr(statement));
}
try (Connection connection = pool.getConnection()) {
Statement statement = connection.createStatement();
if (useResetConnection) {
assertNull(getUserVariableStr(statement));
} else {
assertEquals("123", getUserVariableStr(statement));
}
}
}
}
private String getUserVariableStr(Statement statement) throws SQLException {
ResultSet rs = statement.executeQuery("SELECT @str");
assertTrue(rs.next());
return rs.getString(1);
}
@Test
public void testNetworkTimeout() throws SQLException {
try (MariaDbPoolDataSource pool =
new MariaDbPoolDataSource(connUri + "&maxPoolSize=1&socketTimeout=10000")) {
try (Connection connection = pool.getConnection()) {
assertEquals(10_000, connection.getNetworkTimeout());
connection.setNetworkTimeout(null, 5_000);
}
try (Connection connection = pool.getConnection()) {
assertEquals(10_000, connection.getNetworkTimeout());
}
}
}
@Test
public void testResetReadOnly() throws SQLException {
try (MariaDbPoolDataSource pool = new MariaDbPoolDataSource(connUri + "&maxPoolSize=1")) {
try (Connection connection = pool.getConnection()) {
assertFalse(connection.isReadOnly());
connection.setReadOnly(true);
assertTrue(connection.isReadOnly());
}
try (Connection connection = pool.getConnection()) {
assertFalse(connection.isReadOnly());
}
}
}
@Test
public void testResetAutoCommit() throws SQLException {
try (MariaDbPoolDataSource pool = new MariaDbPoolDataSource(connUri + "&maxPoolSize=1")) {
try (Connection connection = pool.getConnection()) {
assertTrue(connection.getAutoCommit());
connection.setAutoCommit(false);
assertFalse(connection.getAutoCommit());
}
try (Connection connection = pool.getConnection()) {
assertTrue(connection.getAutoCommit());
}
}
}
@Test
public void testResetAutoCommitOption() throws SQLException {
try (MariaDbPoolDataSource pool =
new MariaDbPoolDataSource(connUri + "&maxPoolSize=1&autocommit=false&poolName=PoolTest")) {
try (Connection connection = pool.getConnection()) {
assertFalse(connection.getAutoCommit());
connection.setAutoCommit(true);
assertTrue(connection.getAutoCommit());
}
try (Connection connection = pool.getConnection()) {
assertFalse(connection.getAutoCommit());
}
}
}
@Test
public void testResetTransactionIsolation() throws SQLException {
Assume.assumeTrue(
!sharedIsAurora() && System.getenv("SKYSQL") == null && System.getenv("SKYSQL_HA") == null);
try (MariaDbPoolDataSource pool = new MariaDbPoolDataSource(connUri + "&maxPoolSize=1")) {
try (Connection connection = pool.getConnection()) {
assertEquals(Connection.TRANSACTION_REPEATABLE_READ, connection.getTransactionIsolation());
connection.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
assertEquals(Connection.TRANSACTION_SERIALIZABLE, connection.getTransactionIsolation());
}
try (Connection connection = pool.getConnection()) {
assertEquals(Connection.TRANSACTION_REPEATABLE_READ, connection.getTransactionIsolation());
}
}
}
@Test
public void testJmx() throws Exception {
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
ObjectName filter = new ObjectName("org.mariadb.jdbc.pool:type=PoolTestJmx-*");
try (MariaDbPoolDataSource pool =
new MariaDbPoolDataSource(connUri + "&maxPoolSize=5&minPoolSize=0&poolName=PoolTestJmx")) {
try (Connection connection = pool.getConnection()) {
Set<ObjectName> objectNames = server.queryNames(filter, null);
assertEquals(1, objectNames.size());
ObjectName name = objectNames.iterator().next();
MBeanInfo info = server.getMBeanInfo(name);
assertEquals(4, info.getAttributes().length);
checkJmxInfo(server, name, 1, 1, 0, 0);
try (Connection connection2 = pool.getConnection()) {
checkJmxInfo(server, name, 2, 2, 0, 0);
}
checkJmxInfo(server, name, 1, 2, 1, 0);
}
}
}
@Test
public void testNoMinConnection() throws Exception {
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
ObjectName filter = new ObjectName("org.mariadb.jdbc.pool:type=testNoMinConnection-*");
try (MariaDbPoolDataSource pool =
new MariaDbPoolDataSource(connUri + "&maxPoolSize=5&poolName=testNoMinConnection")) {
try (Connection connection = pool.getConnection()) {
Set<ObjectName> objectNames = server.queryNames(filter, null);
assertEquals(1, objectNames.size());
ObjectName name = objectNames.iterator().next();
MBeanInfo info = server.getMBeanInfo(name);
assertEquals(4, info.getAttributes().length);
// wait to ensure pool has time to create 5 connections
try {
Thread.sleep(sharedIsAurora() ? 10_000 : 500);
} catch (InterruptedException interruptEx) {
// eat
}
checkJmxInfo(server, name, 1, 5, 4, 0);
try (Connection connection2 = pool.getConnection()) {
checkJmxInfo(server, name, 2, 5, 3, 0);
}
checkJmxInfo(server, name, 1, 5, 4, 0);
}
}
}
@Test
public void testIdleTimeout() throws Throwable {
// not for maxscale, testing thread id is not relevant.
// appveyor is so slow wait time are not relevant.
Assume.assumeTrue(
System.getenv("SKYSQL") == null
&& System.getenv("SKYSQL_HA") == null
&& System.getenv("APPVEYOR_BUILD_WORKER_IMAGE") == null);
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
ObjectName filter = new ObjectName("org.mariadb.jdbc.pool:type=testIdleTimeout-*");
try (MariaDbPoolDataSource pool =
new MariaDbPoolDataSource(
connUri + "&maxPoolSize=5&minPoolSize=3&poolName=testIdleTimeout")) {
pool.testForceMaxIdleTime(sharedIsAurora() ? 10 : 3);
// wait to ensure pool has time to create 3 connections
Thread.sleep(sharedIsAurora() ? 5_000 : 1_000);
Set<ObjectName> objectNames = server.queryNames(filter, null);
ObjectName name = objectNames.iterator().next();
checkJmxInfo(server, name, 0, 3, 3, 0);
List<Long> initialThreadIds = pool.testGetConnectionIdleThreadIds();
Thread.sleep(sharedIsAurora() ? 12_000 : 3_500);
// must still have 3 connections, but must be other ones
checkJmxInfo(server, name, 0, 3, 3, 0);
List<Long> threadIds = pool.testGetConnectionIdleThreadIds();
assertEquals(initialThreadIds.size(), threadIds.size());
for (Long initialThread : initialThreadIds) {
assertFalse(threadIds.contains(initialThread));
}
}
}
@Test
public void testMinConnection() throws Throwable {
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
ObjectName filter = new ObjectName("org.mariadb.jdbc.pool:type=testMinConnection-*");
try (MariaDbPoolDataSource pool =
new MariaDbPoolDataSource(
connUri + "&maxPoolSize=5&minPoolSize=3&poolName=testMinConnection")) {
try (Connection connection = pool.getConnection()) {
Set<ObjectName> objectNames = server.queryNames(filter, null);
assertEquals(1, objectNames.size());
ObjectName name = objectNames.iterator().next();
MBeanInfo info = server.getMBeanInfo(name);
assertEquals(4, info.getAttributes().length);
// to ensure pool has time to create minimal connection number
Thread.sleep(sharedIsAurora() ? 5000 : 500);
checkJmxInfo(server, name, 1, 3, 2, 0);
try (Connection connection2 = pool.getConnection()) {
checkJmxInfo(server, name, 2, 3, 1, 0);
}
checkJmxInfo(server, name, 1, 3, 2, 0);
}
}
}
private void checkJmxInfo(
MBeanServer server,
ObjectName name,
long expectedActive,
long expectedTotal,
long expectedIdle,
long expectedRequest)
throws Exception {
assertEquals(
expectedActive, ((Long) server.getAttribute(name, "ActiveConnections")).longValue());
assertEquals(expectedTotal, ((Long) server.getAttribute(name, "TotalConnections")).longValue());
assertEquals(expectedIdle, ((Long) server.getAttribute(name, "IdleConnections")).longValue());
assertEquals(
expectedRequest, ((Long) server.getAttribute(name, "ConnectionRequests")).longValue());
}
@Test
public void testJmxDisable() throws Exception {
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
ObjectName filter = new ObjectName("org.mariadb.jdbc.pool:type=PoolTest-*");
try (MariaDbPoolDataSource pool =
new MariaDbPoolDataSource(
connUri + "&maxPoolSize=2®isterJmxPool=false&poolName=PoolTest")) {
try (Connection connection = pool.getConnection()) {
Set<ObjectName> objectNames = server.queryNames(filter, null);
assertEquals(0, objectNames.size());
}
}
}
@Test
public void testResetRollback() throws SQLException {
sharedConnection.createStatement().execute("FLUSH TABLES");
try (MariaDbPoolDataSource pool = new MariaDbPoolDataSource(connUri + "&maxPoolSize=1")) {
try (Connection connection = pool.getConnection()) {
Statement stmt = connection.createStatement();
stmt.executeUpdate("INSERT INTO testResetRollback (test) VALUES ('heja')");
connection.setAutoCommit(false);
stmt.executeUpdate("INSERT INTO testResetRollback (test) VALUES ('japp')");
}
try (Connection connection = pool.getConnection()) {
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT count(*) FROM testResetRollback");
assertTrue(rs.next());
assertEquals(1, rs.getInt(1));
}
}
}
@Test
public void ensureUsingPool() throws Exception {
Assume.assumeTrue(System.getenv("SKYSQL") == null && System.getenv("SKYSQL_HA") == null);
ThreadPoolExecutor connectionAppender =
new ThreadPoolExecutor(
50,
5000,
10,
TimeUnit.SECONDS,
new LinkedBlockingQueue<>(5000),
new MariaDbThreadFactory("testPool"));
final long start = System.currentTimeMillis();
Set<Integer> threadIds = new HashSet<>();
for (int i = 0; i < 500; i++) {
connectionAppender.execute(
() -> {
try (Connection connection =
DriverManager.getConnection(
connUri + "&pool&staticGlobal&poolName=PoolEnsureUsingPool&log=true")) {
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT CONNECTION_ID()");
rs.next();
Integer connectionId = rs.getInt(1);
threadIds.add(connectionId);
stmt.execute("SELECT * FROM mysql.user");
} catch (SQLException e) {
e.printStackTrace();
}
});
}
connectionAppender.shutdown();
connectionAppender.awaitTermination(sharedIsAurora() ? 200 : 30, TimeUnit.SECONDS);
int numberOfConnection = 0;
for (Integer integer : threadIds) {
System.out.println("Connection id : " + integer);
numberOfConnection++;
}
System.out.println("Size : " + threadIds.size() + " " + numberOfConnection);
assertTrue(
"connection ids must be less than 8 : " + numberOfConnection, numberOfConnection <= 8);
assertTrue(System.currentTimeMillis() - start < (sharedIsAurora() ? 120_000 : 5_000));
Pools.close("PoolTest");
}
@Test
public void ensureClosed() throws Throwable {
Assume.assumeTrue(
System.getenv("MAXSCALE_TEST_DISABLE") == null
&& System.getenv("SKYSQL_HA") == null
&& System.getenv("SKYSQL") == null);
Thread.sleep(500); // ensure that previous close are effective
int initialConnection = getCurrentConnections();
try (MariaDbPoolDataSource pool =
new MariaDbPoolDataSource(connUri + "&maxPoolSize=10&minPoolSize=1")) {
try (Connection connection = pool.getConnection()) {
connection.isValid(10_000);
}
assertTrue(getCurrentConnections() > initialConnection);
// reuse IdleConnection
try (Connection connection = pool.getConnection()) {
connection.isValid(10_000);
}
Thread.sleep(500);
assertTrue(getCurrentConnections() > initialConnection);
}
Thread.sleep(2000); // ensure that previous close are effective
assertEquals(initialConnection, getCurrentConnections());
}
@Test
public void wrongUrlHandling() throws SQLException {
int initialConnection = getCurrentConnections();
try (MariaDbPoolDataSource pool =
new MariaDbPoolDataSource(
"jdbc:mariadb://unknownHost/db?user=wrong&maxPoolSize=10&connectTimeout=500")) {
pool.initialize();
long start = System.currentTimeMillis();
try (Connection connection = pool.getConnection()) {
fail();
} catch (SQLException sqle) {
assertTrue(
"timeout does not correspond to option. Elapsed time:"
+ (System.currentTimeMillis() - start),
(System.currentTimeMillis() - start) >= 500
&& (System.currentTimeMillis() - start) < 700);
assertTrue(
sqle.getMessage()
.contains(
"No connection available within the specified time (option 'connectTimeout': 500 ms)"));
}
}
}
@Test
public void testPrepareReset() throws SQLException {
try (MariaDbPoolDataSource pool =
new MariaDbPoolDataSource(
connUri + "&maxPoolSize=1&useServerPrepStmts=true&useResetConnection")) {
try (Connection connection = pool.getConnection()) {
PreparedStatement preparedStatement = connection.prepareStatement("SELECT ?");
preparedStatement.setString(1, "1");
preparedStatement.execute();
}
try (Connection connection = pool.getConnection()) {
// must re-prepare
PreparedStatement preparedStatement = connection.prepareStatement("SELECT ?");
preparedStatement.setString(1, "1");
preparedStatement.execute();
}
}
}
}