1 // File TxnGuideDPL.java 2 3 package persist.txn; 4 5 import java.io.File; 6 7 import com.sleepycat.je.DatabaseException; 8 import com.sleepycat.je.Environment; 9 import com.sleepycat.je.EnvironmentConfig; 10 import com.sleepycat.persist.EntityStore; 11 import com.sleepycat.persist.StoreConfig; 12 13 public class TxnGuideDPL { 14 15 private static String myEnvPath = "./"; 16 private static String storeName = "exampleStore"; 17 18 // Handles 19 private static EntityStore myStore = null; 20 private static Environment myEnv = null; 21 22 private static final int NUMTHREADS = 5; 23 usage()24 private static void usage() { 25 System.out.println("TxnGuideDPL [-h <env directory>]"); 26 System.exit(-1); 27 } 28 main(String args[])29 public static void main(String args[]) { 30 try { 31 // Parse the arguments list 32 parseArgs(args); 33 // Open the environment and store 34 openEnv(); 35 36 // Start the threads 37 StoreWriter[] threadArray; 38 threadArray = new StoreWriter[NUMTHREADS]; 39 for (int i = 0; i < NUMTHREADS; i++) { 40 threadArray[i] = new StoreWriter(myEnv, myStore); 41 threadArray[i].start(); 42 } 43 44 for (int i = 0; i < NUMTHREADS; i++) { 45 threadArray[i].join(); 46 } 47 } catch (Exception e) { 48 System.err.println("TxnGuideDPL: " + e.toString()); 49 e.printStackTrace(); 50 } finally { 51 closeEnv(); 52 } 53 System.out.println("All done."); 54 } 55 openEnv()56 private static void openEnv() throws DatabaseException { 57 System.out.println("opening env and store"); 58 59 // Set up the environment. 60 EnvironmentConfig myEnvConfig = new EnvironmentConfig(); 61 myEnvConfig.setAllowCreate(true); 62 myEnvConfig.setTransactional(true); 63 // Environment handles are free-threaded by default in JE, 64 // so we do not have to do anything to cause the 65 // environment handle to be free-threaded. 66 67 // Set up the entity store 68 StoreConfig myStoreConfig = new StoreConfig(); 69 myStoreConfig.setAllowCreate(true); 70 myStoreConfig.setTransactional(true); 71 72 // Open the environment 73 myEnv = new Environment(new File(myEnvPath), // Env home 74 myEnvConfig); 75 76 // Open the store 77 myStore = new EntityStore(myEnv, storeName, myStoreConfig); 78 79 } 80 closeEnv()81 private static void closeEnv() { 82 System.out.println("Closing env and store"); 83 if (myStore != null ) { 84 try { 85 myStore.close(); 86 } catch (DatabaseException e) { 87 System.err.println("closeEnv: myStore: " + 88 e.toString()); 89 e.printStackTrace(); 90 } 91 } 92 93 if (myEnv != null ) { 94 try { 95 myEnv.close(); 96 } catch (DatabaseException e) { 97 System.err.println("closeEnv: " + e.toString()); 98 e.printStackTrace(); 99 } 100 } 101 } 102 TxnGuideDPL()103 private TxnGuideDPL() {} 104 parseArgs(String args[])105 private static void parseArgs(String args[]) { 106 int nArgs = args.length; 107 for(int i = 0; i < args.length; ++i) { 108 if (args[i].startsWith("-")) { 109 switch(args[i].charAt(1)) { 110 case 'h': 111 if (i < nArgs - 1) { 112 myEnvPath = new String(args[++i]); 113 } 114 break; 115 default: 116 usage(); 117 } 118 } 119 } 120 } 121 } 122