Skip to content

Commit a8f6e83

Browse files
authored
Merge pull request #1041 from microsoft/trask/fix-sporadic-jdbc-test-failure
Fix sporadic jdbc test failure
2 parents b962602 + 3817beb commit a8f6e83

File tree

1 file changed

+51
-15
lines changed
  • test/smoke/testApps/Jdbc/src/main/java/com/microsoft/applicationinsights/smoketestapp

1 file changed

+51
-15
lines changed

test/smoke/testApps/Jdbc/src/main/java/com/microsoft/applicationinsights/smoketestapp/JdbcTestServlet.java

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
import java.sql.ResultSet;
77
import java.sql.SQLException;
88
import java.sql.Statement;
9+
import java.util.concurrent.Callable;
10+
import java.util.concurrent.TimeUnit;
911
import javax.servlet.ServletException;
1012
import javax.servlet.annotation.WebServlet;
1113
import javax.servlet.http.HttpServlet;
1214
import javax.servlet.http.HttpServletRequest;
1315
import javax.servlet.http.HttpServletResponse;
1416

17+
import com.google.common.base.Stopwatch;
1518
import com.google.common.base.Strings;
1619
import org.hsqldb.jdbc.JDBCDriver;
1720

@@ -184,7 +187,7 @@ private void executeBatchStatement(Connection connection) throws SQLException {
184187
statement.close();
185188
}
186189

187-
private static void setupHsqldb() throws SQLException {
190+
private static void setupHsqldb() throws Exception {
188191
Connection connection = getHsqldbConnection();
189192
setup(connection);
190193
connection.close();
@@ -218,28 +221,61 @@ private static void setupOracle() throws Exception {
218221
connection.close();
219222
}
220223

221-
private static Connection getHsqldbConnection() throws SQLException {
222-
return JDBCDriver.getConnection("jdbc:hsqldb:mem:test", null);
224+
private static Connection getHsqldbConnection() throws Exception {
225+
return getConnection(new Callable<Connection>() {
226+
@Override public Connection call() throws Exception {
227+
return JDBCDriver.getConnection("jdbc:hsqldb:mem:test", null);
228+
}
229+
});
223230
}
224231

225-
private static Connection getMysqlConnection() throws SQLException {
226-
String hostname = System.getenv("MYSQL");
227-
return DriverManager.getConnection("jdbc:mysql://" + hostname + "/mysql", "root", "password");
232+
private static Connection getMysqlConnection() throws Exception {
233+
return getConnection(new Callable<Connection>() {
234+
@Override public Connection call() throws Exception {
235+
String hostname = System.getenv("MYSQL");
236+
return DriverManager.getConnection("jdbc:mysql://" + hostname + "/mysql", "root", "password");
237+
}
238+
});
228239
}
229240

230-
private static Connection getPostgresConnection() throws SQLException {
231-
String hostname = System.getenv("POSTGRES");
232-
return DriverManager.getConnection("jdbc:postgresql://" + hostname + "/postgres", "postgres", "");
241+
private static Connection getPostgresConnection() throws Exception {
242+
return getConnection(new Callable<Connection>() {
243+
@Override public Connection call() throws Exception {
244+
String hostname = System.getenv("POSTGRES");
245+
return DriverManager.getConnection("jdbc:postgresql://" + hostname + "/postgres", "postgres", "");
246+
}
247+
});
233248
}
234249

235-
private static Connection getSqlServerConnection() throws SQLException {
236-
String hostname = System.getenv("SQLSERVER");
237-
return DriverManager.getConnection("jdbc:sqlserver://" + hostname, "sa", "Password1");
250+
private static Connection getSqlServerConnection() throws Exception {
251+
return getConnection(new Callable<Connection>() {
252+
@Override public Connection call() throws Exception {
253+
String hostname = System.getenv("SQLSERVER");
254+
return DriverManager.getConnection("jdbc:sqlserver://" + hostname, "sa", "Password1");
255+
}
256+
});
238257
}
239258

240-
private static Connection getOracleConnection() throws SQLException {
241-
String hostname = System.getenv("ORACLE");
242-
return DriverManager.getConnection("jdbc:oracle:thin:@" + hostname, "system", "password");
259+
private static Connection getOracleConnection() throws Exception {
260+
return getConnection(new Callable<Connection>() {
261+
@Override public Connection call() throws Exception {
262+
String hostname = System.getenv("ORACLE");
263+
return DriverManager.getConnection("jdbc:oracle:thin:@" + hostname, "system", "password");
264+
}
265+
});
266+
}
267+
268+
private static Connection getConnection(Callable<Connection> callable) throws Exception {
269+
Exception exception;
270+
Stopwatch stopwatch = Stopwatch.createStarted();
271+
do {
272+
try {
273+
return callable.call();
274+
} catch (Exception e) {
275+
exception = e;
276+
}
277+
} while (stopwatch.elapsed(TimeUnit.SECONDS) < 30);
278+
throw exception;
243279
}
244280

245281
private static void setup(Connection connection) throws SQLException {

0 commit comments

Comments
 (0)