Check db availability before startup

66 views
Skip to first unread message

siraj

unread,
Feb 7, 2020, 3:22:32 PM2/7/20
to dropwizard-user
In DW 2, I'm starting a secondary db that relies on a VPN connection. 

Currently if VPN is unavailable DW fails to start with DB exception. What's the best approach to ignore DB availability and continue startup and optionally, try later?

My run method
    @Override
    public void run(final AppConfiguration configuration, final Environment environment) throws Exception {

        final Injector injector = Guice.createInjector(new ServiceModule());

        final JdbiFactory jdbiFactory = new JdbiFactory();
        jdbi = jdbiFactory.build(environment, configuration.getDataSourceFactory(), "sqlserver");
        environment.jersey().register(injector.getInstance(JumpResource.class));
    }


Without VPN:
ERROR [2020-02-07 18:35:48,409] org.apache.tomcat.jdbc.pool.ConnectionPool: Unable to create initial connections of pool.
! com.microsoft.sqlserver.jdbc.SQLServerException: The connection to the host DEVSQL2, named instance sqlserver16 failed. Error: "java.net.UnknownHostException: DEVSQL2". Verify the server and instance names and check that no firewall is blocking UDP traffic to port 1434.  For SQL Server 2005 or later, verify that the SQL Server Browser Service is running on the host.
! at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:234)
! at com.microsoft.sqlserver.jdbc.SQLServerConnection.getInstancePort(SQLServerConnection.java:5963)
! at com.microsoft.sqlserver.jdbc.SQLServerConnection.primaryPermissionCheck(SQLServerConnection.java:2383)
! at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:2126)
! at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:1993)
! at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:1164)
! at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:760)
! at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:319)
! at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:212)
! at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:744)
! at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:676)
! at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:483)
! at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:154)
! at org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:118)
! at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:107)
! at io.dropwizard.db.ManagedPooledDataSource.start(ManagedPooledDataSource.java:37)
! at io.dropwizard.lifecycle.JettyManaged.doStart(JettyManaged.java:27)
! at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:72)
! at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:169)
! at org.eclipse.jetty.server.Server.start(Server.java:407)
! at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:117)
! at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:97)
! at org.eclipse.jetty.server.Server.doStart(Server.java:371)
! at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:72)
! at io.dropwizard.cli.ServerCommand.run(ServerCommand.java:53)
! at io.dropwizard.cli.EnvironmentCommand.run(EnvironmentCommand.java:45)
! at io.dropwizard.cli.ConfiguredCommand.run(ConfiguredCommand.java:87)
! at io.dropwizard.cli.Cli.run(Cli.java:79)
! at io.dropwizard.Application.run(Application.java:94)
! at com.barone.JumpApplication.main(JumpApplication.java:28)




Oscar Nalin Nilsson

unread,
Feb 7, 2020, 3:41:42 PM2/7/20
to dropwiz...@googlegroups.com
You can configure your connection pool to ignore exceptions on creation. There is a property called "ignoreExceptionOnPreLoad" which you can set to "true". Then the application will start even though the database is unavailable.

Look under "Database" in the link below:

Best regards
Oscar Nalin

--
You received this message because you are subscribed to the Google Groups "dropwizard-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dropwizard-us...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dropwizard-user/a2b35316-b89c-4778-8ae6-545214b4c54a%40googlegroups.com.

siraj

unread,
Feb 7, 2020, 4:02:10 PM2/7/20
to dropwizard-user
Thanks Oscar --- such an elegant solution - I tried it and it worked as expected.

Is there an option to retry DB availability - either automatically or through a Task etc? 
To unsubscribe from this group and stop receiving emails from it, send an email to dropwiz...@googlegroups.com.

Oscar Nalin Nilsson

unread,
Feb 8, 2020, 4:43:44 PM2/8/20
to dropwiz...@googlegroups.com
I'm unsure what you mean with retry database availability. With default connection pool values, the connection pool will keep trying to create healthy connections to the database until it comes up again. I think this is what you want.

Best regards
Oscar Nalin

To unsubscribe from this group and stop receiving emails from it, send an email to dropwizard-us...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dropwizard-user/d5f2560f-ab32-4d42-9b99-b20ba68cb5c2%40googlegroups.com.

siraj

unread,
Feb 9, 2020, 11:33:29 AM2/9/20
to dropwizard-user
Thanks again Oscar. I did not realize it would retry the connection - I thought it was one try at startup and done. 
I verified the behavior by starting VPN much later after service startup and it connected.
Oscar Nalin

Dondell Batac

unread,
Aug 4, 2021, 5:28:59 AM8/4/21
to dropwizard-user
Hi Oscar,

Can you help me with this issue below with JdbiFactory

java.lang.NoClassDefFoundError: com/codahale/metrics/jdbi3/strategies/StatementNameStrategy
    at com.fulcrumgt.tardis.TardisApplication.run(TardisApplication.java:169)
    at com.fulcrumgt.tardis.TardisApplication.run(TardisApplication.java:72)
    at io.dropwizard.cli.EnvironmentCommand.run(EnvironmentCommand.java:44)
    at io.dropwizard.cli.ConfiguredCommand.run(ConfiguredCommand.java:87)
    at io.dropwizard.cli.Cli.run(Cli.java:79)
    at io.dropwizard.Application.run(Application.java:94)
    at com.fulcrumgt.tardis.TardisApplication.main(TardisApplication.java:87)
Caused by: java.lang.ClassNotFoundException: com.codahale.metrics.jdbi3.strategies.StatementNameStrategy
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
    ... 7 more
Reply all
Reply to author
Forward
Message has been deleted
0 new messages