KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > persist > gettingStarted > MyDbEnv


1 package persist.gettingStarted;
2
3 import java.io.File JavaDoc;
4
5 import com.sleepycat.je.DatabaseException;
6 import com.sleepycat.je.Environment;
7 import com.sleepycat.je.EnvironmentConfig;
8
9 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     // The setup() method opens the environment and store
21
// for us.
22
public void setup(File JavaDoc envHome, boolean readOnly)
23         throws DatabaseException {
24
25         EnvironmentConfig myEnvConfig = new EnvironmentConfig();
26         StoreConfig storeConfig = new StoreConfig();
27
28         myEnvConfig.setReadOnly(readOnly);
29         storeConfig.setReadOnly(readOnly);
30
31         // If the environment is opened for write, then we want to be
32
// able to create the environment and entity store if
33
// they do not exist.
34
myEnvConfig.setAllowCreate(!readOnly);
35         storeConfig.setAllowCreate(!readOnly);
36
37         // Allow transactions if we are writing to the store.
38
myEnvConfig.setTransactional(!readOnly);
39         storeConfig.setTransactional(!readOnly);
40
41         // Open the environment and entity store
42
myEnv = new Environment(envHome, myEnvConfig);
43         store = new EntityStore(myEnv, "EntityStore", storeConfig);
44
45     }
46
47     // Return a handle to the entity store
48
public EntityStore getEntityStore() {
49         return store;
50     }
51
52     // Return a handle to the environment
53
public Environment getEnv() {
54         return myEnv;
55     }
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 }
82
83
Popular Tags