|
6 | 6 | import java.sql.ResultSet; |
7 | 7 | import java.sql.SQLException; |
8 | 8 | import java.sql.Statement; |
| 9 | +import java.util.concurrent.Callable; |
| 10 | +import java.util.concurrent.TimeUnit; |
9 | 11 | import javax.servlet.ServletException; |
10 | 12 | import javax.servlet.annotation.WebServlet; |
11 | 13 | import javax.servlet.http.HttpServlet; |
12 | 14 | import javax.servlet.http.HttpServletRequest; |
13 | 15 | import javax.servlet.http.HttpServletResponse; |
14 | 16 |
|
| 17 | +import com.google.common.base.Stopwatch; |
15 | 18 | import com.google.common.base.Strings; |
16 | 19 | import org.hsqldb.jdbc.JDBCDriver; |
17 | 20 |
|
@@ -184,7 +187,7 @@ private void executeBatchStatement(Connection connection) throws SQLException { |
184 | 187 | statement.close(); |
185 | 188 | } |
186 | 189 |
|
187 | | - private static void setupHsqldb() throws SQLException { |
| 190 | + private static void setupHsqldb() throws Exception { |
188 | 191 | Connection connection = getHsqldbConnection(); |
189 | 192 | setup(connection); |
190 | 193 | connection.close(); |
@@ -218,28 +221,61 @@ private static void setupOracle() throws Exception { |
218 | 221 | connection.close(); |
219 | 222 | } |
220 | 223 |
|
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 | + }); |
223 | 230 | } |
224 | 231 |
|
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 | + }); |
228 | 239 | } |
229 | 240 |
|
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 | + }); |
233 | 248 | } |
234 | 249 |
|
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 | + }); |
238 | 257 | } |
239 | 258 |
|
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; |
243 | 279 | } |
244 | 280 |
|
245 | 281 | private static void setup(Connection connection) throws SQLException { |
|
0 commit comments