Commit implements commit method of two-phase commit worker.
(ctx context.Context, wb twopc.WriteBatch)
| 171 | |
| 172 | // Commit implements commit method of two-phase commit worker. |
| 173 | func (s *Storage) Commit(ctx context.Context, wb twopc.WriteBatch) (result interface{}, err error) { |
| 174 | el, ok := wb.(*ExecLog) |
| 175 | |
| 176 | if !ok { |
| 177 | err = errors.New("unexpected WriteBatch type") |
| 178 | return |
| 179 | } |
| 180 | |
| 181 | s.Lock() |
| 182 | defer s.Unlock() |
| 183 | |
| 184 | if s.tx != nil { |
| 185 | if equalTxID(&s.id, &TxID{el.ConnectionID, el.SeqNo, el.Timestamp}) { |
| 186 | // get last insert id and affected rows result |
| 187 | execResult := ExecResult{} |
| 188 | |
| 189 | for _, q := range s.queries { |
| 190 | // convert arguments types |
| 191 | args := make([]interface{}, len(q.Args)) |
| 192 | |
| 193 | for i, v := range q.Args { |
| 194 | args[i] = v |
| 195 | } |
| 196 | |
| 197 | var res sql.Result |
| 198 | res, err = s.tx.ExecContext(ctx, q.Pattern, args...) |
| 199 | |
| 200 | if err != nil { |
| 201 | log.WithError(err).Debug("commit query failed") |
| 202 | s.tx.Rollback() |
| 203 | s.tx = nil |
| 204 | s.queries = nil |
| 205 | return |
| 206 | } |
| 207 | |
| 208 | lastInsertID, _ := res.LastInsertId() |
| 209 | rowsAffected, _ := res.RowsAffected() |
| 210 | |
| 211 | execResult.LastInsertID = lastInsertID |
| 212 | execResult.RowsAffected += rowsAffected |
| 213 | } |
| 214 | |
| 215 | s.tx.Commit() |
| 216 | s.tx = nil |
| 217 | s.queries = nil |
| 218 | result = execResult |
| 219 | |
| 220 | return |
| 221 | } |
| 222 | |
| 223 | err = fmt.Errorf("twopc: inconsistent state, currently in tx: "+ |
| 224 | "conn = %d, seq = %d, time = %d", s.id.ConnectionID, s.id.SeqNo, s.id.Timestamp) |
| 225 | return |
| 226 | } |
| 227 | |
| 228 | err = errors.New("twopc: tx not prepared") |
| 229 | return |
| 230 | } |
nothing calls this directly
no test coverage detected