1// File TxnGuide.java
2
3package db.txn;
4
5import com.sleepycat.bind.serial.StoredClassCatalog;
6
7import com.sleepycat.db.Database;
8import com.sleepycat.db.DatabaseConfig;
9import com.sleepycat.db.DatabaseException;
10import com.sleepycat.db.DatabaseType;
11import com.sleepycat.db.LockDetectMode;
12
13import com.sleepycat.db.Environment;
14import com.sleepycat.db.EnvironmentConfig;
15
16import java.io.File;
17import java.io.FileNotFoundException;
18
19public class TxnGuide {
20
21    private static String myEnvPath = "./";
22    private static String dbName = "mydb.db";
23    private static String cdbName = "myclassdb.db";
24
25    // DB handles
26    private static Database myDb = null;
27    private static Database myClassDb = null;
28    private static Environment myEnv = null;
29
30    private static final int NUMTHREADS = 5;
31
32    private static void usage() {
33        System.out.println("TxnGuide [-h <env directory>]");
34        System.exit(-1);
35    }
36
37    public static void main(String args[]) {
38        try {
39            // Parse the arguments list
40            parseArgs(args);
41            // Open the environment and databases
42            openEnv();
43            // Get our class catalog (used to serialize objects)
44            StoredClassCatalog classCatalog =
45                new StoredClassCatalog(myClassDb);
46
47            // Start the threads
48            DBWriter[] threadArray;
49            threadArray = new DBWriter[NUMTHREADS];
50            for (int i = 0; i < NUMTHREADS; i++) {
51                threadArray[i] = new DBWriter(myEnv, myDb, classCatalog);
52                threadArray[i].start();
53            }
54
55            for (int i = 0; i < NUMTHREADS; i++) {
56                threadArray[i].join();
57            }
58        } catch (Exception e) {
59            System.err.println("TxnGuide: " + e.toString());
60            e.printStackTrace();
61        } finally {
62            closeEnv();
63        }
64        System.out.println("All done.");
65    }
66
67
68    private static void openEnv() throws DatabaseException {
69        System.out.println("opening env");
70
71        // Set up the environment.
72        EnvironmentConfig myEnvConfig = new EnvironmentConfig();
73        myEnvConfig.setAllowCreate(true);
74        myEnvConfig.setInitializeCache(true);
75        myEnvConfig.setInitializeLocking(true);
76        myEnvConfig.setInitializeLogging(true);
77        myEnvConfig.setRunRecovery(true);
78        myEnvConfig.setTransactional(true);
79        // EnvironmentConfig.setThreaded(true) is the default behavior
80        // in Java, so we do not have to do anything to cause the
81        // environment handle to be free-threaded.
82
83        // Indicate that we want db to internally perform deadlock
84        // detection. Also indicate that the transaction that has
85        // performed the least amount of write activity to
86        // receive the deadlock notification, if any.
87        myEnvConfig.setLockDetectMode(LockDetectMode.MINWRITE);
88
89        // Set up the database
90        DatabaseConfig myDbConfig = new DatabaseConfig();
91        myDbConfig.setType(DatabaseType.BTREE);
92        myDbConfig.setAllowCreate(true);
93        myDbConfig.setTransactional(true);
94        myDbConfig.setSortedDuplicates(true);
95        myDbConfig.setReadUncommitted(true);
96        // no DatabaseConfig.setThreaded() method available.
97        // db handles in java are free-threaded so long as the
98        // env is also free-threaded.
99
100        try {
101            // Open the environment
102            myEnv = new Environment(new File(myEnvPath),    // Env home
103                                    myEnvConfig);
104
105            // Open the database. Do not provide a txn handle. This open
106            // is autocommitted because DatabaseConfig.setTransactional()
107            // is true.
108            myDb = myEnv.openDatabase(null,     // txn handle
109                                      dbName,   // Database file name
110                                      null,     // Database name
111                                      myDbConfig);
112
113            // Used by the bind API for serializing objects
114            // Class database must not support duplicates
115            myDbConfig.setSortedDuplicates(false);
116            myClassDb = myEnv.openDatabase(null,     // txn handle
117                                           cdbName,  // Database file name
118                                           null,     // Database name,
119                                           myDbConfig);
120        } catch (FileNotFoundException fnfe) {
121            System.err.println("openEnv: " + fnfe.toString());
122            System.exit(-1);
123        }
124    }
125
126    private static void closeEnv() {
127        System.out.println("Closing env and databases");
128        if (myDb != null ) {
129            try {
130                myDb.close();
131            } catch (DatabaseException e) {
132                System.err.println("closeEnv: myDb: " +
133                    e.toString());
134                e.printStackTrace();
135            }
136        }
137
138        if (myClassDb != null ) {
139            try {
140                myClassDb.close();
141            } catch (DatabaseException e) {
142                System.err.println("closeEnv: myClassDb: " +
143                    e.toString());
144                e.printStackTrace();
145            }
146        }
147
148        if (myEnv != null ) {
149            try {
150                myEnv.close();
151            } catch (DatabaseException e) {
152                System.err.println("closeEnv: " + e.toString());
153                e.printStackTrace();
154            }
155        }
156    }
157
158    private TxnGuide() {}
159
160    private static void parseArgs(String args[]) {
161        for(int i = 0; i < args.length; ++i) {
162            if (args[i].startsWith("-")) {
163                switch(args[i].charAt(1)) {
164                    case 'h':
165                        myEnvPath = new String(args[++i]);
166                        break;
167                    default:
168                        usage();
169                }
170            }
171        }
172    }
173}
174