http://www.h2database.com/html/frame.html
最近一個桌面的程序需要使用嵌入式數(shù)據(jù)庫,選擇了很多不同的嵌入式數(shù)據(jù),最終選擇了H2,性能是一個很重要的原因,下圖是h2提供的Performance的比較圖
http://www.h2database.com/html/images/performance.png
h2是Thomas Mueller提供的一個開源的、純java實現(xiàn)的關系數(shù)據(jù)庫,在google上面有討論組h2-database@googlegroups.com,Thomas Mueller也非常熱情,回答問題非常及時。
下面說下我最終選擇h2的原因:
(1)性能、小巧
(2)同時支持網(wǎng)絡版和嵌入式版本,另外還提供了內存版
(3)有比較好的兼容性,支持相當標準的sql標準(實際上也不存在一個數(shù)據(jù)庫能夠100%符合標準)
(4)提供了非常友好的基于web的數(shù)據(jù)庫管理界面
(5)支持hibernate
支持的事務隔離級別:
Transaction Isolation
This database supports the following transaction isolation levels:
- Serializable
This is the default level.
To enable, execute the SQL statement 'SET LOCK_MODE 0'
or append ;LOCK_MODE=1 to the database URL: jdbc:h2:~/test;LOCK_MODE=1 - Read Committed
Read locks are released immediately. Higher concurrency is possible when using this level.
This is the isolation level used for many database systems.
To enable, execute the SQL statement 'SET LOCK_MODE 0'
or append ;LOCK_MODE=3 to the database URL: jdbc:h2:~/test;LOCK_MODE=3 - Read Uncommitted
This level means that transaction isolation is disabled.
To enable, execute the SQL statement 'SET LOCK_MODE 0'
or append ;LOCK_MODE=0 to the database URL: jdbc:h2:~/test;LOCK_MODE=0
When using the isolation level 'serializable', dirty reads, non-repeatable reads, and phantom reads are prohibited.
- Dirty Reads
Means a connection can read uncommitted changes made by another connection.
Possible with: read uncommitted - Non-Repeatable Reads
A connection reads a row, another connection changes a row and commits, and the first connection re-reads the same row and gets the new result.
Possible with: read uncommitted, read committed - Phantom Reads
A connection reads a set of rows using a condition, another connection inserts a row that falls in this condition and commits, then the first connection re-reads using the same condition and gets the new row.
Possible with: read uncommitted, read committed
能簡單介紹一下這三種版本嗎?
網(wǎng)絡版,類似于oracle的client/server方式,支持網(wǎng)絡訪問
單機版,不支持網(wǎng)絡方式訪問,只能以本機的進程的方式訪問數(shù)據(jù)庫
比較簡單,也不嚴謹,建議你看下h2的文檔,作為一個開源軟件,h2的文檔相對來說是很全的了。
1.數(shù)據(jù)庫連接的jdbc url
h2分嵌入模式和服務模式,根據(jù)不同的模式和情況。連接數(shù)據(jù)庫jdbc url的寫法不同
連接內嵌模式的數(shù)據(jù)庫
"jdbc:h2:file:D:/test" 連接自定目錄下的指定數(shù)據(jù)庫
"jdbc:h2:~/test" 連接默認目錄下的指定數(shù)據(jù)庫
連接內存模式的數(shù)據(jù)庫
jdbc:h2:mem:test
連接server模式的數(shù)據(jù)庫
jdbc:h2:tcp://localhost/~/test
ssl連接數(shù)據(jù)庫
jdbc:h2:ssl://localhost/~/test
連接壓縮文件內的數(shù)據(jù)庫
jdbc:h2:zip:D:/test.zip!/test
2.h2自帶的連接池
JdbcConnectionPool cp = JdbcConnectionPool.create(url,username,password);
通過連接池獲取連接
Connection conn = cp.getConnection();
note:
默認連接池是初始10個連接
3.嵌入模式數(shù)據(jù)庫管理
通過代碼啟動數(shù)據(jù)庫服務
Server server = Server.createTcpServer();
server.start();
關閉數(shù)據(jù)庫服務
server.stop();
.數(shù)據(jù)庫注銷
server.shutdown();
4.通過h2自帶的方法跟cvs文件讀寫數(shù)據(jù)
1.通過代碼讀取csv的數(shù)據(jù)
ResultSet rs = Csv.getInstance().read(fileName, null, null);
ResultSetMetaData meta = rs.getMetaData();
while (rs.next()) {
for (int i = 0; i < meta.getColumnCount(); i++) {
System.out.println(meta.getColumnLabel(i + 1) + ":"
+ rs.getString(i + 1));
}
System.out.println();
}
rs.close();
通過代碼寫csv數(shù)據(jù)
SimpleResultSet rs = new SimpleResultSet();
rs.addColumn("name", Types.VARCHAR, 255, 0);
rs.addColumn("email", Types.VARCHAR, 255, 0);
rs.addRow("陳盈", "yingchen@midea.com.cn");
rs.addRow("鄒嘉欣", "zoujx@midea.com.cn");
Csv.getInstance().write(fileName, rs, null);
2.使用h2自帶cvs函數(shù)通過sql語句實現(xiàn)
csvread()實現(xiàn)讀csv文件
例:SELECT * FROM CSVREAD('d:/test.csv');
CSVWRITE()實現(xiàn)寫csv文件
例:CALL CSVWRITE('d:/test.csv', 'SELECT * FROM TEST');
3.根據(jù)cvs文件創(chuàng)建h2數(shù)據(jù)庫表
CREATE TABLE TEST AS SELECT * FROM CSVREAD('test.csv');//csv文件數(shù)據(jù)創(chuàng)建test表 CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR(255)) AS SELECT * FROM CSVREAD('test.csv');/創(chuàng)建test表,csv文件相應的列插入到test表相應的字段
5.H2數(shù)據(jù)庫備份
1.備份sql腳本
運行
java -cp h2*.jar org.h2.tools.Script -url jdbc:h2:~/test -user sa -script test.zip -options compression zip
2.備份數(shù)據(jù)庫文件
方法1:進入h2 shell 命令行模式
java -cp h2*.jar org.h2.tools.Shell
方法2:在shell命令行環(huán)境下執(zhí)行備份命令
BACKUP TO 'backup.zip'