KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > Environment


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: Environment.java,v 1.179 2006/11/06 20:36:55 linda Exp $
7  */

8
9 package com.sleepycat.je;
10
11 import java.io.File JavaDoc;
12 import java.io.PrintStream JavaDoc;
13 import java.util.Collections JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Set JavaDoc;
18 import java.util.logging.Level JavaDoc;
19
20 import com.sleepycat.je.dbi.DatabaseImpl;
21 import com.sleepycat.je.dbi.DbConfigManager;
22 import com.sleepycat.je.dbi.DbEnvPool;
23 import com.sleepycat.je.dbi.EnvironmentImpl;
24 import com.sleepycat.je.txn.Locker;
25 import com.sleepycat.je.txn.LockerFactory;
26 import com.sleepycat.je.utilint.DatabaseUtil;
27 import com.sleepycat.je.utilint.Tracer;
28
29 /**
30  * Javadoc for this public class is generated
31  * via the doc templates in the doc_src directory.
32  */

33 public class Environment {
34
35     protected EnvironmentImpl environmentImpl;
36     private TransactionConfig defaultTxnConfig;
37     private EnvironmentMutableConfig handleConfig;
38
39     private Set JavaDoc referringDbs;
40     private Set JavaDoc referringDbTxns;
41
42     private boolean valid;
43
44     /**
45      * Javadoc for this public method is generated via
46      * the doc templates in the doc_src directory.
47      */

48     public static final String JavaDoc CLEANER_NAME = "Cleaner";
49
50     /**
51      * Javadoc for this public method is generated via
52      * the doc templates in the doc_src directory.
53      */

54     public static final String JavaDoc INCOMP_NAME = "INCompressor";
55
56     /**
57      * Javadoc for this public method is generated via
58      * the doc templates in the doc_src directory.
59      */

60     public static final String JavaDoc CHECKPOINTER_NAME = "Checkpointer";
61
62     /**
63      * Javadoc for this public method is generated via
64      * the doc templates in the doc_src directory.
65      */

66     public Environment(File JavaDoc envHome, EnvironmentConfig configuration)
67         throws DatabaseException {
68
69         environmentImpl = null;
70         referringDbs = Collections.synchronizedSet(new HashSet JavaDoc());
71         referringDbTxns = Collections.synchronizedSet(new HashSet JavaDoc());
72         valid = false;
73
74         DatabaseUtil.checkForNullParam(envHome, "envHome");
75
76         /* If the user specified a null object, use the default */
77         EnvironmentConfig baseConfig =
78             (configuration == null) ?
79             EnvironmentConfig.DEFAULT :
80             configuration;
81
82         /* Make a copy, apply je.properties, and init the handle config. */
83         EnvironmentConfig useConfig = baseConfig.cloneConfig();
84         applyFileConfig(envHome, useConfig);
85         copyToHandleConfig(useConfig, useConfig);
86
87         /* Look in the shared pool for this environment. */
88         DbEnvPool.EnvironmentImplInfo envInfo;
89
90     /*
91      * An Environment.close() could occur between the time we get the
92      * envImpl out of the DbEnvPool above and the time we increment the
93      * reference count. If this were to happen then environmentImpl would
94      * refer to an envImpl that was no longer in the DbEnvPool (because
95      * when it was closed it was removed. Check here that the envImpl we
96      * have in hand is still in the DbEnvPool, and if not, abandon it and
97      * try over again. [#15200].
98      */

99     do {
100         envInfo =
101         DbEnvPool.getInstance().getEnvironment(envHome, useConfig);
102         environmentImpl = envInfo.envImpl;
103
104         /* Check if the environmentImpl is valid. */
105         environmentImpl.checkIfInvalid();
106
107         if (!envInfo.firstHandle && configuration != null) {
108
109         /* Perform all environment config updates atomically. */
110         synchronized (environmentImpl) {
111
112             /*
113              * If a non-null configuration parameter was passed in and
114              * this is not the handle that created the underlying
115              * EnvironmentImpl, check that the configuration parameters
116              * specified for this open match those of the currently
117              * open environment. An exception is thrown if the check
118              * fails.
119              *
120              * Don't do this check if this handle created the
121              * environment because the creation might have modified the
122              * parameters, which would create a Catch-22 in terms of
123              * validation. For example, je.maxMemory will be
124              * overridden if the JVM's -mx flag is less than that
125              * setting, so the initial handle's config object won't be
126              * the same as the passed in config.
127              */

128             environmentImpl.checkImmutablePropsForEquality(useConfig);
129         }
130         }
131             
132         if (!valid) {
133         valid = true;
134         }
135
136         /* Successful, increment reference count */
137         environmentImpl.incReferenceCount();
138     } while (DbEnvPool.getInstance().
139          getExistingEnvironment(envHome).envImpl != environmentImpl);
140     }
141
142     /**
143      * Get an Environment for an existing EnvironmentImpl. Used by utilities
144      * such as the JMX MBean which don't want to open the environment or be
145      * reference counted. The calling application must take care not to retain
146      * the the doc templates in the doc_src directory.
147      */

148     Environment(File JavaDoc envHome)
149         throws DatabaseException {
150
151         environmentImpl = null;
152         valid = false;
153
154         /* Look in the shared pool for this environment. */
155         DbEnvPool.EnvironmentImplInfo envInfo;
156     while (true) {
157         envInfo =
158         DbEnvPool.getInstance().getExistingEnvironment(envHome);
159         
160         EnvironmentImpl foundImpl = envInfo.envImpl;
161         if (foundImpl != null) {
162         /* Check if the environmentImpl is valid. */
163         foundImpl.checkIfInvalid();
164
165         /* Successful, increment reference count */
166         environmentImpl = foundImpl;
167         environmentImpl.incReferenceCount();
168
169         /*
170          * An Environment.close() could occur between the time we get
171          * the envImpl out of the DbEnvPool above and the time we
172          * increment the reference count. If this were to happen then
173          * environmentImpl would refer to an envImpl that was no longer
174          * in the DbEnvPool (because when it was closed it was removed.
175          * Check here that the envImpl we have in hand is still in the
176          * DbEnvPool, and if not, abandon it and try over again.
177          * [#15200].
178          */

179         if (DbEnvPool.getInstance().
180             getExistingEnvironment(envHome).envImpl !=
181             environmentImpl) {
182             continue;
183         }
184
185         /*
186          * Initialize the handle's environment config, so that it's
187          * valid to call setConfig and getConfig against this handle.
188          * Make a copy, apply je.properties, and init the handle
189          * config.
190          */

191         EnvironmentConfig useConfig =
192             EnvironmentConfig.DEFAULT.cloneConfig();
193         applyFileConfig(envHome, useConfig);
194         copyToHandleConfig(useConfig, useConfig);
195
196         /* Need this in order to open database handles. */
197         referringDbs = Collections.synchronizedSet(new HashSet JavaDoc());
198
199         valid = true;
200         break;
201         }
202     }
203     }
204
205     /**
206      * Apply the configurations specified in the je.properties file to override
207      * any programatically set configurations.
208      */

209     private void applyFileConfig(File JavaDoc envHome,
210                                  EnvironmentMutableConfig useConfig)
211         throws IllegalArgumentException JavaDoc {
212
213         /* Apply the je.properties file. */
214         if (useConfig.getLoadPropertyFile()) {
215             DbConfigManager.applyFileConfig(envHome,
216                                             DbInternal.getProps(useConfig),
217                                             false /* forReplication */,
218                                             useConfig.getClass().getName());
219         }
220     }
221
222     /**
223      * Javadoc for this public method is generated via
224      * the doc templates in the doc_src directory.
225      */

226     public synchronized void close()
227         throws DatabaseException {
228
229         checkHandleIsValid();
230         try {
231             checkEnv();
232         } catch (RunRecoveryException e) {
233
234             /*
235              * We're trying to close on an environment that has seen a fatal
236              * exception. Try to do the minimum, such as closing file
237              * descriptors, to support re-opening the environment in the same
238              * jvm.
239              */

240             if (environmentImpl != null) {
241                 environmentImpl.closeAfterRunRecovery();
242             }
243             return;
244         }
245
246         StringBuffer JavaDoc errors = new StringBuffer JavaDoc();
247         try {
248             if (referringDbs != null) {
249                 int nDbs = referringDbs.size();
250                 if (nDbs != 0) {
251                     errors.append("There ");
252                     if (nDbs == 1) {
253                         errors.append
254                             ("is 1 open Database in the Environment.\n");
255                     } else {
256                         errors.append("are ");
257                         errors.append(nDbs);
258                         errors.append
259                             (" open Database in the Environment.\n");
260                     }
261                     errors.append("Closing the following databases:\n");
262
263                     Iterator JavaDoc iter = referringDbs.iterator();
264                     while (iter.hasNext()) {
265                         Database db = (Database) iter.next();
266                         /*
267                          * Save the db name before we attempt the close, it's
268                          * unavailable after the close.
269                          */

270                         String JavaDoc dbName = db.getDebugName();
271                         errors.append(dbName).append(" ");
272                         try {
273                             db.close();
274                         } catch (RunRecoveryException e) {
275                             throw e;
276                         } catch (DatabaseException DBE) {
277                             errors.append("\nWhile closing Database ");
278                             errors.append(dbName);
279                             errors.append(" encountered exception: ");
280                             errors.append(DBE).append("\n");
281                         }
282                     }
283                 }
284             }
285
286             if (referringDbTxns != null) {
287                 int nTxns = referringDbTxns.size();
288                 if (nTxns != 0) {
289                     Iterator JavaDoc iter = referringDbTxns.iterator();
290                     errors.append("There ");
291                     if (nTxns == 1) {
292                         errors.append("is 1 existing transaction opened against");
293                         errors.append(" the Environment.\n");
294                     } else {
295                         errors.append("are ");
296                         errors.append(nTxns);
297                         errors.append(" existing transactions opened against");
298                         errors.append(" the Environment.\n");
299                     }
300                     errors.append("Aborting open transactions ...\n");
301
302                     while (iter.hasNext()) {
303                         Transaction txn = (Transaction) iter.next();
304                         try {
305                             txn.abort();
306                         } catch (RunRecoveryException e) {
307                             throw e;
308                         } catch (DatabaseException DBE) {
309                             errors.append("\nWhile aborting transaction ");
310                             errors.append(txn.getId());
311                             errors.append(" encountered exception: ");
312                             errors.append(DBE).append("\n");
313                         }
314                     }
315                 }
316             }
317
318             try {
319                 environmentImpl.close();
320             } catch (RunRecoveryException e) {
321                 throw e;
322             } catch (DatabaseException DBE) {
323                 errors.append
324                     ("\nWhile closing Environment encountered exception: ");
325                 errors.append(DBE).append("\n");
326             }
327         } finally {
328             environmentImpl = null;
329             valid = false;
330             if (errors.length() > 0) {
331                 throw new DatabaseException(errors.toString());
332             }
333         }
334     }
335     
336     /**
337      * Javadoc for this public method is generated via
338      * the doc templates in the doc_src directory.
339      */

340     public synchronized Database openDatabase(Transaction txn,
341                                               String JavaDoc databaseName,
342                                               DatabaseConfig dbConfig)
343         throws DatabaseException {
344
345     try {
346         if (dbConfig == null) {
347         dbConfig = DatabaseConfig.DEFAULT;
348         }
349         Database db = new Database(this);
350         openDb(txn, db, databaseName, dbConfig, false);
351         return db;
352     } catch (Error JavaDoc E) {
353         environmentImpl.invalidate(E);
354         throw E;
355     }
356     }
357
358     /**
359      * Javadoc for this public class is generated via
360      * the doc templates in the doc_src directory.
361      */

362     public synchronized
363     SecondaryDatabase openSecondaryDatabase(Transaction txn,
364                         String JavaDoc databaseName,
365                         Database primaryDatabase,
366                         SecondaryConfig dbConfig)
367         throws DatabaseException {
368
369     try {
370         if (dbConfig == null) {
371         dbConfig = SecondaryConfig.DEFAULT;
372         }
373         SecondaryDatabase db =
374         new SecondaryDatabase(this, dbConfig, primaryDatabase);
375         openDb(txn, db, databaseName, dbConfig,
376            dbConfig.getAllowPopulate());
377         return db;
378     } catch (Error JavaDoc E) {
379         environmentImpl.invalidate(E);
380         throw E;
381     }
382     }
383
384     private void openDb(Transaction txn,
385             Database newDb,
386             String JavaDoc databaseName,
387                         DatabaseConfig dbConfig,
388                         boolean needWritableLockerForInit)
389         throws DatabaseException {
390
391         checkEnv();
392         DatabaseUtil.checkForNullParam(databaseName, "databaseName");
393
394         Tracer.trace(Level.FINEST, environmentImpl, "Environment.open: " +
395                      " name=" + databaseName +
396                      " dbConfig=" + dbConfig);
397
398         /*
399          * Check that the open configuration doesn't conflict with the
400          * environmentImpl configuration.
401          */

402         validateDbConfigAgainstEnv(dbConfig, databaseName);
403
404         Locker locker = null;
405         boolean operationOk = false;
406     boolean dbIsClosing = false;
407         try {
408
409             /*
410              * Does this database exist? Get a transaction to use. If the
411              * database exists already, we really only need a readable locker.
412              * If the database must be created, we need a writable one.
413              * Unfortunately, we have to get the readable one first before we
414              * know whether we have to create. However, if we need to write
415              * during initialization (to populate a secondary for example),
416              * then just create a writable locker now.
417              */

418             boolean isWritableLocker;
419             if (needWritableLockerForInit) {
420                 locker = LockerFactory.getWritableLocker
421                     (this,
422                      txn,
423                      dbConfig.getTransactional(),
424                      true, // retainNonTxnLocks
425
null);
426                 isWritableLocker = true;
427             } else {
428                 locker = LockerFactory.getReadableLocker
429                     (this, txn,
430                      dbConfig.getTransactional(),
431                      true, // retainNonTxnLocks
432
false); // readCommittedIsolation
433
isWritableLocker = !dbConfig.getTransactional() ||
434                                    locker.isTransactional();
435             }
436
437             DatabaseImpl database = environmentImpl.getDb(locker,
438                                                           databaseName,
439                                                           newDb);
440             boolean databaseExists =
441                 (database == null) ? false :
442                 ((database.isDeleted()) ? false : true);
443
444             if (databaseExists) {
445                 if (dbConfig.getAllowCreate() &&
446                     dbConfig.getExclusiveCreate()) {
447                     /* We intended to create this, but it already exists. */
448             dbIsClosing = true;
449                     throw new DatabaseException
450                         ("Database " + databaseName + " already exists");
451                 }
452
453                 newDb.initExisting(this, locker, database, dbConfig);
454             } else {
455                 /* No database. Create if we're allowed to. */
456                 if (dbConfig.getAllowCreate()) {
457
458                     /*
459                      * We're going to have to do some writing. Switch to a
460                      * writable locker if we don't already have one.
461                      */

462                     if (!isWritableLocker) {
463                         locker.operationEnd(OperationStatus.SUCCESS);
464                         locker = LockerFactory.getWritableLocker
465                             (this,
466                              txn,
467                              dbConfig.getTransactional(),
468                              true, // retainNonTxnLocks
469
null);
470                         isWritableLocker = true;
471                     }
472
473                     newDb.initNew(this, locker, databaseName, dbConfig);
474                 } else {
475                     /* We aren't allowed to create this database. */
476                     throw new DatabaseNotFoundException("Database " +
477                                                         databaseName +
478                                                         " not found.");
479                 }
480             }
481
482             operationOk = true;
483             addReferringHandle(newDb);
484         } finally {
485
486             /*
487              * Tell the transaction that this operation is over. Some types of
488              * transactions (BasicLocker and AutoTxn) will actually finish. The
489              * transaction can decide if it is finishing and if it needs to
490              * transfer the db handle lock it owns to someone else.
491              */

492             if (locker != null) {
493                 locker.setHandleLockOwner(operationOk, newDb, dbIsClosing);
494                 locker.operationEnd(operationOk);
495             }
496         }
497     }
498
499     private void validateDbConfigAgainstEnv(DatabaseConfig dbConfig,
500                                             String JavaDoc databaseName)
501         throws DatabaseException {
502
503         /* Check operation's transactional status against the Environment */
504         if (dbConfig.getTransactional() &&
505             !(environmentImpl.isTransactional())) {
506             throw new DatabaseException
507                 ("Attempted to open Database " + databaseName +
508                  " transactionally, but parent Environment is" +
509                  " not transactional");
510         }
511
512         /* Check read/write status */
513         if (environmentImpl.isReadOnly() && (!dbConfig.getReadOnly())) {
514             throw new DatabaseException
515                 ("Attempted to open Database " + databaseName +
516                  " as writable but parent Environment is read only ");
517         }
518     }
519
520     /**
521      * Javadoc for this public method is generated via
522      * the doc templates in the doc_src directory.
523      */

524     public void removeDatabase(Transaction txn,
525                                String JavaDoc databaseName)
526         throws DatabaseException {
527
528         checkHandleIsValid();
529         checkEnv();
530         DatabaseUtil.checkForNullParam(databaseName, "databaseName");
531
532         Locker locker = null;
533         boolean operationOk = false;
534         try {
535
536             /*
537              * Note: use env level isTransactional as proxy for the db
538              * isTransactional.
539              */

540             locker = LockerFactory.getWritableLocker
541                         (this, txn,
542                          environmentImpl.isTransactional(),
543                          true /*retainNonTxnLocks*/,
544                          null);
545             environmentImpl.dbRemove(locker, databaseName);
546             operationOk = true;
547     } catch (Error JavaDoc E) {
548         environmentImpl.invalidate(E);
549         throw E;
550         } finally {
551             if (locker != null) {
552                 locker.operationEnd(operationOk);
553             }
554         }
555     }
556
557     /**
558      * Javadoc for this public method is generated via
559      * the doc templates in the doc_src directory.
560      */

561     public void renameDatabase(Transaction txn,
562                                String JavaDoc databaseName,
563                                String JavaDoc newName)
564         throws DatabaseException {
565
566         DatabaseUtil.checkForNullParam(databaseName, "databaseName");
567         DatabaseUtil.checkForNullParam(newName, "newName");
568
569         checkHandleIsValid();
570         checkEnv();
571
572         Locker locker = null;
573         boolean operationOk = false;
574         try {
575
576             /*
577              * Note: use env level isTransactional as proxy for the db
578              * isTransactional.
579              */

580             locker = LockerFactory.getWritableLocker
581                         (this, txn,
582                          environmentImpl.isTransactional(),
583                          true /*retainNonTxnLocks*/,
584                          null);
585             environmentImpl.dbRename(locker, databaseName, newName);
586             operationOk = true;
587     } catch (Error JavaDoc E) {
588         environmentImpl.invalidate(E);
589         throw E;
590         } finally {
591             if (locker != null) {
592                 locker.operationEnd(operationOk);
593             }
594         }
595     }
596     
597     /**
598      * Javadoc for this public method is generated via
599      * the doc templates in the doc_src directory.
600      */

601     public long truncateDatabase(Transaction txn,
602                                  String JavaDoc databaseName,
603                                  boolean returnCount)
604         throws DatabaseException {
605
606         checkHandleIsValid();
607         checkEnv();
608         DatabaseUtil.checkForNullParam(databaseName, "databaseName");
609
610         Locker locker = null;
611         boolean operationOk = false;
612         long count = 0;
613         try {
614
615             /*
616              * Note: use env level isTransactional as proxy for the db
617              * isTransactional.
618              */

619             locker = LockerFactory.getWritableLocker
620                         (this, txn,
621                          environmentImpl.isTransactional(),
622                          true /*retainNonTxnLocks*/,
623                          null);
624             
625             count = environmentImpl.truncate(locker,
626                                              databaseName,
627                                              returnCount);
628
629             operationOk = true;
630     } catch (Error JavaDoc E) {
631         environmentImpl.invalidate(E);
632         throw E;
633         } finally {
634             if (locker != null) {
635                 locker.operationEnd(operationOk);
636             }
637         }
638         return count;
639     }
640
641     /**
642      * Returns the current memory usage in bytes for all btrees in the
643      * environmentImpl.
644      */

645     long getMemoryUsage()
646         throws DatabaseException {
647
648         checkHandleIsValid();
649         checkEnv();
650
651         return environmentImpl.getMemoryBudget().getCacheMemoryUsage();
652     }
653
654     /**
655      * Javadoc for this public method is generated via
656      * the doc templates in the doc_src directory.
657      */

658     public File JavaDoc getHome()
659         throws DatabaseException {
660
661         checkHandleIsValid();
662         return environmentImpl.getEnvironmentHome();
663     }
664     
665     /*
666      * Transaction management
667      */

668
669     /**
670      * Returns the default txn config for this environment handle.
671      */

672     TransactionConfig getDefaultTxnConfig() {
673         return defaultTxnConfig;
674     }
675
676     /**
677      * Copies the handle properties out of the config properties, and
678      * initializes the default transaction config.
679      */

680     private void copyToHandleConfig(EnvironmentMutableConfig useConfig,
681                                     EnvironmentConfig initStaticConfig)
682         throws DatabaseException {
683
684         /*
685          * Create the new objects, initialize them, then change the instance
686          * fields. This avoids synchronization issues.
687          */

688         EnvironmentMutableConfig newHandleConfig =
689             new EnvironmentMutableConfig();
690         useConfig.copyHandlePropsTo(newHandleConfig);
691         this.handleConfig = newHandleConfig;
692
693         TransactionConfig newTxnConfig =
694             TransactionConfig.DEFAULT.cloneConfig();
695         newTxnConfig.setNoSync(handleConfig.getTxnNoSync());
696         newTxnConfig.setWriteNoSync(handleConfig.getTxnWriteNoSync());
697         if (initStaticConfig != null) {
698             newTxnConfig.setSerializableIsolation
699                 (initStaticConfig.getTxnSerializableIsolation());
700             newTxnConfig.setReadCommitted
701                 (initStaticConfig.getTxnReadCommitted());
702         } else {
703             newTxnConfig.setSerializableIsolation
704                 (defaultTxnConfig.getSerializableIsolation());
705             newTxnConfig.setReadCommitted
706                 (defaultTxnConfig.getReadCommitted());
707         }
708         this.defaultTxnConfig = newTxnConfig;
709     }
710
711     /**
712      * Javadoc for this public method is generated via
713      * the doc templates in the doc_src directory.
714      */

715     public Transaction beginTransaction(Transaction parent,
716                                         TransactionConfig txnConfig)
717         throws DatabaseException {
718
719     try {
720         return beginTransactionInternal(parent, txnConfig);
721     } catch (Error JavaDoc E) {
722         environmentImpl.invalidate(E);
723         throw E;
724     }
725     }
726
727     private Transaction beginTransactionInternal(Transaction parent,
728                          TransactionConfig txnConfig)
729     throws DatabaseException {
730
731         checkHandleIsValid();
732         checkEnv();
733
734         if (!environmentImpl.isTransactional()) {
735             throw new DatabaseException
736         ("Transactions can not be used in a non-transactional " +
737          "environment");
738         }
739
740         if (txnConfig != null &&
741             ((txnConfig.getSerializableIsolation() &&
742               txnConfig.getReadUncommitted()) ||
743              (txnConfig.getSerializableIsolation() &&
744               txnConfig.getReadCommitted()) ||
745              (txnConfig.getReadUncommitted() &&
746               txnConfig.getReadCommitted()))) {
747             throw new IllegalArgumentException JavaDoc
748                 ("Only one may be specified: SerializableIsolation, " +
749                  "ReadCommitted or ReadUncommitted");
750         }
751
752         /*
753      * Apply txn config defaults. We don't need to clone unless we have to
754      * apply the env default, since we don't hold onto a txn config
755      * reference.
756          */

757         TransactionConfig useConfig = null;
758         if (txnConfig == null) {
759             useConfig = defaultTxnConfig;
760         } else {
761             if (defaultTxnConfig.getNoSync() ||
762                 defaultTxnConfig.getWriteNoSync()) {
763
764                 /*
765                  * The environment sync settings have been set, check if any
766                  * were set in the user's txn config. If none were set in the
767                  * user's config, apply the environment defaults
768                  */

769                 if (!txnConfig.getNoSync() &&
770                     !txnConfig.getSync() &&
771                     !txnConfig.getWriteNoSync()) {
772             useConfig = txnConfig.cloneConfig();
773                     if (defaultTxnConfig.getWriteNoSync()) {
774                         useConfig.setWriteNoSync(true);
775                     } else {
776                         useConfig.setNoSync(true);
777                     }
778                 }
779             }
780
781             /* Apply isolation level default. */
782             if (!txnConfig.getSerializableIsolation() &&
783                 !txnConfig.getReadCommitted() &&
784                 !txnConfig.getReadUncommitted()) {
785                 if (defaultTxnConfig.getSerializableIsolation()) {
786                     if (useConfig == null) {
787                         useConfig = txnConfig.cloneConfig();
788                     }
789                     useConfig.setSerializableIsolation(true);
790                 } else if (defaultTxnConfig.getReadCommitted()) {
791                     if (useConfig == null) {
792                         useConfig = txnConfig.cloneConfig();
793                     }
794                     useConfig.setReadCommitted(true);
795                 }
796             }
797             
798             /* No environment level defaults applied. */
799             if (useConfig == null) {
800                 useConfig = txnConfig;
801             }
802         }
803
804         Transaction txn =
805             new Transaction
806         (this, environmentImpl.txnBegin(parent, useConfig));
807         addReferringHandle(txn);
808         return txn;
809     }
810
811     /**
812      * Javadoc for this public method is generated via
813      * the doc templates in the doc_src directory.
814      */

815     public void checkpoint(CheckpointConfig ckptConfig)
816         throws DatabaseException {
817
818     try {
819         checkHandleIsValid();
820         checkEnv();
821         CheckpointConfig useConfig =
822         (ckptConfig == null) ? CheckpointConfig.DEFAULT : ckptConfig;
823
824         environmentImpl.invokeCheckpoint(useConfig,
825                          false, // flushAll
826
"api");
827     } catch (Error JavaDoc E) {
828         environmentImpl.invalidate(E);
829         throw E;
830     }
831     }
832
833     /**
834      * Javadoc for this public method is generated via
835      * the doc templates in the doc_src directory.
836      */

837     public void sync()
838         throws DatabaseException {
839
840     try {
841         checkHandleIsValid();
842         checkEnv();
843         CheckpointConfig config = new CheckpointConfig();
844         config.setForce(true);
845         environmentImpl.invokeCheckpoint(config,
846                          true, // flushAll
847
"sync");
848     } catch (Error JavaDoc E) {
849         environmentImpl.invalidate(E);
850         throw E;
851     }
852     }
853
854     /**
855      * Javadoc for this public method is generated via
856      * the doc templates in the doc_src directory.
857      */

858     public int cleanLog()
859         throws DatabaseException {
860
861     try {
862         checkHandleIsValid();
863         checkEnv();
864         return environmentImpl.invokeCleaner();
865     } catch (Error JavaDoc E) {
866         environmentImpl.invalidate(E);
867         throw E;
868     }
869     }
870
871     /**
872      * Javadoc for this public method is generated via
873      * the doc templates in the doc_src directory.
874      */

875     public void evictMemory()
876         throws DatabaseException {
877
878     try {
879         checkHandleIsValid();
880         checkEnv();
881         environmentImpl.invokeEvictor();
882     } catch (Error JavaDoc E) {
883         environmentImpl.invalidate(E);
884         throw E;
885     }
886     }
887
888     /**
889      * Javadoc for this public method is generated via
890      * the doc templates in the doc_src directory.
891      */

892     public void compress()
893         throws DatabaseException {
894
895     try {
896         checkHandleIsValid();
897         checkEnv();
898         environmentImpl.invokeCompressor();
899     } catch (Error JavaDoc E) {
900         environmentImpl.invalidate(E);
901         throw E;
902     }
903     }
904
905     /**
906      * Javadoc for this public method is generated via
907      * the doc templates in the doc_src directory.
908      */

909     public EnvironmentConfig getConfig()
910         throws DatabaseException {
911
912     try {
913         checkHandleIsValid();
914         EnvironmentConfig config = environmentImpl.cloneConfig();
915         handleConfig.copyHandlePropsTo(config);
916         config.fillInEnvironmentGeneratedProps(environmentImpl);
917         return config;
918     } catch (Error JavaDoc E) {
919         environmentImpl.invalidate(E);
920         throw E;
921     }
922     }
923
924     /**
925      * Javadoc for this public method is generated via
926      * the doc templates in the doc_src directory.
927      */

928     public void setMutableConfig(EnvironmentMutableConfig mutableConfig)
929         throws DatabaseException {
930         
931     try {
932         checkHandleIsValid();
933         DatabaseUtil.checkForNullParam(mutableConfig, "mutableConfig");
934
935         /*
936          * Change the mutable properties specified in the given
937          * configuratation.
938          */

939         environmentImpl.setMutableConfig(mutableConfig);
940
941         /* Reset the handle config properties. */
942         copyToHandleConfig(mutableConfig, null);
943     } catch (Error JavaDoc E) {
944         environmentImpl.invalidate(E);
945         throw E;
946     }
947     }
948
949     /**
950      * Javadoc for this public method is generated via
951      * the doc templates in the doc_src directory.
952      */

953     public EnvironmentMutableConfig getMutableConfig()
954         throws DatabaseException {
955
956     try {
957         checkHandleIsValid();
958         EnvironmentMutableConfig config =
959         environmentImpl.cloneMutableConfig();
960         handleConfig.copyHandlePropsTo(config);
961         return config;
962     } catch (Error JavaDoc E) {
963         environmentImpl.invalidate(E);
964         throw E;
965     }
966     }
967
968     /**
969      * Not public yet, since there's nothing to upgrade.
970      */

971     void upgrade()
972         throws DatabaseException {
973
974         /* Do nothing. Nothing to upgrade yet. */
975     }
976
977     /**
978      * General stats
979      */

980
981     /**
982      * Javadoc for this public method is generated via
983      * the doc templates in the doc_src directory.
984      */

985     public EnvironmentStats getStats(StatsConfig config)
986         throws DatabaseException {
987
988     try {
989         StatsConfig useConfig =
990         (config == null) ? StatsConfig.DEFAULT : config;
991
992         if (environmentImpl != null) {
993         return environmentImpl.loadStats(useConfig);
994         } else {
995         return new EnvironmentStats();
996         }
997     } catch (Error JavaDoc E) {
998         environmentImpl.invalidate(E);
999         throw E;
1000    }
1001    }
1002
1003    /**
1004     * Javadoc for this public method is generated via
1005     * the doc templates in the doc_src directory.
1006     */

1007    public LockStats getLockStats(StatsConfig config)
1008        throws DatabaseException {
1009
1010    try {
1011        checkHandleIsValid();
1012        checkEnv();
1013        StatsConfig useConfig =
1014        (config == null) ? StatsConfig.DEFAULT : config;
1015
1016        return environmentImpl.lockStat(useConfig);
1017    } catch (Error JavaDoc E) {
1018        environmentImpl.invalidate(E);
1019        throw E;
1020    }
1021    }
1022
1023    /**
1024     * Javadoc for this public method is generated via
1025     * the doc templates in the doc_src directory.
1026     */

1027    public TransactionStats getTransactionStats(StatsConfig config)
1028        throws DatabaseException {
1029
1030    try {
1031        checkHandleIsValid();
1032        checkEnv();
1033        StatsConfig useConfig =
1034        (config == null) ? StatsConfig.DEFAULT : config;
1035        return environmentImpl.txnStat(useConfig);
1036    } catch (Error JavaDoc E) {
1037        environmentImpl.invalidate(E);
1038        throw E;
1039    }
1040    }
1041
1042    /**
1043     * Javadoc for this public method is generated via
1044     * the doc templates in the doc_src directory.
1045     */

1046    public List JavaDoc getDatabaseNames()
1047        throws DatabaseException {
1048
1049    try {
1050        checkHandleIsValid();
1051        checkEnv();
1052        return environmentImpl.getDbNames();
1053    } catch (Error JavaDoc E) {
1054        environmentImpl.invalidate(E);
1055        throw E;
1056    }
1057    }
1058
1059    /**
1060     * Javadoc for this public method is generated via
1061     * the doc templates in the doc_src directory.
1062     */

1063    public boolean verify(VerifyConfig config, PrintStream JavaDoc out)
1064        throws DatabaseException {
1065
1066    try {
1067        checkHandleIsValid();
1068        checkEnv();
1069        VerifyConfig useConfig =
1070        (config == null) ? VerifyConfig.DEFAULT : config;
1071        return environmentImpl.verify(useConfig, out);
1072    } catch (Error JavaDoc E) {
1073        environmentImpl.invalidate(E);
1074        throw E;
1075    }
1076    }
1077
1078    /**
1079     * Javadoc for this public method is generated via
1080     * the doc templates in the doc_src directory.
1081     */

1082    public Transaction getThreadTransaction()
1083    throws DatabaseException {
1084
1085    try {
1086        return (Transaction)
1087        environmentImpl.getTxnManager().getTxnForThread();
1088    } catch (Error JavaDoc E) {
1089        environmentImpl.invalidate(E);
1090        throw E;
1091    }
1092    }
1093
1094    /**
1095     * Javadoc for this public method is generated via
1096     * the doc templates in the doc_src directory.
1097     */

1098    public void setThreadTransaction(Transaction txn) {
1099
1100    try {
1101        environmentImpl.getTxnManager().setTxnForThread(txn);
1102    } catch (Error JavaDoc E) {
1103        environmentImpl.invalidate(E);
1104        throw E;
1105    }
1106    }
1107
1108    /*
1109     * Non public api -- helpers
1110     */

1111
1112    /*
1113     * Let the Environment remember what's opened against it.
1114     */

1115    void addReferringHandle(Database db) {
1116        referringDbs.add(db);
1117    }
1118
1119    /**
1120     * Let the Environment remember what's opened against it.
1121     */

1122    void addReferringHandle(Transaction txn) {
1123        referringDbTxns.add(txn);
1124    }
1125
1126    /**
1127     * The referring db has been closed.
1128     */

1129    void removeReferringHandle(Database db) {
1130        referringDbs.remove(db);
1131    }
1132
1133    /**
1134     * The referring Transaction has been closed.
1135     */

1136    void removeReferringHandle(Transaction txn) {
1137        referringDbTxns.remove(txn);
1138    }
1139
1140    /**
1141     * @throws DatabaseException if the environment is not open.
1142     */

1143    public void checkHandleIsValid()
1144        throws DatabaseException {
1145
1146        if (!valid) {
1147            throw new DatabaseException
1148                ("Attempt to use non-open Environment object().");
1149        }
1150    }
1151
1152    /*
1153     * Debugging aids.
1154     */

1155
1156    /**
1157     * Internal entrypoint.
1158     */

1159    EnvironmentImpl getEnvironmentImpl() {
1160        return environmentImpl;
1161    }
1162    
1163    /**
1164     * Throws if the environmentImpl is invalid.
1165     */

1166    protected void checkEnv()
1167        throws DatabaseException, RunRecoveryException {
1168
1169        if (environmentImpl == null) {
1170            return;
1171        }
1172        environmentImpl.checkIfInvalid();
1173        environmentImpl.checkNotClosed();
1174    }
1175}
1176
Popular Tags