| 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 |
package com.zaxxer.hikari.pool; |
| 18 |
|
| 19 |
import static com.zaxxer.hikari.pool.TestElf.getPool; |
| 20 |
import static com.zaxxer.hikari.pool.TestElf.newHikariConfig; |
| 21 |
import static com.zaxxer.hikari.pool.TestElf.setSlf4jLogLevel; |
| 22 |
import static com.zaxxer.hikari.pool.TestElf.setSlf4jTargetStream; |
| 23 |
import static com.zaxxer.hikari.util.ClockSource.currentTime; |
| 24 |
import static com.zaxxer.hikari.util.ClockSource.elapsedMillis; |
| 25 |
import static java.lang.Thread.sleep; |
| 26 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
| 27 |
import static org.junit.Assert.assertNotNull; |
| 28 |
import static org.junit.Assert.assertSame; |
| 29 |
import static org.junit.Assert.assertTrue; |
| 30 |
import static org.junit.Assert.fail; |
| 31 |
|
| 32 |
import java.io.ByteArrayOutputStream; |
| 33 |
import java.io.PrintStream; |
| 34 |
import java.sql.Connection; |
| 35 |
import java.sql.SQLException; |
| 36 |
import java.util.concurrent.Executors; |
| 37 |
import java.util.concurrent.ScheduledExecutorService; |
| 38 |
import java.util.concurrent.TimeUnit; |
| 39 |
|
| 40 |
import org.apache.logging.log4j.Level; |
| 41 |
import org.junit.After; |
| 42 |
import org.junit.Before; |
| 43 |
import org.junit.Test; |
| 44 |
|
| 45 |
import com.zaxxer.hikari.HikariConfig; |
| 46 |
import com.zaxxer.hikari.HikariDataSource; |
| 47 |
import com.zaxxer.hikari.mocks.StubConnection; |
| 48 |
import com.zaxxer.hikari.mocks.StubDataSource; |
| 49 |
|
| 50 |
public class TestConnectionTimeoutRetry |
| 51 |
{ |
| 52 |
@Test |
| 53 |
public void testConnectionRetries() throws SQLException |
| 54 |
{ |
| 55 |
HikariConfig config = newHikariConfig(); |
| 56 |
config.setMinimumIdle(0); |
| 57 |
config.setMaximumPoolSize(1); |
| 58 |
config.setConnectionTimeout(2800); |
| 59 |
config.setValidationTimeout(2800); |
| 60 |
config.setConnectionTestQuery("VALUES 1"); |
| 61 |
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); |
| 62 |
|
| 63 |
try (HikariDataSource ds = new HikariDataSource(config)) { |
| 64 |
StubDataSource stubDataSource = ds.unwrap(StubDataSource.class); |
| 65 |
stubDataSource.setThrowException(new SQLException("Connection refused")); |
| 66 |
|
| 67 |
long start = currentTime(); |
| 68 |
try (Connection connection = ds.getConnection()) { |
| 69 |
connection.close(); |
| 70 |
fail("Should not have been able to get a connection."); |
| 71 |
} |
| 72 |
catch (SQLException e) { |
| 73 |
long elapsed = elapsedMillis(start); |
| 74 |
long timeout = config.getConnectionTimeout(); |
| 75 |
assertTrue("Didn't wait long enough for timeout", (elapsed >= timeout)); |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
@Test |
| 81 |
public void testConnectionRetries2() throws SQLException |
| 82 |
{ |
| 83 |
HikariConfig config = newHikariConfig(); |
| 84 |
config.setMinimumIdle(0); |
| 85 |
config.setMaximumPoolSize(1); |
| 86 |
config.setConnectionTimeout(2800); |
| 87 |
config.setValidationTimeout(2800); |
| 88 |
config.setInitializationFailTimeout(0); |
| 89 |
config.setConnectionTestQuery("VALUES 1"); |
| 90 |
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); |
| 91 |
|
| 92 |
try (HikariDataSource ds = new HikariDataSource(config)) { |
| 93 |
final StubDataSource stubDataSource = ds.unwrap(StubDataSource.class); |
| 94 |
stubDataSource.setThrowException(new SQLException("Connection refused")); |
| 95 |
|
| 96 |
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); |
| 97 |
scheduler.schedule(new Runnable() { |
| 98 |
@Override |
| 99 |
public void run() |
| 100 |
{ |
| 101 |
stubDataSource.setThrowException(null); |
| 102 |
} |
| 103 |
}, 300, TimeUnit.MILLISECONDS); |
| 104 |
|
| 105 |
long start = currentTime(); |
| 106 |
try { |
| 107 |
try (Connection connection = ds.getConnection()) { |
| 108 |
// close immediately |
| 109 |
} |
| 110 |
|
| 111 |
long elapsed = elapsedMillis(start); |
| 112 |
assertTrue("Connection returned too quickly, something is wrong.", elapsed > 250); |
| 113 |
assertTrue("Waited too long to get a connection.", elapsed < config.getConnectionTimeout()); |
| 114 |
} |
| 115 |
catch (SQLException e) { |
| 116 |
fail("Should not have timed out: " + e.getMessage()); |
| 117 |
} |
| 118 |
finally { |
| 119 |
scheduler.shutdownNow(); |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
@Test |
| 125 |
public void testConnectionRetries3() throws SQLException |
| 126 |
{ |
| 127 |
HikariConfig config = newHikariConfig(); |
| 128 |
config.setMinimumIdle(0); |
| 129 |
config.setMaximumPoolSize(2); |
| 130 |
config.setConnectionTimeout(2800); |
| 131 |
config.setValidationTimeout(2800); |
| 132 |
config.setConnectionTestQuery("VALUES 1"); |
| 133 |
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); |
| 134 |
|
| 135 |
try (HikariDataSource ds = new HikariDataSource(config)) { |
| 136 |
final Connection connection1 = ds.getConnection(); |
| 137 |
final Connection connection2 = ds.getConnection(); |
| 138 |
assertNotNull(connection1); |
| 139 |
assertNotNull(connection2); |
| 140 |
|
| 141 |
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2); |
| 142 |
scheduler.schedule(new Runnable() { |
| 143 |
@Override |
| 144 |
public void run() |
| 145 |
{ |
| 146 |
try { |
| 147 |
connection1.close(); |
| 148 |
} |
| 149 |
catch (Exception e) { |
| 150 |
e.printStackTrace(System.err); |
| 151 |
} |
| 152 |
} |
| 153 |
}, 800, MILLISECONDS); |
| 154 |
|
| 155 |
long start = currentTime(); |
| 156 |
try { |
| 157 |
try (Connection connection3 = ds.getConnection()) { |
| 158 |
// close immediately |
| 159 |
} |
| 160 |
|
| 161 |
long elapsed = elapsedMillis(start); |
| 162 |
assertTrue("Waited too long to get a connection.", (elapsed >= 700) && (elapsed < 950)); |
| 163 |
} |
| 164 |
catch (SQLException e) { |
| 165 |
fail("Should not have timed out."); |
| 166 |
} |
| 167 |
finally { |
| 168 |
scheduler.shutdownNow(); |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
@Test |
| 174 |
public void testConnectionRetries5() throws SQLException |
| 175 |
{ |
| 176 |
HikariConfig config = newHikariConfig(); |
| 177 |
config.setMinimumIdle(0); |
| 178 |
config.setMaximumPoolSize(2); |
| 179 |
config.setConnectionTimeout(1000); |
| 180 |
config.setValidationTimeout(1000); |
| 181 |
config.setConnectionTestQuery("VALUES 1"); |
| 182 |
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); |
| 183 |
|
| 184 |
try (HikariDataSource ds = new HikariDataSource(config)) { |
| 185 |
final Connection connection1 = ds.getConnection(); |
| 186 |
|
| 187 |
long start = currentTime(); |
| 188 |
|
| 189 |
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2); |
| 190 |
scheduler.schedule(new Runnable() { |
| 191 |
@Override |
| 192 |
public void run() |
| 193 |
{ |
| 194 |
try { |
| 195 |
connection1.close(); |
| 196 |
} |
| 197 |
catch (Exception e) { |
| 198 |
e.printStackTrace(System.err); |
| 199 |
} |
| 200 |
} |
| 201 |
}, 250, MILLISECONDS); |
| 202 |
|
| 203 |
StubDataSource stubDataSource = ds.unwrap(StubDataSource.class); |
| 204 |
stubDataSource.setThrowException(new SQLException("Connection refused")); |
| 205 |
|
| 206 |
try { |
| 207 |
try (Connection connection2 = ds.getConnection()) { |
| 208 |
// close immediately |
| 209 |
} |
| 210 |
|
| 211 |
long elapsed = elapsedMillis(start); |
| 212 |
assertTrue("Waited too long to get a connection.", (elapsed >= 250) && (elapsed < config.getConnectionTimeout())); |
| 213 |
} |
| 214 |
catch (SQLException e) { |
| 215 |
fail("Should not have timed out."); |
| 216 |
} |
| 217 |
finally { |
| 218 |
scheduler.shutdownNow(); |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
@Test |
| 224 |
public void testConnectionIdleFill() throws Exception |
| 225 |
{ |
| 226 |
StubConnection.slowCreate = false; |
| 227 |
|
| 228 |
HikariConfig config = newHikariConfig(); |
| 229 |
config.setMinimumIdle(5); |
| 230 |
config.setMaximumPoolSize(10); |
| 231 |
config.setConnectionTimeout(2000); |
| 232 |
config.setValidationTimeout(2000); |
| 233 |
config.setConnectionTestQuery("VALUES 2"); |
| 234 |
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); |
| 235 |
|
| 236 |
System.setProperty("com.zaxxer.hikari.housekeeping.periodMs", "400"); |
| 237 |
|
| 238 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 239 |
PrintStream ps = new PrintStream(baos, true); |
| 240 |
setSlf4jTargetStream(HikariPool.class, ps); |
| 241 |
|
| 242 |
try (HikariDataSource ds = new HikariDataSource(config)) { |
| 243 |
setSlf4jLogLevel(HikariPool.class, Level.DEBUG); |
| 244 |
|
| 245 |
HikariPool pool = getPool(ds); |
| 246 |
try ( |
| 247 |
Connection connection1 = ds.getConnection(); |
| 248 |
Connection connection2 = ds.getConnection(); |
| 249 |
Connection connection3 = ds.getConnection(); |
| 250 |
Connection connection4 = ds.getConnection(); |
| 251 |
Connection connection5 = ds.getConnection(); |
| 252 |
Connection connection6 = ds.getConnection(); |
| 253 |
Connection connection7 = ds.getConnection()) { |
| 254 |
|
| 255 |
sleep(1300); |
| 256 |
|
| 257 |
assertSame("Total connections not as expected", 10, pool.getTotalConnections()); |
| 258 |
assertSame("Idle connections not as expected", 3, pool.getIdleConnections()); |
| 259 |
} |
| 260 |
|
| 261 |
assertSame("Total connections not as expected", 10, pool.getTotalConnections()); |
| 262 |
assertSame("Idle connections not as expected", 10, pool.getIdleConnections()); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
@Before |
| 267 |
public void before() |
| 268 |
{ |
| 269 |
setSlf4jLogLevel(HikariPool.class, Level.INFO); |
| 270 |
} |
| 271 |
|
| 272 |
@After |
| 273 |
public void after() |
| 274 |
{ |
| 275 |
System.getProperties().remove("com.zaxxer.hikari.housekeeping.periodMs"); |
| 276 |
setSlf4jLogLevel(HikariPool.class, Level.INFO); |
| 277 |
} |
| 278 |
} |