标签: H2database
在工作中需要做大数据缓存方案,浏览到内存数据库想对数据库实现进行了解,故选择H2做为样本进行源码阅读,环境如下:
- 操作系统:win7 64位系
- H2database源码版本:1.4.196-SNAPSHOT
- Eclipse版本:Neon.3 Release (4.6.3)
- JDK版本:1.8
此份源码已经过修改,如需查看原版代码请在github上查找下面阐述运行源码做的修改
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.196-SNAPSHOT</version>
<packaging>jar</packaging>
<name>H2 Database Engine</name>
<url>http://www.h2database.com</url>
<description>H2 Database Engine</description>
<licenses>
<license>
<name>MPL 2.0 or EPL 1.0</name>
<url>http://h2database.com/html/license.html</url>
<distribution>repo</distribution>
</license>
</licenses>
<scm>
<connection>scm:git:https://github.com/h2database/h2database</connection>
<url>https://github.com/h2database/h2database</url>
</scm>
<developers>
<developer>
<id>thomas.tom.mueller</id>
<name>Thomas Mueller</name>
<email>thomas.tom.mueller at gmail dot com</email>
</developer>
</developers>
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<derby.version>10.10.1.1</derby.version>
<osgi.version>4.2.0</osgi.version>
<slf4j.version>1.6.0</slf4j.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- START COMPILE DEPENDENCIES !-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version>3.6.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<version>${osgi.version}</version>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.enterprise</artifactId>
<version>${osgi.version}</version>
</dependency>
<dependency>
<groupId>com.vividsolutions</groupId>
<artifactId>jts-core</artifactId>
<version>1.14.0</version>
</dependency>
<!-- END COMPILE DEPENDENCIES !-->
<!-- START TEST DEPENDENCIES !-->
<!-- 去除测试需要的jar,保留Junit
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4j.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.3.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>${derby.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyclient</artifactId>
<version>${derby.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbynet</artifactId>
<version>${derby.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4.1209.jre6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
<scope>test</scope>
</dependency>
-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- END TEST DEPENDENCIES !-->
<!-- JDK dependencies -->
<!-- JDK依赖 -->
<!-- <dependency> -->
<!-- <groupId>com.sun</groupId> -->
<!-- <artifactId>tools</artifactId> -->
<!-- <version>1.8</version> -->
<!-- <scope>system</scope> -->
<!-- <systemPath>${tools.jar}</systemPath> -->
<!-- </dependency> -->
</dependencies>
<!-- The test code creates proxy files using javac or tools.jar. Through maven we need to tell it
where to possibly find tools.jar and annoyingly its called classes.jar on OSX -->
<profiles>
<profile>
<id>default-tools.jar</id>
<activation>
<file>
<exists>${java.home}/../lib/tools.jar</exists>
</file>
</activation>
<properties>
<tools.jar>${java.home}/../lib/tools.jar</tools.jar>
</properties>
</profile>
<profile>
<id>default-tools.jar-mac</id>
<activation>
<file>
<exists>${java.home}/../Classes/classes.jar</exists>
</file>
</activation>
<properties>
<tools.jar>${java.home}/../Classes/classes.jar</tools.jar>
</properties>
</profile>
</profiles>
<build>
<sourceDirectory>src/main</sourceDirectory>
<testSourceDirectory>src/test</testSourceDirectory>
<!-- Resources do not follow maven project layout. We need to manually copy them -->
<resources>
<resource>
<directory>src/main</directory>
<includes>
<include>**/*.prop</include>
<include>**/*.png</include>
<include>**/*.jsp</include>
<include>**/*.ico</include>
<include>**/*.gif</include>
<include>**/*.css</include>
<include>org/h2/res/help.csv</include>
<include>org/h2/res/javadoc.properties</include>
<include>org/h2/server/pg/pg_catalog.sql</include>
</includes>
</resource>
</resources>
<testResources>
<testResource>
<directory>src/test</directory>
<includes>
<include>org/h2/test/testSimple.in.txt</include>
<include>org/h2/test/testScript.sql</include>
<include>org/h2/samples/newsfeed.sql</include>
<include>org/h2/samples/optimizations.sql</include>
</includes>
</testResource>
</testResources>
<plugins>
<!-- Add tools folder to test sources but consider moving them to src/test -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<!-- 使用3.0.0插件 -->
<!--<version>1.10</version> -->
<version>3.0.0</version>
<executions>
<execution>
<phase>generate-test-sources</phase>
<goals><goal>add-test-source</goal></goals>
<configuration>
<sources>
<source>src/tools</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<!-- Maven requires at least JRE 1.7 but we want to build with JDK 1.6 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-toolchains-plugin</artifactId>
<version>1.1</version>
<!-- 此插件有jdk的依赖,注释如下配置,使用jdk1.8 -->
<!-- <executions> -->
<!-- <execution> -->
<!-- <goals> -->
<!-- <goal>toolchain</goal> -->
<!-- </goals> -->
<!-- </execution> -->
<!-- </executions> -->
<configuration>
<toolchains>
<jdk>
<version>1.8</version>
</jdk>
</toolchains>
</configuration>
</plugin>
<!-- Make sure we are not using anything outside JDK 1.6 -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>animal-sniffer-maven-plugin</artifactId>
<version>1.15</version>
<executions>
<execution>
<id>check-java-api</id>
<phase>test</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<!-- dk的依赖,使用jdk1.8 -->
<!-- <configuration> -->
<!-- <signature> -->
<!-- <groupId>org.codehaus.mojo.signature</groupId> -->
<!-- <artifactId>java16</artifactId> -->
<!-- <version>1.1</version> -->
<!-- </signature> -->
<!-- </configuration> -->
</plugin>
<!-- Disable surefire since we don't use Junit -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<includes>
<include>TestAllJunit.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</project>
注意:修改集中在中文注释上
测试代码:
@Test
public void test_h2_mem() throws SQLException
{
JdbcConnectionPool ds = JdbcConnectionPool.create("jdbc:h2:mem:test2", "sa", "");
// JdbcConnectionPool ds = JdbcConnectionPool.create("jdbc:h2:E:/test2", "sa", "");
ds.setMaxConnections(1000);
Connection conn = ds.getConnection() ;
Statement stat = conn.createStatement();
// insert data (ddl)
String createTableSql = "";
createTableSql += "create table userInfo( ";
createTableSql += " id int(4) not null primary key auto_increment, ";
createTableSql += " name char(20) not null, ";
createTableSql += " sex int(4) not null default '0', ";
createTableSql += " degree double ";
createTableSql += "); ";
stat.execute( createTableSql );
// stat.execute("CREATE TABLE TEST_TABLE(NAME VARCHAR)");
stat.execute("CREATE INDEX IDXNAME ON userInfo(NAME)");
stat.execute("INSERT INTO userInfo(name,sex,degree) VALUES('Hello World_0','0',23)");
stat.execute("INSERT INTO userInfo(name,sex,degree) VALUES('Hello World_1','1',23)");
stat.execute("INSERT INTO userInfo(name,sex,degree) VALUES('Hello World_2','2',23)");
stat.execute("INSERT INTO userInfo(name,sex,degree) VALUES('Hello World_3','3',23)");
// use data
System.err.println(" select begin ");
ResultSet result = stat.executeQuery("select id,name,sex,degree from userInfo where name like '%Hello World%'");
System.err.println(" select end ");
while (result.next()) {
System.out.println( result.getInt("id")+"|" + result.getString("name")+"|" + result.getInt("sex")+ "|" + result.getDouble("degree"));
}
result.close();
stat.close();
conn.close();
}
运行结果:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
select begin
测试增加
测试增加
测试增加
测试增加
测试增加
select end
1|Hello World_0|0|23.0
2|Hello World_1|1|23.0
3|Hello World_2|2|23.0
4|Hello World_3|3|23.0