close datasource, test setQueryTimeout(), count number of threads
@@ -11,6 +11,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
+import java.lang.management.ManagementFactory;
+import java.lang.management.ThreadInfo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
@@ -152,16 +154,26 @@ public class PoolingTest extends AbstractTest {
config.setJdbcUrl(connectionString);
HikariDataSource ds = new HikariDataSource(config);
+ try{
connect(ds);
}
+ finally{
+ ds.close();
+ }
+ }
@Test
public void testApacheDBCP() throws SQLException {
BasicDataSource ds = new BasicDataSource();
ds.setUrl(connectionString);
+ try{
connect(ds);
}
+ finally{
+ ds.close();
+ }
+ }
private static void connect(DataSource ds) throws SQLException {
Connection con = null;
@@ -170,11 +182,12 @@ public class PoolingTest extends AbstractTest {
try {
con = ds.getConnection();
- con.isValid(5);
-
pst = con.prepareStatement("SELECT SUSER_SNAME()");
+ pst.setQueryTimeout(5);
rs = pst.executeQuery();
+ assertTrue(countTimeoutThreads() >= 1, "Timeout timer is missing.");
+
while (rs.next()) {
rs.getString(1);
}
@@ -193,4 +206,19 @@ public class PoolingTest extends AbstractTest {
}
}
}
+
+ private static int countTimeoutThreads() {
+ int count = 0;
+ String threadName = "mssql-jdbc-TimeoutTimer";
+
+ ThreadInfo[] tinfos = ManagementFactory.getThreadMXBean().getThreadInfo(ManagementFactory.getThreadMXBean().getAllThreadIds(), 0);
+
+ for (ThreadInfo ti : tinfos) {
+ if ((ti.getThreadName().startsWith(threadName)) && (ti.getThreadState().equals(java.lang.Thread.State.TIMED_WAITING))) {
+ count++;
+ }
+ }
+
+ return count;
+ }
}
