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

		}
	}
}
时间: 2024-10-27 07:50:12

BerkeleyDB java的简单使用的相关文章

使用java搭建简单的ligerui环境

最近因为工作需要,学习了ligerui框架.但是,因为在公司,我们只是作为最低层的码农,所以环境都已经搭建好了,我们这些码农平时都是直接拿到工程,然后在别人的框架上不断的ctrl + c.ctrl + v,然后修修补补.所以为了摆脱这种困境,决定自己使用简单的servlet搭建一个ligerui,然后测试下ligerui这玩意到底是怎么跑起来的. 1.下载ligerui相关组件.这个很简单,直接去www.ligerui.com即可找到. 2.使用Eclipse创建一个web工程,然后搭建下面这个

Java数据类型简单总结

Java数据类型简单总结 一:Java数据类型总的分为两大类:基本数据类型和引用数据类型. 基本数据类型:byte.short.int.long.float.double.char.boolean八种. 引用数据类型: 1:除了基本数据类型之外都是引用数据类型, 2:API类:String.File 3:自定义类:Test01...... 4:数组:int [ ].String [ ] 二:基本数据类型 1:分类 数据类型 位(bit) 取值范围 数据类型 位 取值范围 byte 8 -128~

Java对象简单实用(计算器案例)

Java对象简单实用(计算器案例) 对 Java中的对象与属性,方法的使用,简单写了个案例 1 import java.util.Scanner; 2 class Calculste 3 { 4 int a; //定义两个整数 5 int b; 6 String option; //定义接收操作符的字符串 7 public void count(){ 8 9 //对操作符进行判断 10 switch(option){ 11 case "+": 12 System.out.println

Java实现简单版SVM

最近的图像分类工作要用到latent svm,为了更加深入了解svm,自己动手实现一个简单版的. 之所以说是简单版,因为没有用到拉格朗日,对偶,核函数等等.而是用最简单的梯度下降法求解.其中的数学原理我参考了http://blog.csdn.net/lifeitengup/article/details/10951655,文中是用matlab实现的svm. 源代码和数据集下载:https://github.com/linger2012/simpleSvm 其中数据集来自于libsvm,我找了其中

java 的简单介绍

一.Java 特点     1.简单: 和C++相比较     2.面向对象     3.跨平台    由于它是先编译后解释 + JVM(屏蔽底层操作系统的差异) 二.程序的运行一般有两种:     1.编译  源文件 ------> 编译器  机器码文件       快  无法跨平台     2.解释  源文件 ------> 解释器  逐行翻译并运行   慢  可以跨平台 三.Java运行机制: 先编译,后解释     .java 源文件  -------> 编译器 .class 字

去掉有序数组中重复数字 原地 leetcode java (最简单的方法)

1.利用荷兰国旗的思路,每次记住最后一个位置,遇到一个不重复的数,放在它后面,代码很简单. Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with consta

java htmlparser 简单使用入门

请尊重本人的工作成果,转载请留言,并说明转载地址,谢谢.地址如下: http://blog.csdn.net/fukainankai/article/details/27710883 前几节中,我们利用GDI+在窗口中绘制了各种各样的图形.图像,这一节,我们将会将这些图像保存成简单图像.所谓简单图像,指的是bmp/jpg/png等图像或者单帧的gif图像.保存成多帧的gif图像稍微复杂一点,本节中暂时不做说明.保存成动态的tiff文件也比较简单,但这里也不做说明,下次有机会和gif一起介绍. 另

HDU 1316 How Many Fibs?(java,简单题,大数)

题目 /** * compareTo:根据该数值是小于.等于.或大于 val 返回 -1.0 或 1: public int compareTo(BigInteger val) 将此 BigInteger 与指定的 BigInteger 进行比较. 对于针对六个布尔比较运算符 (<, ==, >, >=, !=, <=)中的每一个运算符的各个方法,优先提供此方法. 执行这些比较的建议语句是:(x.compareTo(y) <op> 0), 其中 <op> 是

java 模拟简单搜索

Java 模拟简单搜索 实体类 package org.dennisit.entity; /** * * * @version : 1.0 * * @author : 苏若年 <a href="mailto:[email protected]">发送邮件</a> * * @since : 1.0 创建时间: 2013-4-8 下午04:51:03 * * @function: TODO * */ public class Medicine { private I