Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

transactional_counter.php

00001 <?php
00002 
00003 // Open a new Db4Env
00004 $dbenv = new Db4Env();
00005 $dbenv->set_data_dir("/var/lib/dbhome");
00006 $dbenv->open("/var/lib/dbhome/test");
00007 
00008 // Open a database in $dbenv.  Note that even though
00009 // we pass null in as the transaction, db4 forces this
00010 // operation to be transactionally protected, so PHP
00011 // will force auto-commit internally.
00012 $db = new Db4($dbenv);
00013 $db->open(null, 'a', 'foo');
00014 
00015 $counter = $db->get("counter");
00016 // Create a new transaction
00017 $txn = $dbenv->txn_begin();
00018 if($txn == false) {
00019   print "txn_begin failed";
00020   exit;
00021 }
00022 print "Current value of counter is $counter\n";
00023 
00024 // Increment and reset counter, protect it with $txn
00025 $db->put("counter", $counter+1, $txn);
00026 
00027 // Commit the transaction, otherwise the above put() will rollback.
00028 $txn->commit();
00029 // Sync for good measure
00030 $db->sync();
00031 // This isn't a real close, use _close() for that.
00032 $db->close();
00033 ?>

Generated on Sun Dec 25 12:14:43 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2