1// File TxnGuideInMemory.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 TxnGuideInMemory { 20 21 // DB handles 22 private static Database myDb = null; 23 private static Database myClassDb = null; 24 private static Environment myEnv = null; 25 26 private static int NUMTHREADS = 5; 27 28 public static void main(String args[]) { 29 try { 30 // Open the environment and databases 31 openEnv(); 32 33 // Get our class catalog (used to serialize objects) 34 StoredClassCatalog classCatalog = 35 new StoredClassCatalog(myClassDb); 36 37 // Start the threads 38 DBWriter[] threadArray; 39 threadArray = new DBWriter[NUMTHREADS]; 40 for (int i = 0; i < NUMTHREADS; i++) { 41 threadArray[i] = new DBWriter(myEnv, myDb, classCatalog, true); 42 threadArray[i].start(); 43 } 44 45 System.out.println("Threads started.\n"); 46 47 for (int i = 0; i < NUMTHREADS; i++) { 48 threadArray[i].join(); 49 } 50 } catch (Exception e) { 51 System.err.println("TxnGuideInMemory: " + e.toString()); 52 e.printStackTrace(); 53 } finally { 54 closeEnv(); 55 } 56 System.out.println("All done."); 57 } 58 59 private static void openEnv() throws DatabaseException { 60 System.out.println("opening env"); 61 62 // Set up the environment. 63 EnvironmentConfig myEnvConfig = new EnvironmentConfig(); 64 65 // Region files are not backed by the filesystem, they are 66 // backed by heap memory. 67 myEnvConfig.setPrivate(true); 68 myEnvConfig.setAllowCreate(true); 69 myEnvConfig.setInitializeCache(true); 70 myEnvConfig.setInitializeLocking(true); 71 myEnvConfig.setInitializeLogging(true); 72 myEnvConfig.setThreaded(true); 73 74 myEnvConfig.setTransactional(true); 75 // EnvironmentConfig.setThreaded(true) is the default behavior 76 // in Java, so we do not have to do anything to cause the 77 // environment handle to be free-threaded. 78 79 // Indicate that we want db to internally perform deadlock 80 // detection. Also indicate that the transaction that has 81 // performed the least amount of write activity to 82 // receive the deadlock notification, if any. 83 myEnvConfig.setLockDetectMode(LockDetectMode.MINWRITE); 84 85 // Specify in-memory logging 86 myEnvConfig.setLogInMemory(true); 87 // Specify the size of the in-memory log buffer 88 // Must be large enough to handle the log data created by 89 // the largest transaction. 90 myEnvConfig.setLogBufferSize(10 * 1024 * 1024); 91 // Specify the size of the in-memory cache 92 // Set it large enough so that it won't page. 93 myEnvConfig.setCacheSize(10 * 1024 * 1024); 94 95 // Set up the database 96 DatabaseConfig myDbConfig = new DatabaseConfig(); 97 myDbConfig.setType(DatabaseType.BTREE); 98 myDbConfig.setAllowCreate(true); 99 myDbConfig.setTransactional(true); 100 myDbConfig.setSortedDuplicates(true); 101 // no DatabaseConfig.setThreaded() method available. 102 // db handles in java are free-threaded so long as the 103 // env is also free-threaded. 104 105 try { 106 // Open the environment 107 myEnv = new Environment(null, // Env home 108 myEnvConfig); 109 110 // Open the database. Do not provide a txn handle. This open 111 // is autocommitted because DatabaseConfig.setTransactional() 112 // is true. 113 myDb = myEnv.openDatabase(null, // txn handle 114 null, // Database file name 115 null, // Database name 116 myDbConfig); 117 118 // Used by the bind API for serializing objects 119 // Class database must not support duplicates 120 myDbConfig.setSortedDuplicates(false); 121 myClassDb = myEnv.openDatabase(null, // txn handle 122 null, // Database file name 123 null, // Database name, 124 myDbConfig); 125 } catch (FileNotFoundException fnfe) { 126 System.err.println("openEnv: " + fnfe.toString()); 127 System.exit(-1); 128 } 129 } 130 131 private static void closeEnv() { 132 System.out.println("Closing env"); 133 if (myDb != null ) { 134 try { 135 myDb.close(); 136 } catch (DatabaseException e) { 137 System.err.println("closeEnv: myDb: " + 138 e.toString()); 139 e.printStackTrace(); 140 } 141 } 142 143 if (myClassDb != null ) { 144 try { 145 myClassDb.close(); 146 } catch (DatabaseException e) { 147 System.err.println("closeEnv: myClassDb: " + 148 e.toString()); 149 e.printStackTrace(); 150 } 151 } 152 153 if (myEnv != null ) { 154 try { 155 myEnv.close(); 156 } catch (DatabaseException e) { 157 System.err.println("closeEnv: " + e.toString()); 158 e.printStackTrace(); 159 } 160 } 161 } 162 163 private TxnGuideInMemory() {} 164} 165