| 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.newHikariConfig; |
| 20 |
|
| 21 |
import java.sql.Connection; |
| 22 |
import java.sql.PreparedStatement; |
| 23 |
import java.util.ArrayList; |
| 24 |
import java.util.List; |
| 25 |
import java.util.concurrent.Callable; |
| 26 |
import java.util.concurrent.ExecutorService; |
| 27 |
import java.util.concurrent.Executors; |
| 28 |
import java.util.concurrent.Future; |
| 29 |
|
| 30 |
import org.junit.Test; |
| 31 |
|
| 32 |
import com.zaxxer.hikari.HikariConfig; |
| 33 |
import com.zaxxer.hikari.HikariDataSource; |
| 34 |
|
| 35 |
/** |
| 36 |
* @author Matthew Tambara ([email protected]) |
| 37 |
*/ |
| 38 |
public class ConcurrentCloseConnectionTest |
| 39 |
{ |
| 40 |
@Test |
| 41 |
public void testConcurrentClose() throws Exception |
| 42 |
{ |
| 43 |
HikariConfig config = newHikariConfig(); |
| 44 |
config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); |
| 45 |
|
| 46 |
try (HikariDataSource ds = new HikariDataSource(config); |
| 47 |
final Connection connection = ds.getConnection()) { |
| 48 |
|
| 49 |
ExecutorService executorService = Executors.newFixedThreadPool(10); |
| 50 |
|
| 51 |
List<Future<?>> futures = new ArrayList<>(); |
| 52 |
|
| 53 |
for (int i = 0; i < 500; i++) { |
| 54 |
final PreparedStatement preparedStatement = |
| 55 |
connection.prepareStatement(""); |
| 56 |
|
| 57 |
futures.add(executorService.submit(new Callable<Void>() { |
| 58 |
|
| 59 |
@Override |
| 60 |
public Void call() throws Exception { |
| 61 |
preparedStatement.close(); |
| 62 |
|
| 63 |
return null; |
| 64 |
} |
| 65 |
|
| 66 |
})); |
| 67 |
} |
| 68 |
|
| 69 |
executorService.shutdown(); |
| 70 |
|
| 71 |
for (Future<?> future : futures) { |
| 72 |
future.get(); |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
} |