Until you can use the H2 database in server mode with Dropwizard using Eclipse and connect with DB Viewer.

It was a little difficult to launch the H2 database with Dropwizard and use it, so I decided to post it. The flow is to create a Dropwizard project in Eclipse, create a class to start the H2 server, and let dropwizard's Environment lifecycle manage this class. Then, access the started H2 server with DB Viewer.

environment

The software used is as follows.

Create a project with Dropwizard

First, let's create a project in Eclipse. Select File → New → Maven Project in Eclipse.

WS000000.JPG

For Maven project, enter dropwizard in the filter and select "java-simple" in "io.dropwizard.archetypes". If this item does not appear here, add it from "Add Archetype".

WS000001.JPG

To add it, enter it as shown in the figure.

WS000002.JPG

It should appear in the list.

WS000004.JPG

Name the project group ID and artifact ID whatever you like. The "name" in "Archetype Available Properties" will be used for the class name of the class that will be created automatically later. Press Finish to create the project.

Add plugin to pom.xml

Add the plugin to pom.xml. The plugins to add are: Plugin name: Group ID: Artifact ID.

Make sure it looks like the figure below.

WS000013.JPG

Create a class to launch the H2 server

Create a class to start the H2 server. I created it under resources.

WS000014.JPG

When creating it, create it as follows.

WS000015.JPG

Add org.junit.rules.ExternalResource to the superclass and io.dropwizard.lifecycle.Managed to the interface. ExternalResource is useful to add when testing with JUnit, and the Managed interface is inherited for addition to the Dropwizard Environment lifecycle.

The code for this class is as follows:

H2ServerResource.java


package hoge.fuga.resources;

import org.h2.tools.Server;
import org.junit.rules.ExternalResource;

import io.dropwizard.lifecycle.Managed;

public class H2ServerResource extends ExternalResource implements Managed {

	private Server tcpServer = null;
	private Server webConsole = null;

	public H2ServerResource()  {
		//TODO auto-generated constructor stub
		super();
	}

	@Override
	public void start() throws Exception {
			try {
				this.before();
			} catch (Throwable e) {
				//TODO auto-generated catch block
				e.printStackTrace();
				throw new Exception("H2 server startup failure", e);
			}
		}


	@Override
	public void stop() throws Exception {
		this.after();
	}

	@Override
	protected void before() throws Throwable {
		//Connection reception with tcp and Web console
		if (tcpServer == null) {
			try {
				tcpServer = Server.createTcpServer("-tcpAllowOthers", "-tcpPort", "20020");
				webConsole = Server.createWebServer("-webPort", "20010");
				tcpServer.start();
				webConsole.start();

			} catch (Throwable e) {
				//TODO auto-generated catch block
				e.printStackTrace();
				throw new Exception("H2 server startup failure", e);
			}
		}
	}

	@Override
	protected void after() {
		//Server shutdown
		tcpServer.shutdown();
		webConsole.shutdown();
	}

}

If you inherit ExternalResource, you should override before and after. It feels good with JUnit. Inheriting the Managed interface requires overriding start and stop. Since they are the same process, they are called from either side. One thing to note here is that Dropwizard will call start and stop, so don't create an instance of Server in the constructor. It throws an error because it creates double instances. Also, just in case, null check is done to prevent double instance creation. The argument tcpAllowOthers of createTcpServer can be removed if it is accessed only from the local host.

Then, change the automatically created main class as follows.

piyoApplication.java


package hoge.fuga;

import hoge.fuga.resources.H2ServerResource;
import io.dropwizard.Application;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;

public class piyoApplication extends Application<piyoConfiguration> {

    public static void main(final String[] args) throws Exception {
        new piyoApplication().run(args);
    }

    @Override
    public String getName() {
        return "piyo";
    }

    @Override
    public void initialize(final Bootstrap<piyoConfiguration> bootstrap) {
        // TODO: application initialization
    }

    @Override
    public void run(final piyoConfiguration configuration,
                    final Environment environment) {
        // TODO: implement application
    	H2ServerResource h2serverResource = new H2ServerResource();
    	environment.lifecycle().manage(h2serverResource);
    }

}

Register an instance of H2ServerResource at run. Looking at the original text, it means that you should use it for DB connection or thread pool.

Managed Objects

Most applications involve objects which need to be started and stopped: thread pools, database connections, etc. Dropwizard provides the Managed interface for this. You can either have the class in question implement the #start() and #stop() methods, or write a wrapper class which does so. Adding a Managed instance to your application’s Environment ties that object’s lifecycle to that of the application’s HTTP server. Before the server starts, the #start() method is called. After the server has stopped (and after its graceful shutdown period) the #stop() method is called.

That's all for the program. From here, we will start compiling.

Compile and run the program

WS000016.JPG

Right-click on the project and select Run ® Run Configuration.

When the Run Configuration window opens, select Maven Build and create a new one.

WS000017.JPG

Select the project folder in the base directory, enter package or clean package in the goal, apply and run. Then it will be built.

This will launch the H2 server. Dropwizard Tools is a useful plugin for running Dropwizard.

This plugin will automatically fill in the necessary arguments such as server and config.yml when running dropwizard. Once installed, open the debug configuration.

WS000018.JPG

When the debug configuration window opens, select the main class.

WS000019.JPG

And run. In this state, you can access the DB access (20020) and open the Web console (20010) by accessing the following ports entered earlier, respectively.

tcpServer = Server.createTcpServer("-tcpAllowOthers", "-tcpPort", "20020");
webConsole = Server.createWebServer("-webPort", "20010");

Let's access localhost: 20010. The web console opens. Let's put localhost: 20020 in JDBC URL :. If you access from the console with the following URL, h2db.mv.db will be created in the project folder. jdbc:h2:tcp://localhost:20020/./h2db

WS000020.JPG

Go back to Eclipse and open it from DB Viewer. Open the DBViewer window and press Register.

WS000021.JPG

When the database definition window opens, select the h2 driver from Add File. Look for it in the maven local repository folder. In my environment it was in the following folder. %USERPROFILE%\.m2\repository\com\h2database\h2\1.4.196

WS000022.JPG

Then press Next. In the connection information settings, the connection string was entered on the Web console earlier, jdbc:h2:tcp://localhost:20020/./h2db Enter the. Set the connection user to "sa" and empty the connection schema. Then make sure you can make a test connection.

WS000024.JPG

With the above, it is from the place where you create a project with Dropwizard to the place where you can connect with DB Viewer.

Recommended Posts

Until you can use the H2 database in server mode with Dropwizard using Eclipse and connect with DB Viewer.
Coexistence of Flyway in the embedded database (h2) of the development environment and the release database (SQL Server) with Spring Boot
With Tomcat you can use placeholders ($ {...}) in web.xml
Until you start development with Spring Boot in eclipse 1
Until you start development with Spring Boot in eclipse 2
Until you build a project described in scala with Maven and execute it with the scala command.
In Redmine you can get the project with Project.find (<identifier>)
Install Ubuntu Server 20.04 in VirtualBox on Mac and connect with SSH
[Rails + Webpacker] I want to use images of assets! Until you can view the image in Vue.js