質問編集履歴

2

出力ログを追加

2016/10/26 09:08

投稿

miKoTo0765
miKoTo0765

スコア35

test CHANGED
File without changes
test CHANGED
@@ -309,3 +309,49 @@
309
309
 
310
310
 
311
311
  ```
312
+
313
+
314
+
315
+ 下記のようなログの状態が正しいのですが
316
+
317
+ ```
318
+
319
+ add-begin
320
+
321
+ add-1SELECT
322
+
323
+ add-2SELECT
324
+
325
+ add-3commit
326
+
327
+ del-bigin
328
+
329
+ del-1
330
+
331
+ del-2
332
+
333
+ del-3commit
334
+
335
+ ```
336
+
337
+
338
+
339
+ たまに下記のようなログの状態になります。
340
+
341
+ ```
342
+
343
+ add-bigin
344
+
345
+ del-bigin
346
+
347
+ del-1
348
+
349
+ add-1
350
+
351
+ add-cnt(1):rollback
352
+
353
+ del-2
354
+
355
+ del-3commit
356
+
357
+ ```

1

ソースの追加

2016/10/26 09:08

投稿

miKoTo0765
miKoTo0765

スコア35

test CHANGED
File without changes
test CHANGED
@@ -7,3 +7,305 @@
7
7
 
8
8
 
9
9
  トランザクションが100%効くことはありえないのでしょうか?
10
+
11
+
12
+
13
+
14
+
15
+ addとcancelを交互に実行しています。
16
+
17
+
18
+
19
+ ```php
20
+
21
+ public function add()
22
+
23
+ {
24
+
25
+
26
+
27
+ // 記事ID取得
28
+
29
+ $article_id = $this->_get_article_id();
30
+
31
+ if (!$article_id) {
32
+
33
+ return;
34
+
35
+ }
36
+
37
+
38
+
39
+ // セッションID取得
40
+
41
+ $session_id = $this->_get_session_id();
42
+
43
+ if (!$session_id) {
44
+
45
+ return;
46
+
47
+ }
48
+
49
+
50
+
51
+ // トランザクション開始
52
+
53
+ $this->model->begin();
54
+
55
+ $query_log = $this->model->getDataSource()->getLog();
56
+
57
+ $this->log('add-'.$query_log['log'][0]['query'],LOG_DEBUG);
58
+
59
+
60
+
61
+ // 既に同じ人で同じ記事IDで登録されていないかチェック
62
+
63
+ $count = $this->model->find('count', array(
64
+
65
+ 'fields' => array('id'),
66
+
67
+ 'conditions' => array(
68
+
69
+ 'article_id' => $article_id,
70
+
71
+ 'session' => $session_id,
72
+
73
+ ),
74
+
75
+ ));
76
+
77
+ $query_log = $this->model->getDataSource()->getLog();
78
+
79
+ $this->log('add-1'.$query_log['log'][0]['query'],LOG_DEBUG);
80
+
81
+
82
+
83
+ if ($count >= 1) {
84
+
85
+ // ロールバック
86
+
87
+ $this->model->rollback();
88
+
89
+ $query_log = $this->model->getDataSource()->getLog();
90
+
91
+ $this->log('add-cnt(1):'.$query_log['log'][0]['query'],LOG_DEBUG);
92
+
93
+ $this->_error('failed added article_id');
94
+
95
+ return;
96
+
97
+ }
98
+
99
+
100
+
101
+ // save用配列作成
102
+
103
+ $data['action_datetime'] = date('Y/m/d H:i:s');
104
+
105
+ $data['article_id'] = $article_id;
106
+
107
+ $data['session'] = $session_id;
108
+
109
+ $data['ip_address'] = env('REMOTE_ADDR');
110
+
111
+ $data['user_agent'] = env('HTTP_USER_AGENT');
112
+
113
+
114
+
115
+ // データ保存
116
+
117
+ if ($this->model->save($data)) {
118
+
119
+ $this->log("add article_id: $article_id", 'article_useful');
120
+
121
+ $query_log = $this->model->getDataSource()->getLog();
122
+
123
+ $this->log('add-2'.$query_log['log'][0]['query'],LOG_DEBUG);
124
+
125
+
126
+
127
+ // コミット
128
+
129
+ $this->model->commit();
130
+
131
+ $query_log = $this->model->getDataSource()->getLog();
132
+
133
+ $this->log('add-3'.$query_log['log'][0]['query'],LOG_DEBUG);
134
+
135
+
136
+
137
+ } else {
138
+
139
+ $query_log = $this->model->getDataSource()->getLog();
140
+
141
+ $this->log('add-4'.$query_log['log'][0]['query'],LOG_DEBUG);
142
+
143
+ // ロールバック
144
+
145
+ $this->model->rollback();
146
+
147
+ $query_log = $this->model->getDataSource()->getLog();
148
+
149
+ $this->log('add-5'.$query_log['log'][0]['query'],LOG_DEBUG);
150
+
151
+ debug('save failed');
152
+
153
+ exit;
154
+
155
+ }
156
+
157
+
158
+
159
+ $this->set('result', true);
160
+
161
+ $this->set('message', "add ok");
162
+
163
+ $this->set('_serialize', array('result', 'message'));
164
+
165
+
166
+
167
+ }
168
+
169
+ public function cancel()
170
+
171
+ {
172
+
173
+
174
+
175
+ // 記事ID取得
176
+
177
+ $article_id = $this->_get_article_id();
178
+
179
+ if (!$article_id) {
180
+
181
+ return;
182
+
183
+ }
184
+
185
+
186
+
187
+ // セッションID取得
188
+
189
+ $session_id = $this->_get_session_id();
190
+
191
+ if (!$session_id) {
192
+
193
+ return;
194
+
195
+ }
196
+
197
+
198
+
199
+ // トランザクション開始
200
+
201
+ $this->model->begin();
202
+
203
+ $query_log = $this->model->getDataSource()->getLog();
204
+
205
+ $this->log('del-'.$query_log['log'][0]['query'],LOG_DEBUG);
206
+
207
+
208
+
209
+ // 対象レコードのIDを取得するためにfindする
210
+
211
+ $data = $this->model->find('first', array(
212
+
213
+ 'fields' => array('id'),
214
+
215
+ 'conditions' => array(
216
+
217
+ 'article_id' => $article_id,
218
+
219
+ 'session' => $session_id,
220
+
221
+ ),
222
+
223
+ ));
224
+
225
+ $query_log = $this->model->getDataSource()->getLog();
226
+
227
+ $this->log('del-1'.$query_log['log'][0]['query'],LOG_DEBUG);
228
+
229
+
230
+
231
+ if (!$data) {
232
+
233
+ // ロールバック
234
+
235
+ $this->model->rollback();
236
+
237
+ $query_log = $this->model->getDataSource()->getLog();
238
+
239
+ $this->log('del-cnt(0):'.$query_log['log'][0]['query'],LOG_DEBUG);
240
+
241
+ $this->_error('delete target not found');
242
+
243
+ return;
244
+
245
+ }
246
+
247
+
248
+
249
+ // レコード削除
250
+
251
+ $id = $data['model']['id'];
252
+
253
+ if($this->model->delete($id)){
254
+
255
+ $query_log = $this->model->getDataSource()->getLog();
256
+
257
+ $this->log('del-2'.$query_log['log'][0]['query'],LOG_DEBUG);
258
+
259
+
260
+
261
+ // コミット
262
+
263
+ $this->model->commit();
264
+
265
+ $query_log = $this->model->getDataSource()->getLog();
266
+
267
+ $this->log('del-3'.$query_log['log'][0]['query'],LOG_DEBUG);
268
+
269
+ } else {
270
+
271
+ $query_log = $this->model->getDataSource()->getLog();
272
+
273
+ $this->log('del-4'.$query_log['log'][0]['query'],LOG_DEBUG);
274
+
275
+
276
+
277
+ // ロールバック
278
+
279
+ $this->model->rollback();
280
+
281
+ $query_log = $this->model->getDataSource()->getLog();
282
+
283
+ $this->log('del-5'.$query_log['log'][0]['query'],LOG_DEBUG);
284
+
285
+ debug('del failed');
286
+
287
+ exit;
288
+
289
+
290
+
291
+ }
292
+
293
+
294
+
295
+
296
+
297
+ $this->set('result', true);
298
+
299
+ $this->set('message', 'cancel ok');
300
+
301
+
302
+
303
+ $this->set('_serialize', array('result', 'message'));
304
+
305
+
306
+
307
+ }
308
+
309
+
310
+
311
+ ```