3232 * Performs a read-write transaction to update two sample records in the
3333 * database.
3434 *
35- * This will transfer 100 ,000 from the `MarketingBudget` field for the second
36- * Album to the first Album. If the `MarketingBudget` is too low, it will
37- * raise an exception.
35+ * This will transfer 200 ,000 from the `MarketingBudget` field for the second
36+ * Album to the first Album. If the `MarketingBudget` for the second Album is
37+ * too low, it will raise an exception.
3838 *
3939 * Before running this sample, you will need to run the `update_data` sample
4040 * to populate the fields.
@@ -53,6 +53,8 @@ function read_write_transaction($instanceId, $databaseId)
5353 $ database = $ instance ->database ($ databaseId );
5454
5555 $ database ->runTransaction (function (Transaction $ t ) use ($ spanner ) {
56+ $ transferAmount = 200000 ;
57+
5658 // Read the second album's budget.
5759 $ secondAlbumKey = [2 ,2 ];
5860 $ secondAlbumKeySet = $ spanner ->keySet (['keys ' => [$ secondAlbumKey ]]);
@@ -65,10 +67,10 @@ function read_write_transaction($instanceId, $databaseId)
6567
6668 $ firstRow = $ secondAlbumResult ->rows ()->current ();
6769 $ secondAlbumBudget = $ firstRow ['MarketingBudget ' ];
68- if ($ secondAlbumBudget < 300000 ) {
70+ if ($ secondAlbumBudget < $ transferAmount ) {
6971 // Throwing an exception will automatically roll back the transaction.
7072 throw new UnexpectedValueException (
71- 'The second album \'s budget doesn \' t meet the required minimum '
73+ 'The second album \'s budget is lower than the transfer amount: ' . $ transferAmount
7274 );
7375 }
7476
@@ -86,24 +88,21 @@ function read_write_transaction($instanceId, $databaseId)
8688 $ firstAlbumBudget = $ firstRow ['MarketingBudget ' ];
8789
8890 // Update the budgets.
89- $ transferAmount = 100000 ;
90- if ($ firstAlbumBudget >= $ transferAmount ) {
91- $ secondAlbumBudget += $ transferAmount ;
92- $ firstAlbumBudget -= $ transferAmount ;
93- printf ('Setting first album \'s budget to %s and the second album \'s ' .
94- 'budget to %s. ' . PHP_EOL , $ firstAlbumBudget , $ secondAlbumBudget );
91+ $ secondAlbumBudget -= $ transferAmount ;
92+ $ firstAlbumBudget += $ transferAmount ;
93+ printf ('Setting first album \'s budget to %s and the second album \'s ' .
94+ 'budget to %s. ' . PHP_EOL , $ firstAlbumBudget , $ secondAlbumBudget );
9595
96- // Update the rows.
97- $ t ->updateBatch ('Albums ' , [
98- ['SingerId ' => 1 , 'AlbumId ' => 1 , 'MarketingBudget ' => $ firstAlbumBudget ],
99- ['SingerId ' => 2 , 'AlbumId ' => 2 , 'MarketingBudget ' => $ secondAlbumBudget ],
100- ]);
96+ // Update the rows.
97+ $ t ->updateBatch ('Albums ' , [
98+ ['SingerId ' => 1 , 'AlbumId ' => 1 , 'MarketingBudget ' => $ firstAlbumBudget ],
99+ ['SingerId ' => 2 , 'AlbumId ' => 2 , 'MarketingBudget ' => $ secondAlbumBudget ],
100+ ]);
101101
102- // Commit the transaction!
103- $ t ->commit ();
102+ // Commit the transaction!
103+ $ t ->commit ();
104104
105- print ('Transaction complete. ' . PHP_EOL );
106- }
105+ print ('Transaction complete. ' . PHP_EOL );
107106 });
108107}
109108// [END spanner_read_write_transaction]
0 commit comments