先来无事,发现之前下载的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 stati