@@ -29,11 +29,14 @@ public void init() throws ServletException {
2929 if (!Strings .isNullOrEmpty (System .getenv ("SQLSERVER" ))) setupSqlServer ();
3030 // setupOracle();
3131 } catch (Exception e ) {
32+ // surprisingly not all application servers seem to log init exceptions to stdout
33+ e .printStackTrace ();
3234 throw new ServletException (e );
3335 }
3436 }
3537
36- @ Override protected void doGet (HttpServletRequest req , HttpServletResponse resp ) throws ServletException {
38+ @ Override
39+ protected void doGet (HttpServletRequest req , HttpServletResponse resp ) throws ServletException {
3740 try {
3841 doGetInternal (req );
3942 resp .getWriter ().println ("ok" );
@@ -188,81 +191,94 @@ private void executeBatchStatement(Connection connection) throws SQLException {
188191 }
189192
190193 private static void setupHsqldb () throws Exception {
191- Connection connection = getHsqldbConnection ();
194+ Connection connection = getConnection (new Callable <Connection >() {
195+ @ Override
196+ public Connection call () throws Exception {
197+ return getHsqldbConnection ();
198+ }
199+ });
192200 setup (connection );
193201 connection .close ();
194202 }
195203
196204 private static void setupMysql () throws Exception {
197205 Class .forName ("com.mysql.jdbc.Driver" );
198- Connection connection = getMysqlConnection ();
206+ Connection connection = getConnection (new Callable <Connection >() {
207+ @ Override
208+ public Connection call () throws Exception {
209+ Connection connection = getMysqlConnection ();
210+ testConnection (connection , "select 1" );
211+ return connection ;
212+ }
213+ });
199214 setup (connection );
200215 connection .close ();
201216 }
202217
203218 private static void setupPostgres () throws Exception {
204219 Class .forName ("org.postgresql.Driver" );
205- Connection connection = getPostgresConnection ();
220+ Connection connection = getConnection (new Callable <Connection >() {
221+ @ Override
222+ public Connection call () throws Exception {
223+ Connection connection = getPostgresConnection ();
224+ testConnection (connection , "select 1" );
225+ return connection ;
226+ }
227+ });
206228 setup (connection );
207229 connection .close ();
208230 }
209231
210232 private static void setupSqlServer () throws Exception {
211233 Class .forName ("com.microsoft.sqlserver.jdbc.SQLServerDriver" );
212- Connection connection = getSqlServerConnection ();
234+ Connection connection = getConnection (new Callable <Connection >() {
235+ @ Override
236+ public Connection call () throws Exception {
237+ Connection connection = getSqlServerConnection ();
238+ testConnection (connection , "select 1" );
239+ return connection ;
240+ }
241+ });
213242 setup (connection );
214243 connection .close ();
215244 }
216245
217246 private static void setupOracle () throws Exception {
218247 Class .forName ("com.microsoft.sqlserver.jdbc.SQLServerDriver" );
219- Connection connection = getOracleConnection ();
248+ Connection connection = getConnection (new Callable <Connection >() {
249+ @ Override
250+ public Connection call () throws Exception {
251+ Connection connection = getOracleConnection ();
252+ testConnection (connection , "select 1 from dual" );
253+ return connection ;
254+ }
255+ });
220256 setup (connection );
221257 connection .close ();
222258 }
223259
224260 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- });
261+ return JDBCDriver .getConnection ("jdbc:hsqldb:mem:test" , null );
230262 }
231263
232264 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- });
265+ String hostname = System .getenv ("MYSQL" );
266+ return DriverManager .getConnection ("jdbc:mysql://" + hostname + "/mysql" , "root" , "password" );
239267 }
240268
241269 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- });
270+ String hostname = System .getenv ("POSTGRES" );
271+ return DriverManager .getConnection ("jdbc:postgresql://" + hostname + "/postgres" , "postgres" , "" );
248272 }
249273
250274 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- });
275+ String hostname = System .getenv ("SQLSERVER" );
276+ return DriverManager .getConnection ("jdbc:sqlserver://" + hostname , "sa" , "Password1" );
257277 }
258278
259279 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- });
280+ String hostname = System .getenv ("ORACLE" );
281+ return DriverManager .getConnection ("jdbc:oracle:thin:@" + hostname , "system" , "password" );
266282 }
267283
268284 private static Connection getConnection (Callable <Connection > callable ) throws Exception {
@@ -278,6 +294,15 @@ private static Connection getConnection(Callable<Connection> callable) throws Ex
278294 throw exception ;
279295 }
280296
297+ private static void testConnection (Connection connection , String sql ) throws SQLException {
298+ Statement statement = connection .createStatement ();
299+ try {
300+ statement .execute (sql );
301+ } finally {
302+ statement .close ();
303+ }
304+ }
305+
281306 private static void setup (Connection connection ) throws SQLException {
282307 Statement statement = connection .createStatement ();
283308 try {
0 commit comments