Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

TxnGuide.java

00001 // File TxnGuide.java
00002 
00003 package db.txn;
00004 
00005 import com.sleepycat.bind.serial.StoredClassCatalog;
00006 
00007 import com.sleepycat.db.Database;
00008 import com.sleepycat.db.DatabaseConfig;
00009 import com.sleepycat.db.DatabaseException;
00010 import com.sleepycat.db.DatabaseType;
00011 import com.sleepycat.db.LockDetectMode;
00012 
00013 import com.sleepycat.db.Environment;
00014 import com.sleepycat.db.EnvironmentConfig;
00015 
00016 import java.io.File;
00017 import java.io.FileNotFoundException;
00018 
00019 public class TxnGuide {
00020 
00021     private static String myEnvPath = "./";
00022     private static String dbName = "mydb.db";
00023     private static String cdbName = "myclassdb.db";
00024 
00025     // DB handles
00026     private static Database myDb = null;
00027     private static Database myClassDb = null;
00028     private static Environment myEnv = null;
00029 
00030     private static final int NUMTHREADS = 5;
00031 
00032     private static void usage() {
00033         System.out.println("TxnGuide [-h <env directory>]");
00034         System.exit(-1);
00035     }
00036 
00037     public static void main(String args[]) {
00038         try {
00039             // Parse the arguments list
00040             parseArgs(args);
00041             // Open the environment and databases
00042             openEnv();
00043             // Get our class catalog (used to serialize objects)
00044             StoredClassCatalog classCatalog =
00045                 new StoredClassCatalog(myClassDb);
00046 
00047             // Start the threads
00048             DBWriter[] threadArray;
00049             threadArray = new DBWriter[NUMTHREADS];
00050             for (int i = 0; i < NUMTHREADS; i++) {
00051                 threadArray[i] = new DBWriter(myEnv, myDb, classCatalog);
00052                 threadArray[i].start();
00053             }
00054 
00055             for (int i = 0; i < NUMTHREADS; i++) {
00056                 threadArray[i].join();
00057             }
00058         } catch (Exception e) {
00059             System.err.println("TxnGuide: " + e.toString());
00060             e.printStackTrace();
00061         } finally {
00062             closeEnv();
00063         }
00064         System.out.println("All done.");
00065     }
00066 
00067 
00068     private static void openEnv() throws DatabaseException {
00069         System.out.println("opening env");
00070 
00071         // Set up the environment.
00072         EnvironmentConfig myEnvConfig = new EnvironmentConfig();
00073         myEnvConfig.setAllowCreate(true);
00074         myEnvConfig.setInitializeCache(true);
00075         myEnvConfig.setInitializeLocking(true);
00076         myEnvConfig.setInitializeLogging(true);
00077         myEnvConfig.setRunRecovery(true);
00078         myEnvConfig.setTransactional(true);
00079         // EnvironmentConfig.setThreaded(true) is the default behavior 
00080         // in Java, so we do not have to do anything to cause the
00081         // environment handle to be free-threaded.
00082 
00083         // Indicate that we want db to internally perform deadlock 
00084         // detection. Also indicate that the transaction that has
00085         // performed the least amount of write activity to
00086         // receive the deadlock notification, if any.
00087         myEnvConfig.setLockDetectMode(LockDetectMode.MINWRITE);
00088 
00089         // Set up the database
00090         DatabaseConfig myDbConfig = new DatabaseConfig();
00091         myDbConfig.setType(DatabaseType.BTREE);
00092         myDbConfig.setAllowCreate(true);
00093         myDbConfig.setTransactional(true);
00094         myDbConfig.setSortedDuplicates(true);
00095         myDbConfig.setReadUncommitted(true);
00096         // no DatabaseConfig.setThreaded() method available.
00097         // db handles in java are free-threaded so long as the
00098         // env is also free-threaded.
00099 
00100         try {
00101             // Open the environment
00102             myEnv = new Environment(new File(myEnvPath),    // Env home
00103                                     myEnvConfig);
00104 
00105             // Open the database. Do not provide a txn handle. This open
00106             // is autocommitted because DatabaseConfig.setTransactional()
00107             // is true.
00108             myDb = myEnv.openDatabase(null,     // txn handle
00109                                       dbName,   // Database file name
00110                                       null,     // Database name
00111                                       myDbConfig);
00112 
00113             // Used by the bind API for serializing objects 
00114             // Class database must not support duplicates
00115             myDbConfig.setSortedDuplicates(false);
00116             myClassDb = myEnv.openDatabase(null,     // txn handle
00117                                            cdbName,  // Database file name
00118                                            null,     // Database name,
00119                                            myDbConfig);
00120         } catch (FileNotFoundException fnfe) {
00121             System.err.println("openEnv: " + fnfe.toString());
00122             System.exit(-1);
00123         }
00124     }
00125 
00126     private static void closeEnv() {
00127         System.out.println("Closing env and databases");
00128         if (myDb != null ) {
00129             try {
00130                 myDb.close();
00131             } catch (DatabaseException e) {
00132                 System.err.println("closeEnv: myDb: " + 
00133                     e.toString());
00134                 e.printStackTrace();
00135             }
00136         }
00137 
00138         if (myClassDb != null ) {
00139             try {
00140                 myClassDb.close();
00141             } catch (DatabaseException e) {
00142                 System.err.println("closeEnv: myClassDb: " + 
00143                     e.toString());
00144                 e.printStackTrace();
00145             }
00146         }
00147 
00148         if (myEnv != null ) {
00149             try {
00150                 myEnv.close();
00151             } catch (DatabaseException e) {
00152                 System.err.println("closeEnv: " + e.toString());
00153                 e.printStackTrace();
00154             }
00155         }
00156     }
00157 
00158     private TxnGuide() {}
00159 
00160     private static void parseArgs(String args[]) {
00161         int nArgs = args.length;
00162         for(int i = 0; i < args.length; ++i) {
00163             if (args[i].startsWith("-")) {
00164                 switch(args[i].charAt(1)) {
00165                     case 'h':
00166                         if (i < nArgs - 1) {
00167                             myEnvPath = new String(args[++i]);
00168                         }
00169                     break;
00170                     default:
00171                         usage();
00172                 }
00173             }
00174         }
00175     }
00176 }

Generated on Sun Dec 25 12:14:28 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2