• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-WNDR4500v2-V1.0.0.60_1.0.38/ap/gpl/timemachine/db-4.7.25.NC/examples_java/src/persist/txn/
1// File TxnGuideDPL.java
2
3package persist.txn;
4
5import com.sleepycat.db.DatabaseConfig;
6import com.sleepycat.db.DatabaseException;
7import com.sleepycat.db.DatabaseType;
8import com.sleepycat.db.LockDetectMode;
9
10import com.sleepycat.db.Environment;
11import com.sleepycat.db.EnvironmentConfig;
12
13import com.sleepycat.persist.EntityStore;
14import com.sleepycat.persist.StoreConfig;
15
16import java.io.File;
17import java.io.FileNotFoundException;
18
19public class TxnGuideDPL {
20
21    private static String myEnvPath = "./";
22    private static String storeName = "exampleStore";
23
24    // Handles
25    private static EntityStore myStore = null;
26    private static Environment myEnv = null;
27
28    private static final int NUMTHREADS = 5;
29
30    private static void usage() {
31        System.out.println("TxnGuideDPL [-h <env directory>]");
32        System.exit(-1);
33    }
34
35    public static void main(String args[]) {
36        try {
37            // Parse the arguments list
38            parseArgs(args);
39            // Open the environment and store
40            openEnv();
41
42            // Start the threads
43            StoreWriter[] threadArray;
44            threadArray = new StoreWriter[NUMTHREADS];
45            for (int i = 0; i < NUMTHREADS; i++) {
46                threadArray[i] = new StoreWriter(myEnv, myStore);
47                threadArray[i].start();
48            }
49
50            for (int i = 0; i < NUMTHREADS; i++) {
51                threadArray[i].join();
52            }
53        } catch (Exception e) {
54            System.err.println("TxnGuideDPL: " + e.toString());
55            e.printStackTrace();
56        } finally {
57            closeEnv();
58        }
59        System.out.println("All done.");
60    }
61
62
63    private static void openEnv() throws DatabaseException {
64        System.out.println("opening env and store");
65
66        // Set up the environment.
67        EnvironmentConfig myEnvConfig = new EnvironmentConfig();
68        myEnvConfig.setAllowCreate(true);
69        myEnvConfig.setInitializeCache(true);
70        myEnvConfig.setInitializeLocking(true);
71        myEnvConfig.setInitializeLogging(true);
72        myEnvConfig.setRunRecovery(true);
73        myEnvConfig.setTransactional(true);
74        // EnvironmentConfig.setThreaded(true) is the default behavior
75        // in Java, so we do not have to do anything to cause the
76        // environment handle to be free-threaded.
77
78        // Indicate that we want db to internally perform deadlock
79        // detection. Also indicate that the transaction that has
80        // performed the least amount of write activity to
81        // receive the deadlock notification, if any.
82        myEnvConfig.setLockDetectMode(LockDetectMode.MINWRITE);
83
84        // Set up the entity store
85        StoreConfig myStoreConfig = new StoreConfig();
86        myStoreConfig.setAllowCreate(true);
87        myStoreConfig.setTransactional(true);
88
89        // Need a DatabaseConfig object so as to set uncommitted read
90        // support.
91        DatabaseConfig myDbConfig = new DatabaseConfig();
92        myDbConfig.setType(DatabaseType.BTREE);
93        myDbConfig.setAllowCreate(true);
94        myDbConfig.setTransactional(true);
95        myDbConfig.setReadUncommitted(true);
96
97        try {
98            // Open the environment
99            myEnv = new Environment(new File(myEnvPath),    // Env home
100                                    myEnvConfig);
101
102            // Open the store
103            myStore = new EntityStore(myEnv, storeName, myStoreConfig);
104
105            // Set the DatabaseConfig object, so that the underlying
106            // database is configured for uncommitted reads.
107            myStore.setPrimaryConfig(PayloadDataEntity.class, myDbConfig);
108        } catch (FileNotFoundException fnfe) {
109            System.err.println("openEnv: " + fnfe.toString());
110            System.exit(-1);
111        }
112    }
113
114    private static void closeEnv() {
115        System.out.println("Closing env and store");
116        if (myStore != null ) {
117            try {
118                myStore.close();
119            } catch (DatabaseException e) {
120                System.err.println("closeEnv: myStore: " +
121                    e.toString());
122                e.printStackTrace();
123            }
124        }
125
126        if (myEnv != null ) {
127            try {
128                myEnv.close();
129            } catch (DatabaseException e) {
130                System.err.println("closeEnv: " + e.toString());
131                e.printStackTrace();
132            }
133        }
134    }
135
136    private TxnGuideDPL() {}
137
138    private static void parseArgs(String args[]) {
139        int nArgs = args.length;
140        for(int i = 0; i < args.length; ++i) {
141            if (args[i].startsWith("-")) {
142                switch(args[i].charAt(1)) {
143                    case 'h':
144                        if (i < nArgs - 1) {
145                            myEnvPath = new String(args[++i]);
146                        }
147                    break;
148                    default:
149                        usage();
150                }
151            }
152        }
153    }
154}
155