Writing Transactional Applications with Berkeley DB, Java Edition
Total Page:16
File Type:pdf, Size:1020Kb
Berkeley DB Transaction Processing for Java . Makers of Berkeley DB Legal Notice This documentation is distributed under the terms of the Sleepycat public license. You may review the terms of this license at: http://www.sleepycat.com/download/oslicense.html Sleepycat Software, Berkeley DB, Berkeley DB XML and the Sleepycat logo are trademarks or service marks of Sleepycat Software, Inc. All rights to these marks are reserved. No third-party use is permitted without the express prior written consent of Sleepycat Software, Inc. Java™ and all Java-based marks are a trademark or registered trademark of Sun Microsystems, Inc, in the United States and other countries. To obtain a copy of this document's original source code, please write to <[email protected]>. Published 12/20/2005 Table of Contents Preface .............................................................................................. iv Conventions Used in this Book ............................................................. iv For More Information .................................................................. v 1. Introduction ...................................................................................... 1 Transaction Benefits ......................................................................... 1 A Note on System Failure ............................................................. 2 Application Requirements ............................................................ 2 Multi-threaded and Multi-process Applications ................................... 4 Recoverability ................................................................................ 4 Performance Tuning ......................................................................... 5 2. Enabling Transactions ........................................................................... 6 Environments ................................................................................. 6 File Naming ............................................................................. 7 Specifying the Environment Home Directory ............................... 7 Specifying File Locations ....................................................... 7 Identifying Specific File Locations ............................................ 8 Error Support ........................................................................... 9 Shared Memory Regions ............................................................. 10 Regions Backed by Files ...................................................... 10 Regions Backed by Heap Memory ........................................... 10 Regions Backed by System Memory ......................................... 10 Security Considerations ............................................................. 11 Opening a Transactional Environment and Database ................................. 12 3. Transaction Basics ............................................................................. 15 Committing a Transaction ................................................................. 16 Non-Durable Transactions ........................................................... 18 Aborting a Transaction ..................................................................... 19 Auto Commit ................................................................................ 19 Nested Transactions ........................................................................ 21 Transactional Cursors ...................................................................... 21 Secondary Indices with Transaction Applications ...................................... 23 Configuring the Transaction Subsystem ................................................. 24 4. Concurrency .................................................................................... 27 Which JE Handles are Free-Threaded ................................................... 28 Locks, Blocks, and Deadlocks ............................................................. 28 Locks ................................................................................... 29 Lock Resources ................................................................. 29 Types of Locks ................................................................. 30 Lock Lifetime ................................................................... 30 Blocks .................................................................................. 30 Blocking and Application Performance ..................................... 31 Avoiding Blocks ................................................................. 32 Deadlocks .............................................................................. 33 The Locking Subsystem .................................................................... 34 Configuring the Locking Subsystem ................................................ 34 Configuring Deadlock Detection ................................................... 36 12/20/2005 Using Transactions with JE Page ii Resolving Deadlocks ................................................................. 38 Isolation ...................................................................................... 39 Supported Degrees of Isolation .................................................... 39 Reading Uncommitted Data ........................................................ 40 Committed Reads .................................................................... 43 Transactional Cursors and Concurrent Applications ................................... 45 Using Cursors with Uncommitted Data ............................................ 46 Read/Modify/Write ......................................................................... 47 No Wait on Blocks ........................................................................... 48 Reverse BTree Splits ....................................................................... 49 5. Managing JE Files .............................................................................. 51 Checkpoints .................................................................................. 51 Backup Procedures ......................................................................... 55 About Unix Copy Utilities ........................................................... 56 Offline Backups ....................................................................... 57 Hot Backup ............................................................................ 57 Incremental Backups ................................................................. 58 Recovery Procedures ....................................................................... 58 Normal Recovery ..................................................................... 59 Catastrophic Recovery ............................................................... 60 Designing Your Application for Recovery ................................................ 61 Recovery for Multi-Threaded Applications ....................................... 61 Recovery in Multi-Process Applications ........................................... 63 Effects of Multi-Process Recovery ........................................... 63 Process Registration ........................................................... 64 Using Hot Failovers ......................................................................... 64 Removing Log Files ......................................................................... 66 Configuring the Logging Subsystem ...................................................... 67 Setting the Log File Size ............................................................ 67 Configuring the Logging Region Size .............................................. 68 Configuring In-Memory Logging .................................................... 68 Setting the In-Memory Log Buffer Size ............................................ 70 6. Summary and Examples ....................................................................... 71 Anatomy of a Transactional Application ................................................ 71 Transaction Example ....................................................................... 72 TxnGuide.java ........................................................................ 73 PayloadData.java ..................................................................... 77 DBWriter.java ......................................................................... 78 In-Memory Transaction Example ......................................................... 84 12/20/2005 Using Transactions with JE Page iii Preface This document describes how to use transactions with your Berkeley DB, Java Edition applications. It is intended to describe how to transaction protect your application's data. The APIs used to perform this task are described here, as are the environment infrastructure and administrative tasks required by a transactional application. This book also describes multi-threaded and multi-process JE applications and the requirements they have for deadlock detection. This book is aimed at the software engineer responsible for writing a transactional JE application. This book assumes that you have already read and understood the concepts contained in Getting Started with Berkeley DB. Conventions Used in this Book The following typographical conventions are used within in this manual: Class names are represented in monospaced font, as are method names. For example: "The Environment() constructor returns an Environment class object." Variable or non-literal text is presented