Skip to content

Can't START TRANSACTION ... READ ONLY #817

@taspeotis

Description

@taspeotis

Hi, thanks for the library. I appreciate your effort in giving first-class support to asynchronous operations.

I'd like to START TRANSACTION ... READ ONLY. Unfortunately it seems like nothing in MySqlConnection.BeginDbTransactionAsync will append READ ONLY to the START TRANSACTION command.

var isolationLevelValue = isolationLevel switch
{
	IsolationLevel.ReadUncommitted => "read uncommitted",
	IsolationLevel.ReadCommitted => "read committed",
	IsolationLevel.RepeatableRead => "repeatable read",
	IsolationLevel.Serializable => "serializable",
	IsolationLevel.Snapshot => "repeatable read",

	// "In terms of the SQL:1992 transaction isolation levels, the default InnoDB level is REPEATABLE READ." - http://dev.mysql.com/doc/refman/5.7/en/innodb-transaction-model.html
	IsolationLevel.Unspecified => "repeatable read",

	_ => throw new NotSupportedException("IsolationLevel.{0} is not supported.".FormatInvariant(isolationLevel))
};

using (var cmd = new MySqlCommand($"set session transaction isolation level {isolationLevelValue};", this))
{
	await cmd.ExecuteNonQueryAsync(ioBehavior, cancellationToken).ConfigureAwait(false);

	var consistentSnapshotText = isolationLevel == IsolationLevel.Snapshot ? " with consistent snapshot" : "";
	cmd.CommandText = $"start transaction{consistentSnapshotText};";
	await cmd.ExecuteNonQueryAsync(ioBehavior, cancellationToken).ConfigureAwait(false);
}

There's a corresponding READ WRITE option, too. The relevant MySQL documentation is here.

For now I'm eschewing MySqlTransaction in favour of managing it myself with commands.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions