← Back to team overview

maria-developers team mailing list archive

Re: Help with running a transaction inside the server

 

Hi, Kristian!

On Feb 14, Kristian Nielsen wrote:
> Sergei Golubchik <serg@xxxxxxxxxxxx> writes:
> 
> > What file is it in? I didn't find it in sql_repl.cc
> 
> Sorry, it is rpl_slave_state::record_gtid() in sql/log_event.cc:
> 
>     http://bazaar.launchpad.net/~maria-captains/maria/10.0-mdev26/view/head:/sql/log_event.cc#L6140

Thanks. I tried to look at the source in launchpad, without branching,
and online sources are a bit difficult to grep :)

> > You could do all that in a single statement, and you won't need a
> > multi-statement transaction.
> 
> Right. In fact, I think perhaps I am doing it in a single statement already:
> 
>   mysql_reset_thd_for_next_command(thd, 0);
>   open_and_lock_tables(thd, &tlist, FALSE, 0);
>   table->file->ha_write_row(table->record[0]);
>   table->file->ha_rnd_pos_by_record(table->record[0]));
>   table->file->ha_delete_row(table->record[0]);
>   ha_commit_trans(thd, FALSE);
>   close_thread_tables(thd);
>   if (!in_transaction)
>     ha_commit_trans(thd, TRUE);
> 
> (The in_transaction flag is true when the code runs as part of a slave
> replicated transaction. It is false when there is no containing transaction,
> which is the case I am trying to get to work now).
> 
> Maybe that is all there is to it? I just thought I would need to somehow
> specify the start of the "transaction", even if it is just a single
> statement. Or maybe I need to set autocommit? I am always a little unsure
> about the logic around the all=TRUE_FALSE flag for commit and autocommit and
> how that works...

Yes, that is all there is to it. A transaction starts automatically on
the first "transaction-initiating statement" (according to the
standard).

You don't need to set autocommit. If you do - you won't need the second
ha_commit_trans(thd, TRUE). You current code, as far as I understand,
will work correctly, whether autocommit is on or off.

> I suppose I just need some general advice on how to do DML operations
> from within the server. What needs to be set up in the THD, what needs
> to be done at the start of transaction or autocommit statement, and
> what needs to be done at the end ...

Just do it as above.

In that case, though, your changes may be part of
the already running transaction - do you want that? Oh, hmm, a problem.
If you're in the middle of the already running transaction, you probably
shouldn't commit it after your changes, but wait till the end -
arbitrary adding commits in the middle of a transaction is no good.

But perhaps you cannot be in the middle of a running transaction when
you do record_gtid().

Regards,
Sergei


Follow ups

References