1<!--$Id: txn_begin.so,v 10.80 2006/12/13 18:09:09 bostic Exp $--> 2<!--Copyright (c) 1997,2008 Oracle. All rights reserved.--> 3<!--See the file LICENSE for redistribution information.--> 4<html> 5<head> 6<title>Berkeley DB: DbEnv::txn_begin</title> 7<meta name="description" content="Berkeley DB: An embedded database programmatic toolkit."> 8<meta name="keywords" content="embedded,database,programmatic,toolkit,btree,hash,hashing,transaction,transactions,locking,logging,access method,access methods,Java,C,C++"> 9</head> 10<body bgcolor=white> 11<table width="100%"><tr valign=top> 12<td> 13<b>DbEnv::txn_begin</b> 14</td> 15<td align=right> 16<a href="/api_cxx/api_core.html"><img src="/images/api.gif" alt="API"></a> 17<a href="/ref/toc.html"><img src="/images/ref.gif" alt="Ref"></a></td> 18</tr></table> 19<hr size=1 noshade> 20<tt> 21<b><pre> 22#include <db_cxx.h> 23<p> 24int 25DbEnv::txn_begin(DbTxn *parent, DbTxn **tid, u_int32_t flags); 26</pre></b> 27<hr size=1 noshade> 28<b>Description: DbEnv::txn_begin</b> 29<p>The DbEnv::txn_begin method creates a new transaction in the environment 30and copies a pointer to a <a href="/api_cxx/txn_class.html">DbTxn</a> that uniquely identifies it into 31the memory to which <b>tid</b> refers. Calling the <a href="/api_cxx/txn_abort.html">DbTxn::abort</a>, 32<a href="/api_cxx/txn_commit.html">DbTxn::commit</a> or <a href="/api_cxx/txn_discard.html">DbTxn::discard</a> methods will discard the returned 33handle.</p> 34<p><b>Note: Transactions may only span threads if they do so serially; 35that is, each transaction must be active in only a single thread 36of control at a time. This restriction holds for parents of nested 37transactions as well; no two children may be concurrently active in 38more than one thread of control at any one time.</b></p> 39<p><b>Note: Cursors may not span transactions; that is, each cursor must be 40opened and closed within a single transaction.</b></p> 41<p><b>Note: A parent transaction may not issue any Berkeley DB operations -- except for 42DbEnv::txn_begin, <a href="/api_cxx/txn_abort.html">DbTxn::abort</a> and <a href="/api_cxx/txn_commit.html">DbTxn::commit</a> -- while it has 43active child transactions (child transactions that have not yet been 44committed or aborted).</b></p> 45<p>The DbEnv::txn_begin method 46either returns a non-zero error value 47or throws an exception that encapsulates a non-zero error value on 48failure, and returns 0 on success. 49</p> 50<b>Parameters</b> <br> 51 <b>flags</b><ul compact><li>The <b>flags</b> parameter must be set to 0 or by bitwise inclusively <b>OR</b>'ing together one 52or more of the following values: 53<br> 54<b><a name="DB_READ_COMMITTED">DB_READ_COMMITTED</a></b><ul compact><li>This transaction will have degree 2 isolation. This provides for cursor 55stability but not repeatable reads. Data items which have been 56previously read by this transaction may be deleted or modified by other 57transactions before this transaction completes.</ul> 58<b><a name="DB_READ_UNCOMMITTED">DB_READ_UNCOMMITTED</a></b><ul compact><li>This transaction will have degree 1 isolation. Read operations 59performed by the transaction may read modified but not yet committed 60data. Silently ignored if the DB_READ_UNCOMMITTED flag was not 61specified when the underlying database was opened.</ul> 62<b><a name="DB_TXN_NOSYNC">DB_TXN_NOSYNC</a></b><ul compact><li>Do not synchronously flush the log when this transaction commits or 63prepares. This means the transaction will exhibit the ACI (atomicity, 64consistency, and isolation) properties, but not D (durability); that is, 65database integrity will be maintained but it is possible that this 66transaction may be undone during recovery. 67<p>This behavior may be set for a Berkeley DB environment using the 68<a href="/api_cxx/env_set_flags.html">DbEnv::set_flags</a> method. Any value specified to this method overrides 69that setting.</p></ul> 70<b><a name="DB_TXN_NOWAIT">DB_TXN_NOWAIT</a></b><ul compact><li>If a lock is unavailable for any Berkeley DB operation performed in the context 71of this transaction, cause the operation to 72either return <a href="/ref/program/errorret.html#DB_LOCK_DEADLOCK">DB_LOCK_DEADLOCK</a> or throw a 73<a href="/api_cxx/deadlock_class.html">DbDeadlockException</a> exception (or return 74<a href="/ref/program/errorret.html#DB_LOCK_NOTGRANTED">DB_LOCK_NOTGRANTED</a> or throw a <a href="/api_cxx/lockng_class.html">DbLockNotGrantedException</a> 75exception if the database environment has been configured using the 76<a href="/api_cxx/env_set_flags.html#DB_TIME_NOTGRANTED">DB_TIME_NOTGRANTED</a> flag). 77<p>This behavior may be set for a Berkeley DB environment using the 78<a href="/api_cxx/env_set_flags.html">DbEnv::set_flags</a> method. Any value specified to this method overrides 79that setting.</p></ul> 80<b><a name="DB_TXN_SNAPSHOT">DB_TXN_SNAPSHOT</a></b><ul compact><li>This transaction will execute with <a href="/ref/transapp/read.html">snapshot isolation</a>. For databases with the <a href="/api_cxx/db_open.html#DB_MULTIVERSION">DB_MULTIVERSION</a> 81flag set, data values will be read as they are when the transaction 82begins, without taking read locks. Silently ignored for operations on 83databases with <a href="/api_cxx/db_open.html#DB_MULTIVERSION">DB_MULTIVERSION</a> not set on the underlying 84database (read locks are acquired). 85<p>The error <a name="DB_LOCK_DEADLOCK">DB_LOCK_DEADLOCK</a> will be returned from update 86operations if a snapshot transaction attempts to update data 87which was modified after the snapshot transaction read it.</p></ul> 88<b><a name="DB_TXN_SYNC">DB_TXN_SYNC</a></b><ul compact><li>Synchronously flush the log when this transaction commits or prepares. 89This means the transaction will exhibit all of the ACID (atomicity, 90consistency, isolation, and durability) properties. 91<p>This behavior is the default for Berkeley DB environments unless the 92DB_TXN_NOSYNC flag was specified to the 93<a href="/api_cxx/env_set_flags.html">DbEnv::set_flags</a> method. Any value specified to this method overrides 94that setting.</p></ul> 95<b><a name="DB_TXN_WAIT">DB_TXN_WAIT</a></b><ul compact><li>If a lock is unavailable for any Berkeley DB operation performed in the context 96of this transaction, wait for the lock. 97<p>This behavior is the default for Berkeley DB environments unless the 98DB_TXN_NOWAIT flag was specified to the 99<a href="/api_cxx/env_set_flags.html">DbEnv::set_flags</a> method. Any value specified to this method overrides 100that setting.</p></ul> 101<b><a name="DB_TXN_WRITE_NOSYNC">DB_TXN_WRITE_NOSYNC</a></b><ul compact><li>Write, but do not synchronously flush, the log when this transaction 102commits. This means the transaction will exhibit the ACI (atomicity, 103consistency, and isolation) properties, but not D (durability); that is, 104database integrity will be maintained, but if the system fails, it is 105possible some number of the most recently committed transactions may be 106undone during recovery. The number of transactions at risk is governed 107by how often the system flushes dirty buffers to disk and how often the 108log is flushed or checkpointed. 109<p>This behavior may be set for a Berkeley DB environment using the 110<a href="/api_cxx/env_set_flags.html">DbEnv::set_flags</a> method. Any value specified to this method overrides 111that setting.</p></ul> 112<br></ul> 113 <b>parent</b><ul compact><li>If the <b>parent</b> parameter is non-NULL, the new transaction will 114be a nested transaction, with the transaction indicated by 115<b>parent</b> as its parent. Transactions may be nested to any level. 116In the presence of distributed transactions and two-phase commit, only 117the parental transaction, that is a transaction without a <b>parent</b> 118specified, should be passed as an parameter to <a href="/api_cxx/txn_prepare.html">DbTxn::prepare</a>.</ul> 119<br> 120<br><b>Errors</b> 121<p>If the maximum number of concurrent transactions has been reached, the DbEnv::txn_begin method will fail and 122either return ENOMEM or 123throw a DbMemoryException.</p> 124<hr size=1 noshade> 125<br><b>Class</b> 126<a href="/api_cxx/env_class.html">DbEnv</a>, <a href="/api_cxx/txn_class.html">DbTxn</a> 127<br><b>See Also</b> 128<a href="/api_cxx/txn_list.html">Transaction Subsystem and Related Methods</a> 129</tt> 130<table width="100%"><tr><td><br></td><td align=right> 131<a href="/api_cxx/api_core.html"><img src="/images/api.gif" alt="API"></a><a href="/ref/toc.html"><img src="/images/ref.gif" alt="Ref"></a> 132</td></tr></table> 133<p><font size=1>Copyright (c) 1996,2008 Oracle. All rights reserved.</font> 134</body> 135</html> 136