Commit commits the transaction.
(ctx context.Context)
| 178 | |
| 179 | // Commit commits the transaction. |
| 180 | func (tx *dbTx) Commit(ctx context.Context) error { |
| 181 | if tx.closed { |
| 182 | return ErrTxClosed |
| 183 | } |
| 184 | |
| 185 | commandSQL := "commit" |
| 186 | if tx.commitQuery != "" { |
| 187 | commandSQL = tx.commitQuery |
| 188 | } |
| 189 | |
| 190 | commandTag, err := tx.conn.Exec(ctx, commandSQL) |
| 191 | tx.closed = true |
| 192 | if err != nil { |
| 193 | if tx.conn.PgConn().TxStatus() != 'I' { |
| 194 | _ = tx.conn.Close(ctx) // already have error to return |
| 195 | } |
| 196 | return err |
| 197 | } |
| 198 | if commandTag.String() == "ROLLBACK" { |
| 199 | return ErrTxCommitRollback |
| 200 | } |
| 201 | |
| 202 | return nil |
| 203 | } |
| 204 | |
| 205 | // Rollback rolls back the transaction. Rollback will return ErrTxClosed if the |
| 206 | // Tx is already closed, but is otherwise safe to call multiple times. Hence, a |