cockroach icon indicating copy to clipboard operation
cockroach copied to clipboard

sql: txn status becomes incorrect after executed "commit;begin"

Open yznming opened this issue 10 years ago • 3 comments

txn status becomes incorrect after executed "commit;begin"

root@:26257> commit;begin;
Now adding input for a multi-line SQL transaction client-side.
Press Enter two times to send the SQL text collected so far to the server, or Ctrl+C to cancel.
          -> 
pq: there is no transaction in progress
root@:26257> select 1;
+---+
| 1 |
+---+
| 1 |
+---+
(1 row)
root@:26257> select b;
pq: column name "b" not found
root@:26257> select 1;
+---+
| 1 |
+---+
| 1 |
+---+
(1 row)
root@:26257> commit;
pq: there is no transaction in progress

yznming avatar Nov 17 '16 15:11 yznming

@yznming I have troubleshooted this and I think there is no issue, everything works as intended. Perhaps we are not doing a good job at reporting the transaction status (see #10290) but I believe there is no error. Let me explain:

  1. initially, there is no transaction.
  2. you enter commit; begin. The CLI shell starts a multi-line editor. The two statements commit;begin are not sent to the server yet! there is still no transaction.
  3. you press the "enter" key two times. This causes the shell to send commit;begin to the server. They are sent as one string/statement! So the server sees commit;begin arriving in one string.
  4. the server processes commit; first. This fails, because there is no transaction. So the server produces an error.
  5. because the server has produced an error, the server does not look further into the string it has received. In other words, because of the error in commit, the server does not process begin that was just afterwards.
  6. the commit error is reported to the client: there is no transaction in progress. At this point, because there was an error and begin was skipped, there is still no transaction in progress on the server.
  7. you enter "select 1". This is sent to the server and processed in a spontaneous, single-statement transaction. After this completes and produces a result, there is still no transaction in progress on the server.
  8. you enter "select b". This is sent to the server, again processed in a spontaneous single-statement transaction. After this completes and produces an error, there is still no transaction in progress on the server.
  9. you enter "select 1", same happens as step 7 above.
  10. you enter "commit". The server receives this, but there is no transaction in progress, so commit fails again with there is no transaction in progress.

The situation would have been different if you had placed commit and begin on different lines at the beginning.

Does this clarify?

knz avatar Nov 17 '16 15:11 knz

it's clear now. thank you :)

yznming avatar Nov 17 '16 15:11 yznming

You're welcome! Thanks for asking.

knz avatar Nov 17 '16 16:11 knz