Imported Upstream version 5.3.21
[platform/upstream/libdb.git] / lang / php_db4 / samples / transactional_counter.php
1 <?php
2
3 // Open a new Db4Env.  By default it is transactional.  The directory
4 // path in the open() call must exist.
5 $dbenv = new Db4Env();
6 $dbenv->set_data_dir("/var/tmp/dbhome");
7 $dbenv->open("/var/tmp/dbhome");
8
9 // Open a database in $dbenv.
10 $db = new Db4($dbenv);
11 $txn = $dbenv->txn_begin();
12 $db->open($txn, 'a', 'foo');
13 $txn->commit();
14
15 $counter = $db->get("counter");
16 // Create a new transaction
17 $txn = $dbenv->txn_begin();
18 if($txn == false) {
19   print "txn_begin failed";
20   exit;
21 }
22 print "Current value of counter is $counter\n";
23
24 // Increment and reset counter, protect it with $txn
25 $db->put("counter", $counter+1, $txn);
26
27 // Commit the transaction, otherwise the above put() will rollback.
28 $txn->commit();
29 // Sync for good measure
30 $db->sync();
31 // This isn't a real close, use _close() for that.
32 $db->close();
33 ?>