/*- * See the file LICENSE for redistribution information. * * Copyright (c) 2009 Oracle. All rights reserved. * */ using System; using System.Collections.Generic; using System.Text; using BerkeleyDB.Internal; namespace BerkeleyDB { /// /// Represents errors that occur during Berkley DB operations. /// public class DatabaseException : Exception { /// /// The underlying error code from the Berkeley DB C library. /// public int ErrorCode; /// /// Throw an exception which corresponds to the specified Berkeley DB /// error code. /// /// The Berkeley DB error code public static void ThrowException(int err) { switch (err) { case 0: return; case ErrorCodes.DB_NOTFOUND: throw new NotFoundException(); case ErrorCodes.DB_BUFFER_SMALL: throw new MemoryException(); case ErrorCodes.DB_FOREIGN_CONFLICT: throw new ForeignConflictException(); case ErrorCodes.DB_KEYEMPTY: throw new KeyEmptyException(); case ErrorCodes.DB_KEYEXIST: throw new KeyExistException(); case ErrorCodes.DB_LOCK_DEADLOCK: throw new DeadlockException(); case ErrorCodes.DB_LOCK_NOTGRANTED: throw new LockNotGrantedException(); case ErrorCodes.DB_LOG_BUFFER_FULL: throw new FullLogBufferException(); case ErrorCodes.DB_OLD_VERSION: throw new OldVersionException(); case ErrorCodes.DB_PAGE_NOTFOUND: throw new PageNotFoundException(); case ErrorCodes.DB_REP_DUPMASTER: case ErrorCodes.DB_REP_HOLDELECTION: case ErrorCodes.DB_REP_IGNORE: case ErrorCodes.DB_REP_ISPERM: case ErrorCodes.DB_REP_JOIN_FAILURE: case ErrorCodes.DB_REP_NEWSITE: case ErrorCodes.DB_REP_NOTPERM: return; case ErrorCodes.DB_REP_LEASE_EXPIRED: throw new LeaseExpiredException(); case ErrorCodes.DB_RUNRECOVERY: throw new RunRecoveryException(); case ErrorCodes.DB_SECONDARY_BAD: throw new BadSecondaryException(); case ErrorCodes.DB_VERIFY_BAD: throw new VerificationException(); case ErrorCodes.DB_VERSION_MISMATCH: throw new VersionMismatchException(); default: throw new DatabaseException(err); } } /// /// Create a new DatabaseException, encapsulating a specific error code. /// /// The error code to encapsulate. public DatabaseException(int err) : base(libdb_csharp.db_strerror(err)) { ErrorCode = err; } } /// /// A secondary index has been corrupted. This is likely the result of an /// application operating on related databases without first associating /// them. /// public class BadSecondaryException : DatabaseException { /// /// Initialize a new instance of the BadSecondaryException /// public BadSecondaryException() : base(ErrorCodes.DB_SECONDARY_BAD) { } } /// /// /// public class ForeignConflictException : DatabaseException { /// /// Initialize a new instance of the ForeignConflictException /// public ForeignConflictException() : base(ErrorCodes.DB_FOREIGN_CONFLICT) { } } /// /// In-memory logs are configured and no more log buffer space is available. /// public class FullLogBufferException : DatabaseException { /// /// Initialize a new instance of the FullLogBufferException /// public FullLogBufferException() : base(ErrorCodes.DB_LOG_BUFFER_FULL) { } } /// /// The requested key/data pair logically exists but was never explicitly /// created by the application, or that the requested key/data pair was /// deleted and never re-created. In addition, the Queue access method will /// throw a KeyEmptyException for records that were created as part of a /// transaction that was later aborted and never re-created. /// /// /// The Recno and Queue access methods will automatically create key/data /// pairs under some circumstances. /// public class KeyEmptyException : DatabaseException { /// /// Initialize a new instance of the KeyEmptyException /// public KeyEmptyException() : base(ErrorCodes.DB_KEYEMPTY) { } } /// /// A key/data pair was inserted into the database using /// and the key already /// exists in the database, or using /// or /// and the key/data /// pair already exists in the database. /// public class KeyExistException : DatabaseException { /// /// Initialize a new instance of the KeyExistException /// public KeyExistException() : base(ErrorCodes.DB_KEYEXIST) { } } /// /// When multiple threads of control are modifying the database, there is /// normally the potential for deadlock. In Berkeley DB, deadlock is /// signified by a DeadlockException thrown from the Berkeley DB function. /// Whenever a Berkeley DB function throws a DeadlockException, the /// enclosing transaction should be aborted. /// public class DeadlockException : DatabaseException { /// /// Initialize a new instance of the DeadlockException /// public DeadlockException() : base(ErrorCodes.DB_LOCK_DEADLOCK) { } } /// /// The site's replication master lease has expired. /// public class LeaseExpiredException : DatabaseException { /// /// Initialize a new instance of the LeaseExpiredException /// public LeaseExpiredException() : base(ErrorCodes.DB_REP_LEASE_EXPIRED) { } } /// /// If is true, /// database calls timing out based on lock or transaction timeout values /// will throw a LockNotGrantedException, instead of a DeadlockException. /// public class LockNotGrantedException : DatabaseException { /// /// Initialize a new instance of the LockNotGrantedException /// public LockNotGrantedException() : base(ErrorCodes.DB_LOCK_NOTGRANTED) { } } internal class MemoryException : DatabaseException { /// /// Initialize a new instance of the MemoryException /// internal MemoryException() : base(ErrorCodes.DB_BUFFER_SMALL) { } } /// /// The requested key/data pair did not exist in the database or that /// start-of- or end-of-file has been reached by a cursor. /// public class NotFoundException : DatabaseException { /// /// Initialize a new instance of the NotFoundException /// public NotFoundException() : base(ErrorCodes.DB_NOTFOUND) { } } /// /// This version of Berkeley DB is unable to upgrade a given database. /// public class OldVersionException : DatabaseException { /// /// Initialize a new instance of the OldVersionException /// public OldVersionException() : base(ErrorCodes.DB_OLD_VERSION) { } } /// /// /// public class PageNotFoundException : DatabaseException { /// /// /// public PageNotFoundException() : base(ErrorCodes.DB_PAGE_NOTFOUND) { } } /// /// Berkeley DB has encountered an error it considers fatal to an entire /// environment. Once a RunRecoveryException has been thrown by any /// interface, it will be returned from all subsequent Berkeley DB calls /// made by any threads of control participating in the environment. /// /// /// An example of this type of fatal error is a corrupted database page. The /// only way to recover from this type of error is to have all threads of /// control exit the Berkeley DB environment, run recovery of the /// environment, and re-enter Berkeley DB. (It is not strictly necessary /// that the processes exit, although that is the only way to recover system /// resources, such as file descriptors and memory, allocated by /// Berkeley DB.) /// public class RunRecoveryException : DatabaseException { /// /// Initialize a new instance of the RunRecoveryException /// public RunRecoveryException() : base(ErrorCodes.DB_RUNRECOVERY) { } } /// /// Thrown by if a database is /// corrupted, and by if all /// key/data pairs in the file may not have been successfully output. /// public class VerificationException : DatabaseException { /// /// Initialize a new instance of the VerificationException /// public VerificationException() : base(ErrorCodes.DB_VERIFY_BAD) { } } /// /// The version of the Berkeley DB library doesn't match the version that /// created the database environment. /// public class VersionMismatchException : DatabaseException { /// /// Initialize a new instance of the VersionMismatchException /// public VersionMismatchException() : base(ErrorCodes.DB_VERSION_MISMATCH) { } } }