質問編集履歴
2
出力ログを追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -153,4 +153,27 @@
|
|
153
153
|
|
154
154
|
}
|
155
155
|
|
156
|
+
```
|
157
|
+
|
158
|
+
下記のようなログの状態が正しいのですが
|
159
|
+
```
|
160
|
+
add-begin
|
161
|
+
add-1SELECT
|
162
|
+
add-2SELECT
|
163
|
+
add-3commit
|
164
|
+
del-bigin
|
165
|
+
del-1
|
166
|
+
del-2
|
167
|
+
del-3commit
|
168
|
+
```
|
169
|
+
|
170
|
+
たまに下記のようなログの状態になります。
|
171
|
+
```
|
172
|
+
add-bigin
|
173
|
+
del-bigin
|
174
|
+
del-1
|
175
|
+
add-1
|
176
|
+
add-cnt(1):rollback
|
177
|
+
del-2
|
178
|
+
del-3commit
|
156
179
|
```
|
1
ソースの追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -2,4 +2,155 @@
|
|
2
2
|
|
3
3
|
このajaxの中でMysqlのトランザクションの処理があるのですがリンクを連打するとトランザクションが効かない時があって困っています。
|
4
4
|
|
5
|
-
トランザクションが100%効くことはありえないのでしょうか?
|
5
|
+
トランザクションが100%効くことはありえないのでしょうか?
|
6
|
+
|
7
|
+
|
8
|
+
addとcancelを交互に実行しています。
|
9
|
+
|
10
|
+
```php
|
11
|
+
public function add()
|
12
|
+
{
|
13
|
+
|
14
|
+
// 記事ID取得
|
15
|
+
$article_id = $this->_get_article_id();
|
16
|
+
if (!$article_id) {
|
17
|
+
return;
|
18
|
+
}
|
19
|
+
|
20
|
+
// セッションID取得
|
21
|
+
$session_id = $this->_get_session_id();
|
22
|
+
if (!$session_id) {
|
23
|
+
return;
|
24
|
+
}
|
25
|
+
|
26
|
+
// トランザクション開始
|
27
|
+
$this->model->begin();
|
28
|
+
$query_log = $this->model->getDataSource()->getLog();
|
29
|
+
$this->log('add-'.$query_log['log'][0]['query'],LOG_DEBUG);
|
30
|
+
|
31
|
+
// 既に同じ人で同じ記事IDで登録されていないかチェック
|
32
|
+
$count = $this->model->find('count', array(
|
33
|
+
'fields' => array('id'),
|
34
|
+
'conditions' => array(
|
35
|
+
'article_id' => $article_id,
|
36
|
+
'session' => $session_id,
|
37
|
+
),
|
38
|
+
));
|
39
|
+
$query_log = $this->model->getDataSource()->getLog();
|
40
|
+
$this->log('add-1'.$query_log['log'][0]['query'],LOG_DEBUG);
|
41
|
+
|
42
|
+
if ($count >= 1) {
|
43
|
+
// ロールバック
|
44
|
+
$this->model->rollback();
|
45
|
+
$query_log = $this->model->getDataSource()->getLog();
|
46
|
+
$this->log('add-cnt(1):'.$query_log['log'][0]['query'],LOG_DEBUG);
|
47
|
+
$this->_error('failed added article_id');
|
48
|
+
return;
|
49
|
+
}
|
50
|
+
|
51
|
+
// save用配列作成
|
52
|
+
$data['action_datetime'] = date('Y/m/d H:i:s');
|
53
|
+
$data['article_id'] = $article_id;
|
54
|
+
$data['session'] = $session_id;
|
55
|
+
$data['ip_address'] = env('REMOTE_ADDR');
|
56
|
+
$data['user_agent'] = env('HTTP_USER_AGENT');
|
57
|
+
|
58
|
+
// データ保存
|
59
|
+
if ($this->model->save($data)) {
|
60
|
+
$this->log("add article_id: $article_id", 'article_useful');
|
61
|
+
$query_log = $this->model->getDataSource()->getLog();
|
62
|
+
$this->log('add-2'.$query_log['log'][0]['query'],LOG_DEBUG);
|
63
|
+
|
64
|
+
// コミット
|
65
|
+
$this->model->commit();
|
66
|
+
$query_log = $this->model->getDataSource()->getLog();
|
67
|
+
$this->log('add-3'.$query_log['log'][0]['query'],LOG_DEBUG);
|
68
|
+
|
69
|
+
} else {
|
70
|
+
$query_log = $this->model->getDataSource()->getLog();
|
71
|
+
$this->log('add-4'.$query_log['log'][0]['query'],LOG_DEBUG);
|
72
|
+
// ロールバック
|
73
|
+
$this->model->rollback();
|
74
|
+
$query_log = $this->model->getDataSource()->getLog();
|
75
|
+
$this->log('add-5'.$query_log['log'][0]['query'],LOG_DEBUG);
|
76
|
+
debug('save failed');
|
77
|
+
exit;
|
78
|
+
}
|
79
|
+
|
80
|
+
$this->set('result', true);
|
81
|
+
$this->set('message', "add ok");
|
82
|
+
$this->set('_serialize', array('result', 'message'));
|
83
|
+
|
84
|
+
}
|
85
|
+
public function cancel()
|
86
|
+
{
|
87
|
+
|
88
|
+
// 記事ID取得
|
89
|
+
$article_id = $this->_get_article_id();
|
90
|
+
if (!$article_id) {
|
91
|
+
return;
|
92
|
+
}
|
93
|
+
|
94
|
+
// セッションID取得
|
95
|
+
$session_id = $this->_get_session_id();
|
96
|
+
if (!$session_id) {
|
97
|
+
return;
|
98
|
+
}
|
99
|
+
|
100
|
+
// トランザクション開始
|
101
|
+
$this->model->begin();
|
102
|
+
$query_log = $this->model->getDataSource()->getLog();
|
103
|
+
$this->log('del-'.$query_log['log'][0]['query'],LOG_DEBUG);
|
104
|
+
|
105
|
+
// 対象レコードのIDを取得するためにfindする
|
106
|
+
$data = $this->model->find('first', array(
|
107
|
+
'fields' => array('id'),
|
108
|
+
'conditions' => array(
|
109
|
+
'article_id' => $article_id,
|
110
|
+
'session' => $session_id,
|
111
|
+
),
|
112
|
+
));
|
113
|
+
$query_log = $this->model->getDataSource()->getLog();
|
114
|
+
$this->log('del-1'.$query_log['log'][0]['query'],LOG_DEBUG);
|
115
|
+
|
116
|
+
if (!$data) {
|
117
|
+
// ロールバック
|
118
|
+
$this->model->rollback();
|
119
|
+
$query_log = $this->model->getDataSource()->getLog();
|
120
|
+
$this->log('del-cnt(0):'.$query_log['log'][0]['query'],LOG_DEBUG);
|
121
|
+
$this->_error('delete target not found');
|
122
|
+
return;
|
123
|
+
}
|
124
|
+
|
125
|
+
// レコード削除
|
126
|
+
$id = $data['model']['id'];
|
127
|
+
if($this->model->delete($id)){
|
128
|
+
$query_log = $this->model->getDataSource()->getLog();
|
129
|
+
$this->log('del-2'.$query_log['log'][0]['query'],LOG_DEBUG);
|
130
|
+
|
131
|
+
// コミット
|
132
|
+
$this->model->commit();
|
133
|
+
$query_log = $this->model->getDataSource()->getLog();
|
134
|
+
$this->log('del-3'.$query_log['log'][0]['query'],LOG_DEBUG);
|
135
|
+
} else {
|
136
|
+
$query_log = $this->model->getDataSource()->getLog();
|
137
|
+
$this->log('del-4'.$query_log['log'][0]['query'],LOG_DEBUG);
|
138
|
+
|
139
|
+
// ロールバック
|
140
|
+
$this->model->rollback();
|
141
|
+
$query_log = $this->model->getDataSource()->getLog();
|
142
|
+
$this->log('del-5'.$query_log['log'][0]['query'],LOG_DEBUG);
|
143
|
+
debug('del failed');
|
144
|
+
exit;
|
145
|
+
|
146
|
+
}
|
147
|
+
|
148
|
+
|
149
|
+
$this->set('result', true);
|
150
|
+
$this->set('message', 'cancel ok');
|
151
|
+
|
152
|
+
$this->set('_serialize', array('result', 'message'));
|
153
|
+
|
154
|
+
}
|
155
|
+
|
156
|
+
```
|