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

TxnGuideInMemory.java

00001 // File TxnGuideInMemory.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 TxnGuideInMemory {
00020 
00021     // DB handles
00022     private static Database myDb = null;
00023     private static Database myClassDb = null;
00024     private static Environment myEnv = null;
00025 
00026     private static int NUMTHREADS = 5;
00027 
00028     public static void main(String args[]) {
00029         try {
00030             // Open the environment and databases
00031             openEnv();
00032 
00033             // Get our class catalog (used to serialize objects)
00034             StoredClassCatalog classCatalog =
00035                 new StoredClassCatalog(myClassDb);
00036 
00037             // Start the threads
00038             DBWriter[] threadArray;
00039             threadArray = new DBWriter[NUMTHREADS];
00040             for (int i = 0; i < NUMTHREADS; i++) {
00041                 threadArray[i] = new DBWriter(myEnv, myDb, classCatalog, true);
00042                 threadArray[i].start();
00043             }
00044 
00045             System.out.println("Threads started.\n");
00046 
00047             for (int i = 0; i < NUMTHREADS; i++) {
00048                 threadArray[i].join();
00049             }
00050         } catch (Exception e) {
00051             System.err.println("TxnGuideInMemory: " + e.toString());
00052             e.printStackTrace();
00053         } finally {
00054             closeEnv();
00055         }
00056         System.out.println("All done.");
00057     }
00058 
00059     private static void openEnv() throws DatabaseException {
00060         System.out.println("opening env");
00061 
00062         // Set up the environment.
00063         EnvironmentConfig myEnvConfig = new EnvironmentConfig();
00064         
00065         // Region files are not backed by the filesystem, they are
00066         // backed by heap memory.
00067         myEnvConfig.setPrivate(true);
00068         myEnvConfig.setAllowCreate(true);
00069         myEnvConfig.setInitializeCache(true);
00070         myEnvConfig.setInitializeLocking(true);
00071         myEnvConfig.setInitializeLogging(true);
00072         myEnvConfig.setThreaded(true);
00073         
00074         myEnvConfig.setTransactional(true);
00075         // EnvironmentConfig.setThreaded(true) is the default behavior 
00076         // in Java, so we do not have to do anything to cause the
00077         // environment handle to be free-threaded.
00078 
00079         // Indicate that we want db to internally perform deadlock 
00080         // detection. Also indicate that the transaction that has
00081         // performed the least amount of write activity to
00082         // receive the deadlock notification, if any.
00083         myEnvConfig.setLockDetectMode(LockDetectMode.MINWRITE);
00084 
00085         // Specify in-memory logging
00086         myEnvConfig.setLogInMemory(true);
00087         // Specify the size of the in-memory log buffer
00088         // Must be large enough to handle the log data created by
00089         // the largest transaction.
00090         myEnvConfig.setLogBufferSize(10 * 1024 * 1024);
00091         // Specify the size of the in-memory cache
00092         // Set it large enough so that it won't page.
00093         myEnvConfig.setCacheSize(10 * 1024 * 1024);
00094 
00095         // Set up the database
00096         DatabaseConfig myDbConfig = new DatabaseConfig();
00097         myDbConfig.setType(DatabaseType.BTREE);
00098         myDbConfig.setAllowCreate(true);
00099         myDbConfig.setTransactional(true);
00100         myDbConfig.setSortedDuplicates(true);
00101         // no DatabaseConfig.setThreaded() method available.
00102         // db handles in java are free-threaded so long as the
00103         // env is also free-threaded.
00104 
00105         try {
00106             // Open the environment
00107             myEnv = new Environment(null,    // Env home
00108                                     myEnvConfig);
00109 
00110             // Open the database. Do not provide a txn handle. This open
00111             // is autocommitted because DatabaseConfig.setTransactional()
00112             // is true.
00113             myDb = myEnv.openDatabase(null,     // txn handle
00114                                       null,     // Database file name
00115                                       null,     // Database name
00116                                       myDbConfig);
00117 
00118             // Used by the bind API for serializing objects 
00119             // Class database must not support duplicates
00120             myDbConfig.setSortedDuplicates(false);
00121             myClassDb = myEnv.openDatabase(null,     // txn handle
00122                                            null,     // Database file name
00123                                            null,     // Database name,
00124                                            myDbConfig);
00125         } catch (FileNotFoundException fnfe) {
00126             System.err.println("openEnv: " + fnfe.toString());
00127             System.exit(-1);
00128         }
00129     }
00130 
00131     private static void closeEnv() {
00132         System.out.println("Closing env");
00133         if (myDb != null ) {
00134             try {
00135                 myDb.close();
00136             } catch (DatabaseException e) {
00137                 System.err.println("closeEnv: myDb: " + 
00138                     e.toString());
00139                 e.printStackTrace();
00140             }
00141         }
00142 
00143         if (myClassDb != null ) {
00144             try {
00145                 myClassDb.close();
00146             } catch (DatabaseException e) {
00147                 System.err.println("closeEnv: myClassDb: " + 
00148                     e.toString());
00149                 e.printStackTrace();
00150             }
00151         }
00152 
00153         if (myEnv != null ) {
00154             try {
00155                 myEnv.close();
00156             } catch (DatabaseException e) {
00157                 System.err.println("closeEnv: " + e.toString());
00158                 e.printStackTrace();
00159             }
00160         }
00161     }
00162 
00163     private TxnGuideInMemory() {}
00164 }

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