Post by r***@gmail.comIn my understanding for calling the Db::put in a transaction,
1. Open an environment
2. Begin a transaction
3. Open the file in the transaction
4. call Db::put
5. commit/abort the transaction
6. Close the file.
7. close the environment.
Hope for calling the put operation in a transaction the file should be
opened in that transaction. Am i right?
While that's one option, it is not required. The DB handle can be
used in other transactions once the transaction containing the DB-
Post by r***@gmail.comopen() has been committed. Indeed, to quote the documentation for DB-
Calling DB->open is a relatively expensive operation,
and maintaining a set of open databases will normally
be preferable to repeatedly opening and closing the
database for each new query.
So the typical flow for a program doing more than a single operation
is something like:
(startup code)
1. Open an environment
2. Open all the DB handles with DB_AUTO_COMMIT
(alternatively, create a transaction and open them in that, then
commit it as the last step in the startup)
(operation code, performed many times)
3. Begin a transaction
4. call Db::put/get/del/cursor/etc
5. commit/abort the transaction
(shutdown code)
6. Close the DB handles
7. close the environment.
The exceptions would be programs that need to perform some other
operation atomicly with opening of the DB handle. For example, if you
kept a table that listed all the other tables in your system, you
would want to do something like:
1) begin transaction
2) open the table with DB_CREATE (and maybe DB_EXCL)
3) put a record into the other table showing that the new table exists
4) commit the transaction
If the open or put fails, you abort the transaction, rolling back the
create, and close the handle. If the system crashes between the open
and the commit, the create will be rolled back during recovery,
keeping everything consistent.
That's a special case, however, and most applications can avoid that
complexity and do all the opens up front.
Philip Guenther