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