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) {

		}
	}
}


你可能感兴趣的:(java,内存数据库,BerkeleyDB)