01: package persist.gettingStarted;
02:
03: import java.io.File;
04:
05: import com.sleepycat.je.DatabaseException;
06: import com.sleepycat.je.Environment;
07: import com.sleepycat.je.EnvironmentConfig;
08:
09: import com.sleepycat.persist.EntityStore;
10: import com.sleepycat.persist.StoreConfig;
11:
12: public class MyDbEnv {
13:
14: private Environment myEnv;
15: private EntityStore store;
16:
17: // Our constructor does nothing
18: public MyDbEnv() {
19: }
20:
21: // The setup() method opens the environment and store
22: // for us.
23: public void setup(File envHome, boolean readOnly)
24: throws DatabaseException {
25:
26: EnvironmentConfig myEnvConfig = new EnvironmentConfig();
27: StoreConfig storeConfig = new StoreConfig();
28:
29: myEnvConfig.setReadOnly(readOnly);
30: storeConfig.setReadOnly(readOnly);
31:
32: // If the environment is opened for write, then we want to be
33: // able to create the environment and entity store if
34: // they do not exist.
35: myEnvConfig.setAllowCreate(!readOnly);
36: storeConfig.setAllowCreate(!readOnly);
37:
38: // Allow transactions if we are writing to the store.
39: myEnvConfig.setTransactional(!readOnly);
40: storeConfig.setTransactional(!readOnly);
41:
42: // Open the environment and entity store
43: myEnv = new Environment(envHome, myEnvConfig);
44: store = new EntityStore(myEnv, "EntityStore", storeConfig);
45:
46: }
47:
48: // Return a handle to the entity store
49: public EntityStore getEntityStore() {
50: return store;
51: }
52:
53: // Return a handle to the environment
54: public Environment getEnv() {
55: return myEnv;
56: }
57:
58: // Close the store and environment
59: public void close() {
60: if (store != null) {
61: try {
62: store.close();
63: } catch (DatabaseException dbe) {
64: System.err.println("Error closing store: "
65: + dbe.toString());
66: System.exit(-1);
67: }
68: }
69:
70: if (myEnv != null) {
71: try {
72: // Finally, close the store and environment.
73: myEnv.close();
74: } catch (DatabaseException dbe) {
75: System.err.println("Error closing MyDbEnv: "
76: + dbe.toString());
77: System.exit(-1);
78: }
79: }
80: }
81: }
|