1818
1919import flask
2020
21- # [START taskq-imp ]
21+ # [START gae_ndb_transactions_import ]
2222from google .appengine .api import taskqueue
2323from google .appengine .ext import ndb
24-
25- # [END taskq-imp]
24+ # [END gae_ndb_transactions_import]
2625
2726
2827class Note (ndb .Model ):
@@ -73,72 +72,70 @@ def main_page():
7372 return response
7473
7574
76- # [START standard ]
75+ # [START gae_ndb_transactions_insert_standard ]
7776@ndb .transactional
7877def insert_if_absent (note_key , note ):
7978 fetch = note_key .get ()
8079 if fetch is None :
8180 note .put ()
8281 return True
8382 return False
83+ # [END gae_ndb_transactions_insert_standard]
8484
8585
86- # [END standard]
87-
88-
89- # [START two-tries]
86+ # [START gae_ndb_transactions_insert_two_tries]
9087@ndb .transactional (retries = 1 )
9188def insert_if_absent_2_retries (note_key , note ):
9289 # do insert
93- # [END two-tries ]
90+ # [END gae_ndb_transactions_insert_two_tries ]
9491 fetch = note_key .get ()
9592 if fetch is None :
9693 note .put ()
9794 return True
9895 return False
9996
10097
101- # [START cross-group ]
98+ # [START gae_ndb_transactions_insert_cross_group ]
10299@ndb .transactional (xg = True )
103100def insert_if_absent_xg (note_key , note ):
104101 # do insert
105- # [END cross-group ]
102+ # [END gae_ndb_transactions_insert_cross_group ]
106103 fetch = note_key .get ()
107104 if fetch is None :
108105 note .put ()
109106 return True
110107 return False
111108
112109
113- # [START sometimes ]
110+ # [START gae_ndb_transactions_insert_sometimes ]
114111def insert_if_absent_sometimes (note_key , note ):
115112 # do insert
116- # [END sometimes ]
113+ # [END gae_ndb_transactions_insert_sometimes ]
117114 fetch = note_key .get ()
118115 if fetch is None :
119116 note .put ()
120117 return True
121118 return False
122119
123120
124- # [START indep ]
121+ # [START gae_ndb_transactions_insert_independent ]
125122@ndb .transactional (propagation = ndb .TransactionOptions .INDEPENDENT )
126123def insert_if_absent_indep (note_key , note ):
127124 # do insert
128- # [END indep ]
125+ # [END gae_ndb_transactions_insert_independent ]
129126 fetch = note_key .get ()
130127 if fetch is None :
131128 note .put ()
132129 return True
133130 return False
134131
135132
136- # [START taskq ]
133+ # [START gae_ndb_transactions_insert_task_queue ]
137134@ndb .transactional
138135def insert_if_absent_taskq (note_key , note ):
139136 taskqueue .add (url = flask .url_for ("taskq_worker" ), transactional = True )
140137 # do insert
141- # [END taskq ]
138+ # [END gae_ndb_transactions_insert_task_queue ]
142139 fetch = note_key .get ()
143140 if fetch is None :
144141 note .put ()
@@ -154,17 +151,17 @@ def taskq_worker():
154151def pick_random_insert (note_key , note ):
155152 choice = random .randint (0 , 5 )
156153 if choice == 0 :
157- # [START calling2 ]
154+ # [START gae_ndb_transactions_insert_standard_calling_2 ]
158155 inserted = insert_if_absent (note_key , note )
159- # [END calling2 ]
156+ # [END gae_ndb_transactions_insert_standard_calling_2 ]
160157 elif choice == 1 :
161158 inserted = insert_if_absent_2_retries (note_key , note )
162159 elif choice == 2 :
163160 inserted = insert_if_absent_xg (note_key , note )
164161 elif choice == 3 :
165- # [START sometimes-call ]
162+ # [START gae_ndb_transactions_insert_sometimes_callback ]
166163 inserted = ndb .transaction (lambda : insert_if_absent_sometimes (note_key , note ))
167- # [END sometimes-call ]
164+ # [END gae_ndb_transactions_insert_sometimes_callback ]
168165 elif choice == 4 :
169166 inserted = insert_if_absent_indep (note_key , note )
170167 elif choice == 5 :
@@ -183,10 +180,10 @@ def add_note():
183180 choice = random .randint (0 , 1 )
184181 if choice == 0 :
185182 # Use transactional function
186- # [START calling ]
183+ # [START gae_ndb_transactions_insert_standard_calling_1 ]
187184 note_key = ndb .Key (Note , note_title , parent = parent )
188185 note = Note (key = note_key , content = note_text )
189- # [END calling ]
186+ # [END gae_ndb_transactions_insert_standard_calling_1 ]
190187 if pick_random_insert (note_key , note ) is False :
191188 return 'Already there<br><a href="%s">Return</a>' % flask .url_for (
192189 "main_page" , page_name = page_name
0 commit comments