日期:2014-05-16  浏览次数:20675 次

h2 database源码分析·(一)

先来无事,发现之前下载的h2 database源码还在source insight里放着,就打开看看。

h2 database是一个开源的,用java写的,支持jdbc连接的内存数据库,提供浏览器模式的控制台。

其源码结构(我用的版本是1.3.154)如下(进入到src目录下,src/org/h2/)

Api ??

Bnf

Command

Compress

Constant

Constraint

Engine

Expression

Fulltext

Index

Jdbc

Jdbcx

Jmx

Message

Res

Result

Schema

Security

Server

Store

Table

Tools 工具类,其中有数据库的入口Server.main()

Upgrade

Util

Value

driver.java

从main函数开始说起

?

new Server().runTool(args);

?

?调用runTool()方法,在该方法中,会首先设置一些默认启动参数,然后根据传入的args修改这些默认参数,最后按顺序启动webserver,browser,tcpserver和pgserver,典型代码:

?

if (startDefaultServers) {
            tcpStart = true;
            pgStart = true;
            webStart = true;
            browserStart = true;
}
/*-----------------------------------*/
try {
            if (webStart) {
                web = createWebServer(args);
                web.setShutdownHandler(this);
                SQLException result = null;
                try {
                    web.start();
                } catch (Exception e) {
                    result = DbException.toSQLException(e);
                }
                out.println(web.getStatus());
// start browser in any case (even if the server is already running)
// because some people don't look at the output,
// but are wondering why nothing happens
                if (browserStart) {
                    try {
                        openBrowser(web.getURL());
                    } catch (Exception e) {
                        out.println(e.getMessage());
                    }
                }
                if (result != null) {
                    throw result;
                }
            }
            if (tcpStart) {
                tcp = createTcpServer(args);
                tcp.start();
                out.println(tcp.getStatus());
                tcp.setShutdownHandler(this);
            }
            if (pgStart) {
                pg = createPgServer(args);
                pg.start();
                out.println(pg.getStatus());
            }
} catch (SQLException e) {
            stopAll();
            throw e;
}
?

下面分别来看看这3个server和1个browser:

1WebServer

?

public static Server createWebServer(String... args){
        WebServer service = new WebServer();
        Server server = new Server(service, args);
        service.setShutdownHandler(server);
        return server;
}

?2TcpServer

public static Server createTcpServer(String... args) {
        TcpServer service = new TcpServer();
        Server server = new Server(service, args);
        service.setShutdownHandler(server);
        return server;
}

?3PgServer

public static Server createPgServer(String... args){
        return new Server(new PgServer(), args);
}

?3个server都只是创建,要启动server,都需要调用start()方法,在start方法中,调用各自的service类,start各自的server,然后创建单独的线程执行运行该server,若为守护进程,还需要将其设置为守护进程:

public Server start() throws SQLException {
        try {
            started = true;
            service.start();
            Thread t = new Thread(this);
            t.setDaemon(service.isDaemon());
            String name = service.getName() + " (" + service.getURL() + ")";
            t.setName(name);
            t.start();
            for (int i = 1; i < 64; i += i) {
                wait(i);
                if (isRunning(false)) {