KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > je > txn > TxnGuide


1 // File TxnGuide.java
2

3 package je.txn;
4
5 import java.io.File JavaDoc;
6
7 import com.sleepycat.bind.serial.StoredClassCatalog;
8 import com.sleepycat.je.Database;
9 import com.sleepycat.je.DatabaseConfig;
10 import com.sleepycat.je.DatabaseException;
11 import com.sleepycat.je.Environment;
12 import com.sleepycat.je.EnvironmentConfig;
13
14 public class TxnGuide {
15
16     private static String JavaDoc myEnvPath = "./";
17     private static String JavaDoc dbName = "mydb.db";
18     private static String JavaDoc cdbName = "myclassdb.db";
19
20     // DB handles
21
private static Database myDb = null;
22     private static Database myClassDb = null;
23     private static Environment myEnv = null;
24
25     private static int NUMTHREADS = 5;
26
27     private static void usage() {
28         System.out.println("TxnGuide [-h <env directory>]");
29         System.exit(-1);
30     }
31
32     public static void main(String JavaDoc args[]) {
33         try {
34             // Parse the arguments list
35
parseArgs(args);
36             // Open the environment and databases
37
openEnv();
38             // Get our class catalog (used to serialize objects)
39
StoredClassCatalog classCatalog =
40                 new StoredClassCatalog(myClassDb);
41
42             // Start the threads
43
DBWriter[] threadArray;
44             threadArray = new DBWriter[NUMTHREADS];
45             for (int i = 0; i < NUMTHREADS; i++) {
46                 threadArray[i] = new DBWriter(myEnv, myDb, classCatalog);
47                 threadArray[i].start();
48             }
49
50             for (int i = 0; i < NUMTHREADS; i++) {
51                 threadArray[i].join();
52             }
53         } catch (Exception JavaDoc e) {
54             System.err.println("TxnGuide: " + e.toString());
55             e.printStackTrace();
56         } finally {
57             closeEnv();
58         }
59         System.out.println("All done.");
60     }
61
62
63     private static void openEnv() throws DatabaseException {
64         System.out.println("opening env");
65
66         // Set up the environment.
67
EnvironmentConfig myEnvConfig = new EnvironmentConfig();
68         myEnvConfig.setAllowCreate(true);
69         myEnvConfig.setTransactional(true);
70         // Environment handles are free-threaded in JE,
71
// so we do not have to do anything to cause the
72
// environment handle to be free-threaded.
73

74         // Set up the database
75
DatabaseConfig myDbConfig = new DatabaseConfig();
76         myDbConfig.setAllowCreate(true);
77         myDbConfig.setTransactional(true);
78         myDbConfig.setSortedDuplicates(true);
79         // no DatabaseConfig.setThreaded() method available.
80
// db handles in java are free-threaded so long as the
81
// env is also free-threaded.
82

83         // Open the environment
84
myEnv = new Environment(new File JavaDoc(myEnvPath), // Env home
85
myEnvConfig);
86
87         // Open the database. Do not provide a txn handle. This open
88
// is autocommitted because DatabaseConfig.setTransactional()
89
// is true.
90
myDb = myEnv.openDatabase(null, // txn handle
91
dbName, // Database file name
92
myDbConfig);
93
94         // Used by the bind API for serializing objects
95
// Class database must not support duplicates
96
myDbConfig.setSortedDuplicates(false);
97         myClassDb = myEnv.openDatabase(null, // txn handle
98
cdbName, // Database file name
99
myDbConfig);
100     }
101
102     private static void closeEnv() {
103         System.out.println("Closing env and databases");
104         if (myDb != null ) {
105             try {
106                 myDb.close();
107             } catch (DatabaseException e) {
108                 System.err.println("closeEnv: myDb: " +
109                     e.toString());
110                 e.printStackTrace();
111             }
112         }
113
114         if (myClassDb != null ) {
115             try {
116                 myClassDb.close();
117             } catch (DatabaseException e) {
118                 System.err.println("closeEnv: myClassDb: " +
119                     e.toString());
120                 e.printStackTrace();
121             }
122         }
123
124         if (myEnv != null ) {
125             try {
126                 myEnv.close();
127             } catch (DatabaseException e) {
128                 System.err.println("closeEnv: " + e.toString());
129                 e.printStackTrace();
130             }
131         }
132     }
133
134     private TxnGuide() {}
135
136     private static void parseArgs(String JavaDoc args[]) {
137         for(int i = 0; i < args.length; ++i) {
138             if (args[i].startsWith("-")) {
139                 switch(args[i].charAt(1)) {
140                     case 'h':
141                         myEnvPath = new String JavaDoc(args[++i]);
142                     break;
143                     default:
144                         usage();
145                 }
146             }
147         }
148     }
149 }
150
Popular Tags