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