Professional Documents
Culture Documents
of Contents
1. Introduction
2. About this book
3. How to start
4. WebServer
H2 Database Source book
This is a book about h2 database source code.
How to start
When reading code, how can we find where to start? How can we find the class with the main method in lots of java codes?
Actually, it's very easy if you know how to start the program you write. For h2 database, we can easily find how to start it on
its website. We can just run 'java -jar h2-*.jar' to start. But how dose the jar command know which class contains the main
method. It's in the jar file's menifest file. You can unzip the jar file, and you can also find a line with 'Mainclass', which
indicate the entry of you program.
And now, we know where to start. Let's begin with the main class h2.org.tools.Console .
Main method is where our program starts. So we can just follow it and find out how h2 database was implemented.
In the main method we initialized an instance of Console, and then run it's runtTool method with args.
As the comment tells us, this tool starts the H2 Console (web-) server, as well as the TCP and PG server.
First part of the code parses parameters from command line. This parameters contains:
webStart
This option starts a standalone HTTP server that implements the H2 Console application.
if (webStart) {
try {
web = Server.createWebServer(args);
web.setShutdownHandler(this);
web.start();
if (printStatus) {
out.println(web.getStatus());
}
webRunning = true;
} catch (SQLException e) {
printProblem(e, web);
startException = e;
}
}
tcpStart
if (tcpStart) {
try {
tcp = Server.createTcpServer(args);
tcp.start();
if (printStatus) {
out.println(tcp.getStatus());
}
tcp.setShutdownHandler(this);
} catch (SQLException e) {
printProblem(e, tcp);
if (startException == null) {
startException = e;
}
}
}
pgStart
if (pgStart) {
try {
pg = Server.createPgServer(args);
pg.start();
if (printStatus) {
out.println(pg.getStatus());
}
} catch (SQLException e) {
printProblem(e, pg);
if (startException == null) {
startException = e;
}
}
}
Exception
if (startException != null) {
shutdown();
throw startException;
}
WebServer
First let's start with where we started about webserver in last chapter. Let's see following codes:
1 if (webStart) {
2 try {
3 web = Server.createWebServer(args);
4 web.setShutdownHandler(this);
5 web.start();
6 if (printStatus) {
7 out.println(web.getStatus());
8 }
9 webRunning = true;
10 } catch (SQLException e) {
11 printProblem(e, web);
12 startException = e;
13 }
14}
createWebServer
Now, let's see how dose the method createWebServer create the webserver for us.
Now, you may konw how this works.We use service to provide service for our server, and the server deal with our clients.