標籤:berkeleydb java 記憶體資料庫
關於BerkeleyDB的有點和好處,列在下面
JE offers the following major features:
Large database support. JE databases efficiently scale from one to millions of records. The size of your JE databases are likely to be limited more by hardware resources than by any limits imposed upon you by JE.
Databases are described in Databases.
Database environments. Database environments provide a unit of encapsulation and management for one or more databases. Environments are also the unit of management for internal resources such as the in-memory cache and the background threads. Finally, you use environments to manage concurrency and transactions. Note that all applications using JE are required to use database environments.
Database environments are described in Database Environments.
Multiple thread and process support. JE is designed for multiple threads of control. Both read and write operations can be performed by multiple threads. JE uses record-level locking for high concurrency in threaded applications. Further, JE uses timeouts for deadlock detection to help you ensure that two threads of control do not deadlock indefinitely.
Moreover, JE allows multiple processes to access the same databases. However, in this configuration JE requires that there be no more than one process allowed to write to the database. Read-only processes are guaranteed a consistent, although potentially out of date, view of the stored data as of the time that the environment is opened.
Transactions. Transactions allow you to treat one or more operations on one or more databases as a single unit of work. JE transactions offer the application developer recoverability, atomicity, and isolation for your database operations.
Note that transaction protection is optional. Transactions are described in the Berkeley DB, Java Edition Getting Started with Transaction Processing guide.
In-memory cache. The cache allows for high speed database access for both read and write operations by avoiding unnecessary disk I/O. The cache will grow on demand up to a pre-configured maximum size. To improve your application‘s performance immediately after startup time, you can preload your cache in order to avoid disk I/O for production requests of your data.
Cache management is described in Sizing the Cache.
Indexes. JE allows you to easily create and maintain secondary indices for your primary data. In this way, you can obtain rapid access to your data through the use of an alternative, or secondary, key.
How indices work is dependent upon the API you are using. If you are using the DPL, seeWorking with Indices. Otherwise, see Secondary Databases.
Log files. JE databases are stored in one or more numerically-named log files in the environment directory. The log files are write-once and are portable across platforms with different endian-ness.
當然也你能看懂個大概,看程式,在程式中大致翻譯了一下
package bdb;import java.io.File;import com.sleepycat.je.Database;import com.sleepycat.je.DatabaseConfig;import com.sleepycat.je.DatabaseEntry;import com.sleepycat.je.DatabaseException;import com.sleepycat.je.Environment;import com.sleepycat.je.EnvironmentConfig;/** * BerkeleyDB java的簡單使用 * @author Xiaohua * */public class Test {//此資料庫的好處//大資料的支援,可以支援millions的記錄,並且更可能是硬體出現瓶頸而不是je//資料庫環境支援,資料庫環境可以一次配置多個資料庫或者一個資料庫;環境也可以管理並發和事務//多線程和多進程的支援//支援事務//支援記憶體緩衝//支援索引//日誌記錄private static String dbEnv = "D://dbEnv";public static void main(String[] args) {Environment myDbEnvironment = null;Database myDatabase = null;try {EnvironmentConfig envConfig = new EnvironmentConfig();// 配置環境變數envConfig.setAllowCreate(true);File f=new File(dbEnv);if(!f.exists()){f.mkdirs();}myDbEnvironment = new Environment(f, envConfig);} catch (DatabaseException dbe) {}try {DatabaseConfig dbConfig = new DatabaseConfig();// 開啟資料庫dbConfig.setAllowCreate(true);myDatabase = myDbEnvironment.openDatabase(null, "myDatabase",dbConfig);} catch (DatabaseException dbe2) {}//儲存資料String aKey = "key4";String aData = "data";try {DatabaseEntry theKey = new DatabaseEntry(aKey.getBytes("UTF-8"));DatabaseEntry theData = new DatabaseEntry(aData.getBytes("UTF-8"));myDatabase.put(null, theKey, theData);//myDbEnvironment.sync();System.out.println(myDatabase.count());} catch (Exception e) {}// 關閉,應該會自動認可try {if (myDatabase != null) {myDatabase.close();}if (myDbEnvironment != null) {myDbEnvironment.cleanLog(); // 在關閉環境前清理下日誌myDbEnvironment.close();}} catch (DatabaseException dbe) {}}}
BerkeleyDB java的簡單使用