| 1 |
/* |
| 2 |
* Copyright (C) 2013, 2014 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 |
/** |
| 18 |
* |
| 19 |
*/ |
| 20 |
package com.zaxxer.hikari.pool; |
| 21 |
|
| 22 |
import static com.zaxxer.hikari.pool.TestElf.newHikariConfig; |
| 23 |
import static com.zaxxer.hikari.util.ClockSource.currentTime; |
| 24 |
import static com.zaxxer.hikari.util.ClockSource.elapsedMillis; |
| 25 |
import static com.zaxxer.hikari.util.UtilityElf.quietlySleep; |
| 26 |
import static java.util.concurrent.TimeUnit.SECONDS; |
| 27 |
import static org.junit.Assert.assertTrue; |
| 28 |
import static org.mockito.ArgumentMatchers.anyInt; |
| 29 |
import static org.mockito.Mockito.doAnswer; |
| 30 |
import static org.mockito.Mockito.when; |
| 31 |
|
| 32 |
import java.sql.Connection; |
| 33 |
import java.sql.SQLException; |
| 34 |
|
| 35 |
import org.mockito.invocation.InvocationOnMock; |
| 36 |
import org.mockito.stubbing.Answer; |
| 37 |
|
| 38 |
import com.zaxxer.hikari.HikariConfig; |
| 39 |
import com.zaxxer.hikari.HikariDataSource; |
| 40 |
import com.zaxxer.hikari.mocks.MockDataSource; |
| 41 |
|
| 42 |
/** |
| 43 |
* Test for cases when db network connectivity goes down and close is called on existing connections. By default Hikari |
| 44 |
* blocks longer than getMaximumTimeout (it can hang for a lot of time depending on driver timeout settings). Closing |
| 45 |
* async the connections fixes this issue. |
| 46 |
* |
| 47 |
*/ |
| 48 |
public class TestConnectionCloseBlocking { |
| 49 |
private static volatile boolean shouldFail = false; |
| 50 |
|
| 51 |
// @Test |
| 52 |
public void testConnectionCloseBlocking() throws SQLException { |
| 53 |
HikariConfig config = newHikariConfig(); |
| 54 |
config.setMinimumIdle(0); |
| 55 |
config.setMaximumPoolSize(1); |
| 56 |
config.setConnectionTimeout(1500); |
| 57 |
config.setDataSource(new CustomMockDataSource()); |
| 58 |
|
| 59 |
long start = currentTime(); |
| 60 |
try (HikariDataSource ds = new HikariDataSource(config); |
| 61 |
Connection connection = ds.getConnection()) { |
| 62 |
|
| 63 |
connection.close(); |
| 64 |
|
| 65 |
// Hikari only checks for validity for connections with lastAccess > 1000 ms so we sleep for 1001 ms to force |
| 66 |
// Hikari to do a connection validation which will fail and will trigger the connection to be closed |
| 67 |
quietlySleep(1100L); |
| 68 |
|
| 69 |
shouldFail = true; |
| 70 |
|
| 71 |
// on physical connection close we sleep 2 seconds |
| 72 |
try (Connection connection2 = ds.getConnection()) { |
| 73 |
assertTrue("Waited longer than timeout", (elapsedMillis(start) < config.getConnectionTimeout())); |
| 74 |
} |
| 75 |
} catch (SQLException e) { |
| 76 |
assertTrue("getConnection failed because close connection took longer than timeout", (elapsedMillis(start) < config.getConnectionTimeout())); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
private static class CustomMockDataSource extends MockDataSource { |
| 81 |
@Override |
| 82 |
public Connection getConnection() throws SQLException { |
| 83 |
Connection mockConnection = super.getConnection(); |
| 84 |
when(mockConnection.isValid(anyInt())).thenReturn(!shouldFail); |
| 85 |
doAnswer(new Answer<Void>() { |
| 86 |
@Override |
| 87 |
public Void answer(InvocationOnMock invocation) throws Throwable { |
| 88 |
if (shouldFail) { |
| 89 |
SECONDS.sleep(2); |
| 90 |
} |
| 91 |
return null; |
| 92 |
} |
| 93 |
}).when(mockConnection).close(); |
| 94 |
return mockConnection; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
} |