| 1 |
/* |
| 2 |
* Copyright (C) 2013 Brett Wooldridge |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package com.zaxxer.hikari.pool; |
| 18 |
|
| 19 |
import com.zaxxer.hikari.HikariConfig; |
| 20 |
import com.zaxxer.hikari.HikariDataSource; |
| 21 |
import com.zaxxer.hikari.mocks.StubConnection; |
| 22 |
import com.zaxxer.hikari.mocks.StubDataSource; |
| 23 |
import com.zaxxer.hikari.mocks.StubStatement; |
| 24 |
import com.zaxxer.hikari.pool.HikariPool.PoolInitializationException; |
| 25 |
import org.apache.logging.log4j.Level; |
| 26 |
import org.junit.After; |
| 27 |
import org.junit.Before; |
| 28 |
import org.junit.Test; |
| 29 |
|
| 30 |
import java.sql.*; |
| 31 |
import java.util.concurrent.TimeUnit; |
| 32 |
import java.util.concurrent.atomic.AtomicReference; |
| 33 |
|
| 34 |
import static com.zaxxer.hikari.pool.TestElf.*; |
| 35 |
import static com.zaxxer.hikari.util.UtilityElf.quietlySleep; |
| 36 |
import static java.util.concurrent.TimeUnit.SECONDS; |
| 37 |
import static org.junit.Assert.*; |
| 38 |
|
| 39 |
/** |
| 40 |
* @author Brett Wooldridge |
| 41 |
*/ |
| 42 |
public class TestConnections |
| 43 |
{ |
| 44 |
@Before |
| 45 |
public void before() |
| 46 |
{ |
| 47 |
setSlf4jTargetStream(HikariPool.class, System.err); |
| 48 |
setSlf4jLogLevel(HikariPool.class, Level.DEBUG); |
| 49 |
setSlf4jLogLevel(PoolBase.class, Level.DEBUG); |
| 50 |
} |
| 51 |
|
| 52 |
@After |
| 53 |
public void after() |
| 54 |
{ |
| 55 |
System.getProperties().remove("com.zaxxer.hikari.housekeeping.periodMs"); |
| 56 |
setSlf4jLogLevel(HikariPool.class, Level.WARN); |
| 57 |
setSlf4jLogLevel(PoolBase.class, Level.WARN); |
| 58 |
} |
| 59 |
|
| 60 |
@Test |
| 61 |
public void testCreate() throws SQLException |
| 62 |
{ |
| 63 |
HikariConfig config = newHikariConfig(); |
| 64 |
config.setMinimumIdle(1); |
| 65 |
config.setMaximumPoolSize(1); |
| 66 |
config.setConnectionTestQuery("VALUES 1"); |
| 67 |
config.setConnectionInitSql("SELECT 1"); |
| 68 |
config.setReadOnly(true); |
| 69 |
config.setConnectionTimeout(2500); |
| 70 |
config.setLeakDetectionThreshold(TimeUnit.SECONDS.toMillis(30)); |
| 71 |
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); |
| 72 |
|
| 73 |
try (HikariDataSource ds = new HikariDataSource(config)) { |
| 74 |
ds.setLoginTimeout(10); |
| 75 |
assertSame(10, ds.getLoginTimeout()); |
| 76 |
|
| 77 |
HikariPool pool = getPool(ds); |
| 78 |
ds.getConnection().close(); |
| 79 |
assertSame("Total connections not as expected", 1, pool.getTotalConnections()); |
| 80 |
assertSame("Idle connections not as expected", 1, pool.getIdleConnections()); |
| 81 |
|
| 82 |
try (Connection connection = ds.getConnection(); |
| 83 |
PreparedStatement statement = connection.prepareStatement("SELECT * FROM device WHERE device_id=?")) { |
| 84 |
|
| 85 |
assertNotNull(connection); |
| 86 |
assertNotNull(statement); |
| 87 |
|
| 88 |
assertSame("Total connections not as expected", 1, pool.getTotalConnections()); |
| 89 |
assertSame("Idle connections not as expected", 0, pool.getIdleConnections()); |
| 90 |
|
| 91 |
statement.setInt(1, 0); |
| 92 |
|
| 93 |
try (ResultSet resultSet = statement.executeQuery()) { |
| 94 |
assertNotNull(resultSet); |
| 95 |
|
| 96 |
assertFalse(resultSet.next()); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
assertSame("Total connections not as expected", 1, pool.getTotalConnections()); |
| 101 |
assertSame("Idle connections not as expected", 1, pool.getIdleConnections()); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
@Test |
| 106 |
public void testMaxLifetime() throws Exception |
| 107 |
{ |
| 108 |
HikariConfig config = newHikariConfig(); |
| 109 |
config.setMinimumIdle(0); |
| 110 |
config.setMaximumPoolSize(1); |
| 111 |
config.setConnectionTimeout(2500); |
| 112 |
config.setConnectionTestQuery("VALUES 1"); |
| 113 |
config.setInitializationFailTimeout(Long.MAX_VALUE); |
| 114 |
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); |
| 115 |
|
| 116 |
System.setProperty("com.zaxxer.hikari.housekeeping.periodMs", "100"); |
| 117 |
|
| 118 |
setConfigUnitTest(true); |
| 119 |
try (HikariDataSource ds = new HikariDataSource(config)) { |
| 120 |
System.clearProperty("com.zaxxer.hikari.housekeeping.periodMs"); |
| 121 |
|
| 122 |
getUnsealedConfig(ds).setMaxLifetime(700); |
| 123 |
|
| 124 |
HikariPool pool = getPool(ds); |
| 125 |
|
| 126 |
assertSame("Total connections not as expected", 0, pool.getTotalConnections()); |
| 127 |
assertSame("Idle connections not as expected", 0, pool.getIdleConnections()); |
| 128 |
|
| 129 |
Connection unwrap; |
| 130 |
Connection unwrap2; |
| 131 |
try (Connection connection = ds.getConnection()) { |
| 132 |
unwrap = connection.unwrap(Connection.class); |
| 133 |
assertNotNull(connection); |
| 134 |
|
| 135 |
assertSame("Second total connections not as expected", 1, pool.getTotalConnections()); |
| 136 |
assertSame("Second idle connections not as expected", 0, pool.getIdleConnections()); |
| 137 |
} |
| 138 |
|
| 139 |
assertSame("Idle connections not as expected", 1, pool.getIdleConnections()); |
| 140 |
|
| 141 |
try (Connection connection = ds.getConnection()) { |
| 142 |
unwrap2 = connection.unwrap(Connection.class); |
| 143 |
assertSame(unwrap, unwrap2); |
| 144 |
assertSame("Second total connections not as expected", 1, pool.getTotalConnections()); |
| 145 |
assertSame("Second idle connections not as expected", 0, pool.getIdleConnections()); |
| 146 |
} |
| 147 |
|
| 148 |
quietlySleep(TimeUnit.SECONDS.toMillis(2)); |
| 149 |
|
| 150 |
try (Connection connection = ds.getConnection()) { |
| 151 |
unwrap2 = connection.unwrap(Connection.class); |
| 152 |
assertNotSame("Expected a different connection", unwrap, unwrap2); |
| 153 |
} |
| 154 |
|
| 155 |
assertSame("Post total connections not as expected", 1, pool.getTotalConnections()); |
| 156 |
assertSame("Post idle connections not as expected", 1, pool.getIdleConnections()); |
| 157 |
} |
| 158 |
finally { |
| 159 |
setConfigUnitTest(false); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
@Test |
| 164 |
public void testMaxLifetime2() throws Exception |
| 165 |
{ |
| 166 |
HikariConfig config = newHikariConfig(); |
| 167 |
config.setMinimumIdle(0); |
| 168 |
config.setMaximumPoolSize(1); |
| 169 |
config.setConnectionTimeout(2500); |
| 170 |
config.setConnectionTestQuery("VALUES 1"); |
| 171 |
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); |
| 172 |
|
| 173 |
System.setProperty("com.zaxxer.hikari.housekeeping.periodMs", "100"); |
| 174 |
|
| 175 |
setConfigUnitTest(true); |
| 176 |
try (HikariDataSource ds = new HikariDataSource(config)) { |
| 177 |
getUnsealedConfig(ds).setMaxLifetime(700); |
| 178 |
|
| 179 |
HikariPool pool = getPool(ds); |
| 180 |
assertSame("Total connections not as expected", 0, pool.getTotalConnections()); |
| 181 |
assertSame("Idle connections not as expected", 0, pool.getIdleConnections()); |
| 182 |
|
| 183 |
Connection unwrap; |
| 184 |
Connection unwrap2; |
| 185 |
try (Connection connection = ds.getConnection()) { |
| 186 |
unwrap = connection.unwrap(Connection.class); |
| 187 |
assertNotNull(connection); |
| 188 |
|
| 189 |
assertSame("Second total connections not as expected", 1, pool.getTotalConnections()); |
| 190 |
assertSame("Second idle connections not as expected", 0, pool.getIdleConnections()); |
| 191 |
} |
| 192 |
|
| 193 |
assertSame("Idle connections not as expected", 1, pool.getIdleConnections()); |
| 194 |
|
| 195 |
try (Connection connection = ds.getConnection()) { |
| 196 |
unwrap2 = connection.unwrap(Connection.class); |
| 197 |
assertSame(unwrap, unwrap2); |
| 198 |
assertSame("Second total connections not as expected", 1, pool.getTotalConnections()); |
| 199 |
assertSame("Second idle connections not as expected", 0, pool.getIdleConnections()); |
| 200 |
} |
| 201 |
|
| 202 |
quietlySleep(800); |
| 203 |
|
| 204 |
try (Connection connection = ds.getConnection()) { |
| 205 |
unwrap2 = connection.unwrap(Connection.class); |
| 206 |
assertNotSame("Expected a different connection", unwrap, unwrap2); |
| 207 |
} |
| 208 |
|
| 209 |
assertSame("Post total connections not as expected", 1, pool.getTotalConnections()); |
| 210 |
assertSame("Post idle connections not as expected", 1, pool.getIdleConnections()); |
| 211 |
} |
| 212 |
finally { |
| 213 |
setConfigUnitTest(false); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
@Test |
| 218 |
public void testDoubleClose() throws Exception |
| 219 |
{ |
| 220 |
HikariConfig config = newHikariConfig(); |
| 221 |
config.setMinimumIdle(1); |
| 222 |
config.setMaximumPoolSize(1); |
| 223 |
config.setConnectionTimeout(2500); |
| 224 |
config.setConnectionTestQuery("VALUES 1"); |
| 225 |
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); |
| 226 |
|
| 227 |
try (HikariDataSource ds = new HikariDataSource(config); |
| 228 |
Connection connection = ds.getConnection()) { |
| 229 |
connection.close(); |
| 230 |
|
| 231 |
// should no-op |
| 232 |
connection.abort(null); |
| 233 |
|
| 234 |
assertTrue("Connection should have closed", connection.isClosed()); |
| 235 |
assertFalse("Connection should have closed", connection.isValid(5)); |
| 236 |
assertTrue("Expected to contain ClosedConnection, but was " + connection, connection.toString().contains("ClosedConnection")); |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
@Test |
| 241 |
public void testEviction() throws Exception |
| 242 |
{ |
| 243 |
HikariConfig config = newHikariConfig(); |
| 244 |
config.setMinimumIdle(0); |
| 245 |
config.setMaximumPoolSize(5); |
| 246 |
config.setConnectionTimeout(2500); |
| 247 |
config.setConnectionTestQuery("VALUES 1"); |
| 248 |
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); |
| 249 |
|
| 250 |
try (HikariDataSource ds = new HikariDataSource(config)) { |
| 251 |
Connection connection = ds.getConnection(); |
| 252 |
|
| 253 |
HikariPool pool = getPool(ds); |
| 254 |
assertEquals(1, pool.getTotalConnections()); |
| 255 |
ds.evictConnection(connection); |
| 256 |
assertEquals(0, pool.getTotalConnections()); |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
@Test |
| 261 |
public void testBackfill() throws Exception |
| 262 |
{ |
| 263 |
HikariConfig config = newHikariConfig(); |
| 264 |
config.setMinimumIdle(1); |
| 265 |
config.setMaximumPoolSize(4); |
| 266 |
config.setConnectionTimeout(1000); |
| 267 |
config.setInitializationFailTimeout(Long.MAX_VALUE); |
| 268 |
config.setConnectionTestQuery("VALUES 1"); |
| 269 |
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); |
| 270 |
|
| 271 |
StubConnection.slowCreate = true; |
| 272 |
try (HikariDataSource ds = new HikariDataSource(config)) { |
| 273 |
|
| 274 |
HikariPool pool = getPool(ds); |
| 275 |
quietlySleep(1250); |
| 276 |
|
| 277 |
assertSame("Total connections not as expected", 1, pool.getTotalConnections()); |
| 278 |
assertSame("Idle connections not as expected", 1, pool.getIdleConnections()); |
| 279 |
|
| 280 |
// This will take the pool down to zero |
| 281 |
try (Connection connection = ds.getConnection()) { |
| 282 |
assertNotNull(connection); |
| 283 |
|
| 284 |
assertSame("Total connections not as expected", 1, pool.getTotalConnections()); |
| 285 |
assertSame("Idle connections not as expected", 0, pool.getIdleConnections()); |
| 286 |
|
| 287 |
PreparedStatement statement = connection.prepareStatement("SELECT some, thing FROM somewhere WHERE something=?"); |
| 288 |
assertNotNull(statement); |
| 289 |
|
| 290 |
ResultSet resultSet = statement.executeQuery(); |
| 291 |
assertNotNull(resultSet); |
| 292 |
|
| 293 |
try { |
| 294 |
statement.getMaxFieldSize(); |
| 295 |
fail(); |
| 296 |
} |
| 297 |
catch (Exception e) { |
| 298 |
assertSame(SQLException.class, e.getClass()); |
| 299 |
} |
| 300 |
|
| 301 |
pool.logPoolState("testBackfill() before close..."); |
| 302 |
|
| 303 |
// The connection will be ejected from the pool here |
| 304 |
} |
| 305 |
|
| 306 |
assertSame("Total connections not as expected", 0, pool.getTotalConnections()); |
| 307 |
|
| 308 |
pool.logPoolState("testBackfill() after close..."); |
| 309 |
|
| 310 |
quietlySleep(1250); |
| 311 |
|
| 312 |
assertSame("Total connections not as expected", 1, pool.getTotalConnections()); |
| 313 |
assertSame("Idle connections not as expected", 1, pool.getIdleConnections()); |
| 314 |
} |
| 315 |
finally { |
| 316 |
StubConnection.slowCreate = false; |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
@Test |
| 321 |
public void testMaximumPoolLimit() throws Exception |
| 322 |
{ |
| 323 |
HikariConfig config = newHikariConfig(); |
| 324 |
config.setMinimumIdle(1); |
| 325 |
config.setMaximumPoolSize(4); |
| 326 |
config.setConnectionTimeout(20000); |
| 327 |
config.setInitializationFailTimeout(0); |
| 328 |
config.setConnectionTestQuery("VALUES 1"); |
| 329 |
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); |
| 330 |
|
| 331 |
final AtomicReference<Exception> ref = new AtomicReference<>(); |
| 332 |
|
| 333 |
StubConnection.count.set(0); // reset counter |
| 334 |
|
| 335 |
try (final HikariDataSource ds = new HikariDataSource(config)) { |
| 336 |
|
| 337 |
final HikariPool pool = getPool(ds); |
| 338 |
|
| 339 |
Thread[] threads = new Thread[20]; |
| 340 |
for (int i = 0; i < threads.length; i++) { |
| 341 |
threads[i] = new Thread(() -> { |
| 342 |
try { |
| 343 |
pool.logPoolState("Before acquire "); |
| 344 |
try (Connection ignored = ds.getConnection()) { |
| 345 |
pool.logPoolState("After acquire "); |
| 346 |
quietlySleep(500); |
| 347 |
} |
| 348 |
} |
| 349 |
catch (Exception e) { |
| 350 |
ref.set(e); |
| 351 |
} |
| 352 |
}); |
| 353 |
} |
| 354 |
|
| 355 |
for (Thread thread : threads) { |
| 356 |
thread.start(); |
| 357 |
} |
| 358 |
|
| 359 |
for (Thread thread : threads) { |
| 360 |
thread.join(); |
| 361 |
} |
| 362 |
|
| 363 |
pool.logPoolState("before check "); |
| 364 |
assertNull((ref.get() != null ? ref.get().toString() : ""), ref.get()); |
| 365 |
assertSame("StubConnection count not as expected", 4, StubConnection.count.get()); |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
@Test |
| 370 |
@SuppressWarnings("EmptyTryBlock") |
| 371 |
public void testOldDriver() throws Exception |
| 372 |
{ |
| 373 |
HikariConfig config = newHikariConfig(); |
| 374 |
config.setMinimumIdle(1); |
| 375 |
config.setMaximumPoolSize(1); |
| 376 |
config.setConnectionTimeout(2500); |
| 377 |
config.setConnectionTestQuery("VALUES 1"); |
| 378 |
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); |
| 379 |
|
| 380 |
StubConnection.oldDriver = true; |
| 381 |
StubStatement.oldDriver = true; |
| 382 |
try (HikariDataSource ds = new HikariDataSource(config)) { |
| 383 |
quietlySleep(500); |
| 384 |
|
| 385 |
try (Connection ignored = ds.getConnection()) { |
| 386 |
// close |
| 387 |
} |
| 388 |
|
| 389 |
quietlySleep(500); |
| 390 |
try (Connection ignored = ds.getConnection()) { |
| 391 |
// close |
| 392 |
} |
| 393 |
} |
| 394 |
finally { |
| 395 |
StubConnection.oldDriver = false; |
| 396 |
StubStatement.oldDriver = false; |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
@Test |
| 401 |
public void testSuspendResume() throws Exception |
| 402 |
{ |
| 403 |
HikariConfig config = newHikariConfig(); |
| 404 |
config.setMinimumIdle(3); |
| 405 |
config.setMaximumPoolSize(3); |
| 406 |
config.setConnectionTimeout(2500); |
| 407 |
config.setAllowPoolSuspension(true); |
| 408 |
config.setConnectionTestQuery("VALUES 1"); |
| 409 |
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); |
| 410 |
|
| 411 |
try (final HikariDataSource ds = new HikariDataSource(config)) { |
| 412 |
HikariPool pool = getPool(ds); |
| 413 |
while (pool.getTotalConnections() < 3) { |
| 414 |
quietlySleep(50); |
| 415 |
} |
| 416 |
|
| 417 |
Thread t = new Thread(() -> { |
| 418 |
try { |
| 419 |
ds.getConnection(); |
| 420 |
ds.getConnection(); |
| 421 |
} |
| 422 |
catch (Exception e) { |
| 423 |
fail(); |
| 424 |
} |
| 425 |
}); |
| 426 |
|
| 427 |
try (Connection ignored = ds.getConnection()) { |
| 428 |
assertEquals(2, pool.getIdleConnections()); |
| 429 |
|
| 430 |
pool.suspendPool(); |
| 431 |
t.start(); |
| 432 |
|
| 433 |
quietlySleep(500); |
| 434 |
assertEquals(2, pool.getIdleConnections()); |
| 435 |
} |
| 436 |
assertEquals(3, pool.getIdleConnections()); |
| 437 |
pool.resumePool(); |
| 438 |
quietlySleep(500); |
| 439 |
assertEquals(1, pool.getIdleConnections()); |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
@Test |
| 444 |
public void testSuspendResumeWithThrow() throws Exception |
| 445 |
{ |
| 446 |
HikariConfig config = newHikariConfig(); |
| 447 |
config.setMinimumIdle(3); |
| 448 |
config.setMaximumPoolSize(3); |
| 449 |
config.setConnectionTimeout(2500); |
| 450 |
config.setAllowPoolSuspension(true); |
| 451 |
config.setConnectionTestQuery("VALUES 1"); |
| 452 |
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); |
| 453 |
|
| 454 |
System.setProperty("com.zaxxer.hikari.throwIfSuspended", "true"); |
| 455 |
try (final HikariDataSource ds = new HikariDataSource(config)) { |
| 456 |
HikariPool pool = getPool(ds); |
| 457 |
while (pool.getTotalConnections() < 3) { |
| 458 |
quietlySleep(50); |
| 459 |
} |
| 460 |
|
| 461 |
AtomicReference<Exception> exception = new AtomicReference<>(); |
| 462 |
Thread t = new Thread(() -> { |
| 463 |
try { |
| 464 |
ds.getConnection(); |
| 465 |
ds.getConnection(); |
| 466 |
} |
| 467 |
catch (Exception e) { |
| 468 |
exception.set(e); |
| 469 |
} |
| 470 |
}); |
| 471 |
|
| 472 |
try (Connection ignored = ds.getConnection()) { |
| 473 |
assertEquals(2, pool.getIdleConnections()); |
| 474 |
|
| 475 |
pool.suspendPool(); |
| 476 |
t.start(); |
| 477 |
|
| 478 |
quietlySleep(500); |
| 479 |
assertEquals(SQLTransientException.class, exception.get().getClass()); |
| 480 |
assertEquals(2, pool.getIdleConnections()); |
| 481 |
} |
| 482 |
|
| 483 |
assertEquals(3, pool.getIdleConnections()); |
| 484 |
pool.resumePool(); |
| 485 |
|
| 486 |
try (Connection ignored = ds.getConnection()) { |
| 487 |
assertEquals(2, pool.getIdleConnections()); |
| 488 |
} |
| 489 |
} |
| 490 |
finally { |
| 491 |
System.getProperties().remove("com.zaxxer.hikari.throwIfSuspended"); |
| 492 |
} |
| 493 |
} |
| 494 |
|
| 495 |
@Test |
| 496 |
public void testInitializationFailure1() |
| 497 |
{ |
| 498 |
StubDataSource stubDataSource = new StubDataSource(); |
| 499 |
stubDataSource.setThrowException(new SQLException("Connection refused")); |
| 500 |
|
| 501 |
try (HikariDataSource ds = newHikariDataSource()) { |
| 502 |
ds.setMinimumIdle(1); |
| 503 |
ds.setMaximumPoolSize(1); |
| 504 |
ds.setConnectionTimeout(2500); |
| 505 |
ds.setConnectionTestQuery("VALUES 1"); |
| 506 |
ds.setDataSource(stubDataSource); |
| 507 |
|
| 508 |
try (Connection ignored = ds.getConnection()) { |
| 509 |
fail("Initialization should have failed"); |
| 510 |
} |
| 511 |
catch (SQLException e) { |
| 512 |
// passed |
| 513 |
} |
| 514 |
} |
| 515 |
} |
| 516 |
|
| 517 |
@Test |
| 518 |
public void testInitializationFailure2() throws SQLException |
| 519 |
{ |
| 520 |
StubDataSource stubDataSource = new StubDataSource(); |
| 521 |
stubDataSource.setThrowException(new SQLException("Connection refused")); |
| 522 |
|
| 523 |
HikariConfig config = newHikariConfig(); |
| 524 |
config.setMinimumIdle(1); |
| 525 |
config.setConnectionTestQuery("VALUES 1"); |
| 526 |
config.setDataSource(stubDataSource); |
| 527 |
|
| 528 |
try (HikariDataSource ds = new HikariDataSource(config); |
| 529 |
Connection ignored = ds.getConnection()) { |
| 530 |
fail("Initialization should have failed"); |
| 531 |
} |
| 532 |
catch (PoolInitializationException e) { |
| 533 |
// passed |
| 534 |
} |
| 535 |
} |
| 536 |
|
| 537 |
@Test |
| 538 |
public void testInvalidConnectionTestQuery() |
| 539 |
{ |
| 540 |
class BadConnection extends StubConnection { |
| 541 |
/** {@inheritDoc} */ |
| 542 |
@Override |
| 543 |
public Statement createStatement() throws SQLException |
| 544 |
{ |
| 545 |
throw new SQLException("Simulated exception in createStatement()"); |
| 546 |
} |
| 547 |
} |
| 548 |
|
| 549 |
StubDataSource stubDataSource = new StubDataSource() { |
| 550 |
/** {@inheritDoc} */ |
| 551 |
@Override |
| 552 |
public Connection getConnection() |
| 553 |
{ |
| 554 |
return new BadConnection(); |
| 555 |
} |
| 556 |
}; |
| 557 |
|
| 558 |
HikariConfig config = newHikariConfig(); |
| 559 |
config.setMinimumIdle(1); |
| 560 |
config.setMaximumPoolSize(2); |
| 561 |
config.setConnectionTimeout(TimeUnit.SECONDS.toMillis(3)); |
| 562 |
config.setConnectionTestQuery("VALUES 1"); |
| 563 |
config.setInitializationFailTimeout(TimeUnit.SECONDS.toMillis(2)); |
| 564 |
config.setDataSource(stubDataSource); |
| 565 |
|
| 566 |
try (HikariDataSource ds = new HikariDataSource(config)) { |
| 567 |
try (Connection ignored = ds.getConnection()) { |
| 568 |
fail("getConnection() should have failed"); |
| 569 |
} |
| 570 |
catch (SQLException e) { |
| 571 |
assertSame("Simulated exception in createStatement()", e.getNextException().getMessage()); |
| 572 |
} |
| 573 |
} |
| 574 |
catch (PoolInitializationException e) { |
| 575 |
assertSame("Simulated exception in createStatement()", e.getCause().getMessage()); |
| 576 |
} |
| 577 |
|
| 578 |
config.setInitializationFailTimeout(0); |
| 579 |
try (HikariDataSource ignored = new HikariDataSource(config)) { |
| 580 |
fail("Initialization should have failed"); |
| 581 |
} |
| 582 |
catch (PoolInitializationException e) { |
| 583 |
// passed |
| 584 |
} |
| 585 |
} |
| 586 |
|
| 587 |
@Test |
| 588 |
public void testPopulationSlowAcquisition() throws InterruptedException, SQLException |
| 589 |
{ |
| 590 |
HikariConfig config = newHikariConfig(); |
| 591 |
config.setMaximumPoolSize(20); |
| 592 |
config.setConnectionTestQuery("VALUES 1"); |
| 593 |
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); |
| 594 |
|
| 595 |
System.setProperty("com.zaxxer.hikari.housekeeping.periodMs", "1000"); |
| 596 |
|
| 597 |
StubConnection.slowCreate = true; |
| 598 |
try (HikariDataSource ds = new HikariDataSource(config)) { |
| 599 |
System.clearProperty("com.zaxxer.hikari.housekeeping.periodMs"); |
| 600 |
|
| 601 |
getUnsealedConfig(ds).setIdleTimeout(3000); |
| 602 |
|
| 603 |
SECONDS.sleep(2); |
| 604 |
|
| 605 |
HikariPool pool = getPool(ds); |
| 606 |
assertSame("Total connections not as expected", 2, pool.getTotalConnections()); |
| 607 |
assertSame("Idle connections not as expected", 2, pool.getIdleConnections()); |
| 608 |
|
| 609 |
try (Connection connection = ds.getConnection()) { |
| 610 |
assertNotNull(connection); |
| 611 |
|
| 612 |
SECONDS.sleep(20); |
| 613 |
|
| 614 |
assertSame("Second total connections not as expected", 20, pool.getTotalConnections()); |
| 615 |
assertSame("Second idle connections not as expected", 19, pool.getIdleConnections()); |
| 616 |
} |
| 617 |
|
| 618 |
assertSame("Idle connections not as expected", 20, pool.getIdleConnections()); |
| 619 |
|
| 620 |
SECONDS.sleep(5); |
| 621 |
|
| 622 |
assertSame("Third total connections not as expected", 20, pool.getTotalConnections()); |
| 623 |
assertSame("Third idle connections not as expected", 20, pool.getIdleConnections()); |
| 624 |
} |
| 625 |
finally { |
| 626 |
StubConnection.slowCreate = false; |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
@Test |
| 631 |
@SuppressWarnings("EmptyTryBlock") |
| 632 |
public void testMinimumIdleZero() throws SQLException |
| 633 |
{ |
| 634 |
HikariConfig config = newHikariConfig(); |
| 635 |
config.setMinimumIdle(0); |
| 636 |
config.setMaximumPoolSize(5); |
| 637 |
config.setConnectionTimeout(1000L); |
| 638 |
config.setConnectionTestQuery("VALUES 1"); |
| 639 |
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); |
| 640 |
|
| 641 |
try (HikariDataSource ds = new HikariDataSource(config); |
| 642 |
Connection ignored = ds.getConnection()) { |
| 643 |
// passed |
| 644 |
} |
| 645 |
catch (SQLTransientConnectionException sqle) { |
| 646 |
fail("Failed to obtain connection"); |
| 647 |
} |
| 648 |
} |
| 649 |
} |