00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 package db;
00011
00012 import com.sleepycat.db.*;
00013 import java.io.File;
00014 import java.io.FileNotFoundException;
00015 import java.io.OutputStream;
00016
00017
00018
00019
00020
00021
00022
00023 public class EnvExample {
00024 private static final String progname = "EnvExample";
00025 private static final File DATABASE_HOME = new File("/tmp/database");
00026
00027 private static void runApplication(Environment dbenv)
00028 throws DatabaseException {
00029
00030
00031
00032 }
00033
00034 private static void setupEnvironment(File home,
00035 String dataDir,
00036 OutputStream errs)
00037 throws DatabaseException, FileNotFoundException {
00038
00039
00040 EnvironmentConfig config = new EnvironmentConfig();
00041 config.setErrorStream(errs);
00042 config.setErrorPrefix(progname);
00043
00044
00045
00046
00047
00048 config.setCacheSize(64 * 1024);
00049
00050
00051 config.addDataDir(dataDir);
00052
00053
00054 config.setAllowCreate(true);
00055 config.setInitializeCache(true);
00056 config.setTransactional(true);
00057 config.setInitializeLocking(true);
00058
00059
00060
00061
00062
00063 Environment dbenv = new Environment(home, config);
00064
00065 try {
00066
00067 runApplication(dbenv);
00068 } finally {
00069
00070
00071 dbenv.close();
00072 }
00073 }
00074
00075 private static void teardownEnvironment(File home,
00076 String dataDir,
00077 OutputStream errs)
00078 throws DatabaseException, FileNotFoundException {
00079
00080
00081 EnvironmentConfig config = new EnvironmentConfig();
00082
00083 config.setErrorStream(errs);
00084 config.setErrorPrefix(progname);
00085 config.addDataDir(dataDir);
00086 Environment.remove(home, true, config);
00087 }
00088
00089 public static void main(String[] args) {
00090
00091
00092
00093
00094
00095
00096
00097
00098 File home = DATABASE_HOME;
00099 String dataDir = "/database/files";
00100
00101 try {
00102 System.out.println("Setup env");
00103 setupEnvironment(home, dataDir, System.err);
00104
00105 System.out.println("Teardown env");
00106 teardownEnvironment(home, dataDir, System.err);
00107 } catch (DatabaseException dbe) {
00108 System.err.println(progname + ": environment open: " + dbe.toString());
00109 System.exit (1);
00110 } catch (FileNotFoundException fnfe) {
00111 System.err.println(progname + ": unexpected open environment error " + fnfe);
00112 System.exit (1);
00113 }
00114 }
00115
00116 }