介绍和使用Berkeley DB Java Edition(嵌入式数据库)

[url]http://www.iteye.com/topic/227040[/url]

一、 简介

Berkeley DB Java Edition (JE)是一个完全用JAVA写的,它适合于管理海量的,简单的数据。

能够高效率的处理1到1百万条记录,制约JE数据库的往往是硬件系统,而不是JE本身。

多线程支持,JE使用超时的方式来处理线程间的死琐问题。

Database都采用简单的key/value对应的形式。

事务支持。
允许创建二级库。这样我们就可以方便的使用一级key,二级key来访问我们的数据。

支持RAM缓冲,这样就能减少频繁的IO操作。

支持日志。

数据备份和恢复。

游标支持。

二、 获取JE

JE下载地址:

http://www.oracle.com/technology/software/products/berkeley-db/je/index.html

解开包后 把JE_HOME/lib/je-<version>.jar 中的jar文件添加到你的环境变量中就可以使用je了。

相关帮助文档可以参考 JE_HOME/docs/index.html

源代码见JE_HOME/src/*.*

三、 JE常见的异常

DatabaseNotFoundException 当没有找到指定的数据库的时候会返回这个异常

DeadlockException 线程间死锁异常

RunRecoveryException 回收异常,当发生此异常的时候,你必须得重新打开环境变量。

四、 关于日志文件必须了解的六项

JE的日志文件跟其他的数据库的日志文件不太一样,跟C版的DBD也是有区别的

JE的日志文件只能APPEND,第一个日志文件名是 00000000.jdb,当他增长到一定大小的时候(默认是10M),开始写第二个日志文件00000001.jdb,已此类推。

跟C版本有所不同,JE的数据日志和事务日志是放在一起的,而不是分开放的。

JE cleaner负责清扫没用到的磁盘空间,删除后,或者更新后新的记录会追加进来,而原有的记录空间就不在使用了,cleaner负责清理不用的空间。

清理并不是立即进行的,当你关闭你的数据库环境后,通过调用一个cleaner方法来清理。

清理也不是只动执行的,需要你自己手动调用cleaner 方法来定时清理的。

日志文件的删除仅发生在检查点之后。cleaner准备出哪些log 文件需要被删除,当检查点过后,删掉一些不在被使用的文件。每写20M的日志文件就执行一次检查点,默认下。

JE的英文文档:http://www.oracle.com/technology/documentation/berkeley-db/je/GettingStartedGuide/index.html

JE的中文文档(不全):http://fesir.itpub.net/post/4728/253789
http://fesir.itpub.net/post/4728/253789

上面大概介绍一下,接下来是我在工作中写的一个例子(仅供参考):

Java代码 复制代码

1. package src.util;
2.
3. import java.io.File;
4. import java.util.List;
5.
6. import junit.framework.TestCase;
7. import src.com.bizjato.b2b.entity.Suppliers;
8.
9. import com.sleepycat.bind.EntryBinding;
10. import com.sleepycat.bind.serial.SerialBinding;
11. import com.sleepycat.bind.serial.StoredClassCatalog;
12. import com.sleepycat.je.Cursor;
13. import com.sleepycat.je.CursorConfig;
14. import com.sleepycat.je.Database;
15. import com.sleepycat.je.DatabaseConfig;
16. import com.sleepycat.je.DatabaseEntry;
17. import com.sleepycat.je.DatabaseException;
18. import com.sleepycat.je.Environment;
19. import com.sleepycat.je.EnvironmentConfig;
20. import com.sleepycat.je.EnvironmentMutableConfig;
21. import com.sleepycat.je.LockMode;
22. import com.sleepycat.je.OperationStatus;
23.
24. /**
25. * 嵌入式数据库
26. *
27. * @author zhangqinjian
28. *
29. */
30. public class Jedtion extends TestCase {
31.
32. private static final String RESOURCE = ".//src//util//importData";
33. private Environment env;
34.
35. private Database db, classDB;
36.
37. private StoredClassCatalog classCatalog;
38. /**
39. * 初始化,设置数据库的环境和创建数据库
40. */
41. public void setUp() throws Exception {
42.
43. env = new Environment(new File(".//src//util//importData"), null);
44. EnvironmentMutableConfig envMutableConfig = new EnvironmentMutableConfig();
45. envMutableConfig.setTxnNoSync(true);
46. env.setMutableConfig(envMutableConfig);
47.
48. DatabaseConfig dbConfig = new DatabaseConfig();
49. dbConfig.setAllowCreate(true);
50. db = env.openDatabase(null, "myDB", dbConfig);
51. classDB = env.openDatabase(null, "classDB", dbConfig);
52. classCatalog = new StoredClassCatalog(classDB);
53. }
54. // test: put key-value
55. public void testPut(String key, Suppliers supp) throws Exception {
56. EntryBinding dataBinding = new SerialBinding(classCatalog,
57. Suppliers.class);
58. DatabaseEntry keyEntry = new DatabaseEntry(key.getBytes("UTF-8"));
59. DatabaseEntry dataEntry = new DatabaseEntry();
60. dataBinding.objectToEntry(supp, dataEntry);
61. db.put(null, keyEntry, dataEntry);
62. }
63. // test: get
64. public void testGet() throws Exception {
65. EnvironmentConfig envConfig = new EnvironmentConfig();
66. envConfig.setAllowCreate(true);
67. // envConfig.setReadOnly(true);
68. env = new Environment(new File(".//src//util//importData"), envConfig);
69. List myDbNames = env.getDatabaseNames();
70. System.out.println("Database size: " + myDbNames.size());
71. for (int i = 0; i < myDbNames.size(); i++) {
72. System.out.println("Database Name: " + (String) myDbNames.get(i));
73. }
74.
75. DatabaseConfig dbConfig = new DatabaseConfig();
76. dbConfig.setAllowCreate(true);
77. // dbConfig.setReadOnly(true);
78. db = env.openDatabase(null, "myDB", dbConfig);
79. classDB = env.openDatabase(null, "classDB", dbConfig);
80. classCatalog = new StoredClassCatalog(classDB);
81.
82. System.out.println("Db: " + db.count());
83.
84. EntryBinding dataBinding = new SerialBinding(classCatalog,
85. Suppliers.class);
86.
87. DatabaseEntry keyEntry = new DatabaseEntry("key".getBytes("UTF-8"));
88. DatabaseEntry dataEntry = new DatabaseEntry();
89. db.get(null, keyEntry, dataEntry, LockMode.DEFAULT);
90. Suppliers p = (Suppliers) dataBinding.entryToObject(dataEntry);
91. System.out.println(p.getCategory());
92. }
93.
94. /**
95. * test: read database
96. * @throws Exception
97. */
98. public void testStore() throws Exception {
99.
100. EnvironmentConfig envConfig = new EnvironmentConfig();
101. envConfig.setAllowCreate(true);
102. env = new Environment(new File(".//src//util//importData"), envConfig);
103.
104. DatabaseConfig dbConfig = new DatabaseConfig();
105. dbConfig.setAllowCreate(true);
106. db = env.openDatabase(null, "myDB", dbConfig);
107. classDB = env.openDatabase(null, "classDB", dbConfig);
108. classCatalog = new StoredClassCatalog(classDB);
109.
110. EntryBinding dataBinding = new SerialBinding(classCatalog,Suppliers.class);
111.
112. Cursor cursor = null;
113. CursorConfig config = new CursorConfig();
114. config.setDirtyRead(true);
115. cursor = db.openCursor(null, config); // open cursor
116.
117. try {
118. // Database and environment open omitted for brevity
119. // Open the cursor. cursor = myDatabase.openCursor(null, null);
120. // Cursors need a pair of DatabaseEntry objects to operate. These hold
121. // the key and data found at any given position in the database.
122. DatabaseEntry foundKey = new DatabaseEntry();
123. DatabaseEntry foundData = new DatabaseEntry();
124. // To iterate, just call getNext() until the last database record has been
125. // read. All cursor operations return an OperationStatus, so just read
126. // until we no longer see OperationStatus.SUCCESS
127. while (cursor.getNext(foundKey, foundData, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
128. // getData() on the DatabaseEntry objects returns the byte array
129. // held by that object. We use this to get a String value. If the
130. // DatabaseEntry held a byte array representation of some other data
131. // type (such as a complex object) then this operation would look
132. // considerably different.
133. String keyString = new String(foundKey.getData());
134.
135. Suppliers supp = (Suppliers)dataBinding.entryToObject(foundData);
136. System.out.println("Key - Data : " + keyString + " - "
137. + supp.getCompanyName() + "");
138. }
139. } catch (DatabaseException de) {
140. System.err.println("Error accessing database." + de);
141. } finally {
142. // Cursors must be closed.
143. cursor.close();
144. db.close();
145. classDB.close();
146. env.cleanLog();
147. env.close();
148. }
149.
150. }
151.
152. public void tearDown() throws Exception {
153. db.close();
154. classDB.close();
155. env.cleanLog();
156. env.close();
157. }
158.
159. public static void main(String[] args) {
160. Jedtion t = new Jedtion();
161. try {
162. // t.setUp();
163. // Suppliers supp = new Suppliers();
164. // supp.setCategory("fivefive");
165. // t.testPut("5",supp);
166. // t.testGet();
167. // t.testStore();
168. // t.tearDown();
169. } catch (Exception e) {
170. // TODO Auto-generated catch block
171. e.printStackTrace();
172. }
173.
174. }
175. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值