Skip to content

Commit

Permalink
fixes error to acquire the lock
Browse files Browse the repository at this point in the history
  • Loading branch information
lillo42 committed Nov 29, 2024
1 parent 2de675b commit 2a3151d
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 12 deletions.
6 changes: 2 additions & 4 deletions src/Paramore.Brighter.Locking.MySql/MySqlLockingProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class MySqlLockingProvider(MySqlConnectionProvider connectionProvider) :
/// <returns>The id of the lock that has been acquired or null if no lock was able to be acquired</returns>
public async Task<string> ObtainLockAsync(string resource, CancellationToken cancellationToken)
{
if (!_connections.ContainsKey(resource))
if (_connections.ContainsKey(resource))
{
return null;
}
Expand All @@ -44,9 +44,7 @@ public async Task<string> ObtainLockAsync(string resource, CancellationToken can

command.Parameters.Add(new MySqlParameter("@TIMEOUT", MySqlDbType.UInt32)
{
// Infinity timeout
// MariaDB doesn't support -1 value, see: https://stackoverflow.com/questions/49792089/set-infinite-timeout-get-lock-in-mariadb/49809919
Value = 0xffffffff
Value = 1
});

var result = await command.ExecuteScalarAsync(cancellationToken) ?? -1;
Expand Down
4 changes: 2 additions & 2 deletions src/Paramore.Brighter.Locking.MySql/MySqlLockingQueries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public static class MySqlLockingQueries
{
public const string ObtainLockQuery = "SELECT GET_LOCK(@RESOURCE_NAME)";
public const string ObtainLockQuery = "SELECT GET_LOCK(@RESOURCE_NAME, @TIMEOUT)";

public const string ReleaseLockQuery = "SELECT RELEASE_LOCK(@RESOURCE_NAME, @TIMEOUT)";
public const string ReleaseLockQuery = "SELECT RELEASE_LOCK(@RESOURCE_NAME)";
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Data;
using System.Data.Common;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -11,22 +10,22 @@
namespace Paramore.Brighter.MySQL.Tests.LockingProvider;

[Trait("Category", "MySql")]
public class MsSqlLockingProviderTests
public class MySqlLockingProviderTests
{
private readonly MySqlTestHelper _msSqlTestHelper;

public MsSqlLockingProviderTests()
public MySqlLockingProviderTests()
{
_msSqlTestHelper = new MySqlTestHelper();
_msSqlTestHelper.SetupMessageDb();
}


[Fact]
public async Task GivenAMsSqlLockingProvider_WhenLockIsCalled_LockCanBeObtainedAndThenReleased()
public async Task GivenAMySqlLockingProvider_WhenLockIsCalled_LockCanBeObtainedAndThenReleased()
{
var provider = new MySqlLockingProvider((MySqlConnectionProvider)_msSqlTestHelper.ConnectionProvider);
var resource = "Sweeper";
const string resource = "Sweeper";

var result = await provider.ObtainLockAsync(resource, CancellationToken.None);

Expand Down Expand Up @@ -85,7 +84,7 @@ private async Task<DbConnection> ObtainLockForManualDisposal(string resource)
command.Parameters.Add(new MySqlParameter("@RESOURCE_NAME", MySqlDbType.String));
command.Parameters["@RESOURCE_NAME"].Value = resource;
command.Parameters.Add(new MySqlParameter("@TIMEOUT", MySqlDbType.UInt32));
command.Parameters["@TIMEOUT"].Value = -1;
command.Parameters["@TIMEOUT"].Value = 1;

var respone = await command.ExecuteScalarAsync(CancellationToken.None);

Expand Down

0 comments on commit 2a3151d

Please sign in to comment.