I have an object which is used to access a SQL Server table, do insert / updates.
I have two windows services, which use the same object to read/write to the same table.
To preserve the integrity, I was using Mutex lock and release on this object. But of late getting sync errors (Object synchronization method was called from an unsynchronized block of code). Hence I am adding a bool to identify the caller (In the code below bool isThreadOwnedLockAskingToRelease in 'Release' method). Will this resolve the sync issues and is this the correct way?
Class ST
public ST() //constructor
{
Mutex stBusy = utils.GetMutex(#"ST");
bool lockAcquired = false;
lock (ex)
{
//open connection
// build dataset
// close conn
}
}
// ...
public bool Lock(string nameOfLock)
{
if (stBusy != null)
{
if (stBusy.WaitOne(2000))
{
lockAcquired = true;
//...
}
}
return lockAcquired;
}
public void Release(string NameOfUnlocker, bool isThreadOwnedLockAskingToRelease)
{
if (stBusy != null && isThreadOwnedLockAskingToRelease)
{
stBusy.ReleaseMutex();
}
}
Class Service1:
ST st;
bool smlock = st.Lock("STLock");
if (smlock)
{
st = new ST(); // construct object
// do work
st.Release("STLock",smlock); // 2nd param added to identify the locker
}
Class Service2:
ST st1;
bool lockOwner = st1.Lock("QM");
st1= new StatusTable();
//Do work
// Unlock the status table
st1.Release("QM", lockOwner); // Addl. parameter added to fix sync issue
Related
I have an endpoint which runs a method. I want to ensure that method will not be executed while it's running when I hit the endpoint multiple times.
I imagine I can set a variable somewhere in the thread pool or something? I'm not sure how to go about investigating this.Some leads would be appreciated.
My example:
namespace Website.Import
{
public class ImportProducts : IHttpHandler
{
private static bool running = false;
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/json";
if (running)
{
throw new Exception("Already running");
}
running = true;
try
{
var repo = new MemoryRepository();
var productImporter = new ProductImporter(repo);
var groupImporter = new GroupImporter(repo);
var discountImporter = new DiscountImporter(repo);
}
finally
{
running = false;
}
context.Response.Write("Done");
}
public bool IsReusable
{
get
{
return true;
}
}
}
}
You can create a lock object to ensure the action doesn't run in parallel.
private static object lockObject = new object();
lock (lockObject)
{
// your code
}
There might be a problem though: this method will get locked for every user since static variables are shared across all sessions.
This behavior might be intentional. If you want to block execution for all sessions, this code is fine. If you want to block execution for only this user, allowing the method to run once at the same time per user, you have to put the object in the session storage.
If you just want to inform the client it is already running, you could also throw an exception:
private static bool alreadyExecuting = false;
if (alreadyExecuting)
{
throw new Exception("Already running");
}
alreadyExecuting = true;
try
{
// your code
}
finally
{
alreadyExecuting = false;
}
I am currently using the following approach for saving my data in multiple tables in database, which I am extracting from excel files.
public class Saver
{
public static int SaveCensusBatch(string key, ICollection<tbl_Life_Census> collection)
{
using (var db = new AuraEntities())
{
var entry = new tbl_Life_Master() { UUID = key, tbl_Life_Census = collection };
db.tbl_Life_Master.Add(entry);
db.SaveChanges();
return 1;
}
}
public static int SaveLifeData(string key2, ICollection<tbl_Life_General_Info> collection)
{
using (var db = new AuraEntities())
{
var entry = new tbl_Life_Master() { UUID = key2, tbl_Life_General_Info = collection };
db.tbl_Life_Master.Add(entry);
db.SaveChanges();
return 1;
}
}
public static T GetDBRecordByPK<T>(string key) where T : class
{
using (var db = new AuraEntities())
{
var t = db.Set<T>().Find(key);
return t;
}
}
}
Following is the code for calling this in main:
foreach (var r in results)
{
r.UUID = key.ToString();
}
Saver.SaveCensusBatch(key.ToString(), results);
Saver.SaveLifeData(key.ToString(), results3);
var master = Saver.GetDBRecordByPK<tbl_Life_Master>(key.ToString());
Please suggest me how can I do everything under one 'using block' and one function only instead of implementing several functions. This is because I have to insert data into 20-30 tables simultaneously.
You can do it by making all the Saver class' methods non-static (it also prevents keeping data in memory when you don't need it), and make it implement IDisposable interface. After that, you only need to follow the disposable pattern that Microsoft suggests (https://msdn.microsoft.com/en-us/library/b1yfkh5e%28v=vs.110%29.aspx), for example:
public class Saver : IDisposable
{
private readonly AuraEntities db;
private bool disposed;
public Saver()
{
db = new AuraEntities();
disposed = false;
}
public int SaveCensusBatch(string key, ICollection<tbl_Life_Census> collection)
{
var entry = new tbl_Life_Master() { UUID = key, tbl_Life_Census = collection };
db.tbl_Life_Master.Add(entry);
return 1;
}
public int SaveLifeData(string key2, ICollection<tbl_Life_General_Info> collection)
{
var entry = new tbl_Life_Master() { UUID = key2, tbl_Life_General_Info = collection };
db.tbl_Life_Master.Add(entry);
return 1;
}
public T GetDBRecordByPK<T>(string key) where T : class
{
var t = db.Set<T>().Find(key);
return t;
}
public void Save()
{
db.SaveChanges();
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposed)
{
return;
}
if (disposing && db != null)
{
db.Dispose();
disposed = true;
}
}
}
And then calling the methods within an using statement like this:
using(var saver = new Saver())
{
saver.SaveCensusBatch(key.ToString(), results);
saver.SaveLifeData(key.ToString(), results3);
saver.Save();
}
Updated: It is better if you save from outside the saver class. Because he whole process will behave as a single transaction, and it won't be persisted if an exception is raised.
You could select your results into a List and pass the list to the function. In that save function you could do your loop for each UUID and have the code for the three previous functions within the loop. Then call the db.SaveChanges() at the end outside the loop. The db.saveChanges() works as a transaction and would rollback if there was an error at any point during the save.
I have following method, which is called by different instances of my ReportGenerator class. The ReportGenerator class starts a single task, which accesses following method with an Interface to the class containing this method.
public IRapportBestilling GetNextReportOrderAndLock(DateTime nextTimeoutValue, string correlationId, Func<EstimatedReportSize?, int, bool> getPermission, int taskId)
{
this.ValidateReportOrderQueueTimeout(nextTimeoutValue, correlationId);
IRapportBestillingKoe reportOrderQueue;
try
{
using (var scope = new QueryEngineSessionScope())
{
--> Lock here bool allowLargeReports = getPermission.Invoke(EstimatedReportSize.RequestForLarge, taskId);
reportOrderQueue = this.rapportBestillingKoeRepository.GetNextReportOrderQueueItem(scope, correlationId, allowLargeReports, taskId);
reportOrderQueue.Laast = true;
reportOrderQueue.Timeout = nextTimeoutValue;
this.rapportBestillingKoeRepository.Save(reportOrderQueue, scope, correlationId);
scope.Complete();
--> Release lock getPermission.Invoke(reportOrderQueue.EstimeretRapportStoerelse, taskId);
var rep = this.rapportBestillingRepository.GetDomainObjectById(reportOrderQueue.RapportBestillingId, scope, correlationId);
rep.StorRapport = (reportOrderQueue.EstimeretRapportStoerelse == EstimatedReportSize.Large);
return rep;
}
}
}
I need to only allow a single task to be executing the code block in the method shown above. I have used Interlocked as well as Monitor class to handle this problem, but this is not working since this method is called on different instances of my class. Is there an approach to handle this problem ?.
You can do it with Monitor, just lock on a static object so that it's shared between all instances.
private static object _lock = new object();
public IRapportBestilling GetNextReportOrderAndLock(...)
{
...
using (var scope = new QueryEngineSessionScope())
{
lock(_lock)
{
...
}
}
}
I'm working in WinRt calling a WinRt Class Library from a Windows Store App using C# and SQLite... objects are returning null reference errors at unexpected times...
I'm trying to implement a logon service that checks to see if the current user is already logged in by checking if the data file exists, then checking if the user is the current user logged in...
The user can simply enter their ID and click a logon button. It creates a DataService object that wraps a SQLite database and is then "injected" into the UserStartupService.
The UserStartupService uses Dependency Injection, singleton and implements IDisposable.
Issue 1) if the user clicks the logon button a second time, the UserStartupService object constructor does not run and when internal objects are used they throw null reference errors even though it runs through the dispose method after it exits the using block, which forces me to deactivate the logon button, which is a kluge fix at best. A new user has to exit the program to log in as a new user. (The original code did not implement IAsyncOperationWithProgress, but that should not matter...)
Issue 2) I'm now trying to implement IAsyncOperationWithProgress to relay progress back to the UI and it gets a null reference error the instant it tries to use _dataFeedService on the line:
var json = await _dataFeedService.ValidateUser(userId);
Even though it runs the constructor at the top of the using statement as expected...
I think there is a scope/thread issue that I'm missing here. Maybe something obvious...
Any ideas? Thanks!
// logon button pressed...
private void LogOn_Click(object sender, RoutedEventArgs e)
{
// Create database service for DI
DataService _dataService = new DataService("MyData.sqlite");
// using statement for scope control
using (UserStartupService uss = UserStartupService.GetInstance(_dataService))
{
// progress bar...
CurrentProgress.Visibility = Windows.UI.Xaml.Visibility.Visible;
// create op and call...
IAsyncOperationWithProgress<string, int> op;
op = uss.SetUpUser(txtUserId.Text);
op.Progress = (info, progress) =>
{
CurrentProgress.Value = progress;
};
op.Completed = (info, status) =>
{
var results = info.GetResults();
// when completed...
if (status == AsyncStatus.Completed)
{
txtMessage.Text = "Current user data already loaded...";
CurrentProgress.Value = 100;
} // if cancelled...
else if (status == AsyncStatus.Canceled)
{
// Operation canceled - not implemented...
}
};
}
btnLogon.IsEnabled = false;
}
public sealed class UserStartupService : IDisposable
{
#region properties
// services
private static DataService _dataService;
private static DataFeedService _dataFeedService;
private static SqliteService _sqlMAFService;
private static SerialiseDeserialiseService _serializeService;
private string _token = String.Empty;
#endregion properties
#region constructors with DI and singleton pattern
// use this code to implement singleton patter...
// private constructor = can't instance without GetInstance...
private UserStartupService(DataService dataService)
{
// guard clause...
if (dataService == null)
{
throw new ArgumentNullException("DataService");
}
_dataService = dataService;
_dataFeedService = new DataFeedService();
_sqlMAFService = new SqliteService(_dataService);
_serializeService = new SerialiseDeserialiseService();
}
// implement singleton
public static UserStartupService GetInstance(DataService dataService)
{
_dataService = dataService;
return MyNestedSingletonClass.singleton;
}
class MyNestedSingletonClass
{
internal static readonly UserStartupService singleton = new UserStartupService(_dataService);
static MyNestedSingletonClass() { }
}
#endregion constructors with DI and singleton pattern
public IAsyncOperationWithProgress<string, int> SetUpUser(string userId)
{
return AsyncInfo.Run<string, int>((token, progress) =>
Task.Run<string>(async () =>
{
progress.Report(1);
try
{
// validate user against server REST feed and get token
var json = await _dataFeedService.ValidateUser(userId);
// ... it never gets here due to _dataFeedService null exception
// ...more code ... never gets here...
}
catch (Exception ex)
{
return ex.Message;
}
progress.Report(100);
return "";
}, token));
}
#region implement IDisposable
public void Dispose()
{
_serializeService = null;
_sqlMAFService.Dispose();
_sqlMAFService = null;
_dataFeedService.Dispose();
_dataFeedService = null;
_dataService.CloseConnection();
_dataService = null;
}
#endregion implement IDisposable
}
The using block will dispose uss before it is done executing, so that's where your null reference exceptions are coming from (for both issues).
If UserStartupService is a singleton, and it could be used multiple times, then don't dispose it.
Also, I would recommend using await rather than callbacks (it's usually simpler), so something like this should work:
private async void LogOn_Click(object sender, RoutedEventArgs e)
{
btnLogon.IsEnabled = false;
// Create database service for DI
DataService _dataService = new DataService("MyData.sqlite");
var uss = UserStartupService.GetInstance(_dataService);
// progress bar...
CurrentProgress.Visibility = Windows.UI.Xaml.Visibility.Visible;
// create op and call...
var op = uss.SetUpUser(txtUserId.Text)
.AsTask(progress => { CurrentProgress.Value = progress; });
var result = await op;
txtMessage.Text = "Current user data already loaded...";
CurrentProgress.Value = 100;
btnLogon.IsEnabled = true;
}
If I have an object that I would like to force to be accessed from within a lock, like so:
var obj = new MyObject();
lock (obj)
{
obj.Date = DateTime.Now;
obj.Name = "My Name";
}
Is it possible, from within the AddOne and RemoveOne functions to detect whether the current execution context is within a lock?
Something like:
Monitor.AreWeCurrentlyEnteredInto(this)
Edit: (for clarification of intent)
The intent here is to be able to reject any modification made outside of the lock, so that all changes to the object itself will be transactional and thread-safe. Locking on a mutex within the object itself does not ensure a transactional nature to the edits.
I know that it is possible to do this:
var obj = new MyObject();
obj.MonitorEnterThis();
try
{
obj.Date = DateTime.Now;
obj.Name = "My Name";
}
finally
{
obj.MonitorExitThis();
}
But this would allow any other thread to call the Add/Remove functions without first calling the Enter, thereby circumventing the protection.
Edit 2:
Here is what I'm currently doing:
var obj = new MyObject();
using (var mylock = obj.Lock())
{
obj.SetDate(DateTime.Now, mylock);
obj.SetName("New Name", mylock);
}
Which is simple enough, but it has two problems:
I'm implementing IDisposable on the
mylock object, which is a little bit
of an abuse of the IDisposable
interface.
I would like to change the SetDate and SetName functions to
Properties, for clarity.
I don't think that's possible without tracking the state yourself (e.g. by using some kind of semaphore). But even if it were, that'd be a gross violation of encapsulation. Your methods usually shouldn't care whether or not they're executing in a particular locking context.
There's no documented method of checking for this kind of condition at runtime, and if there were, I'd be suspicious of any code that used it, because any code that alters its behaviour based on the call stack would be very difficult to debug.
True ACID semantics are not trivial to implement, and I personally wouldn't try; that's what we have databases for, and you can use an in-memory database if you need the code to be fast/portable. If you just want forced-single-threaded semantics, that is a somewhat easier beast to tame, although as a disclaimer I should mention that in the long run you'd be better off simply providing atomic operations as opposed to trying to prevent multi-threaded access.
Let's suppose that you have a very good reason for wanting to do this. Here is a proof-of-concept class you could use:
public interface ILock : IDisposable
{
}
public class ThreadGuard
{
private static readonly object SlotMarker = new Object();
[ThreadStatic]
private static Dictionary<Guid, object> locks;
private Guid lockID;
private object sync = new Object();
public void BeginGuardedOperation()
{
lock (sync)
{
if (lockID == Guid.Empty)
throw new InvalidOperationException("Guarded operation " +
"was blocked because no lock has been obtained.");
object currentLock;
Locks.TryGetValue(lockID, out currentLock);
if (currentLock != SlotMarker)
{
throw new InvalidOperationException("Guarded operation " +
"was blocked because the lock was obtained on a " +
"different thread from the calling thread.");
}
}
}
public ILock GetLock()
{
lock (sync)
{
if (lockID != Guid.Empty)
throw new InvalidOperationException("This instance is " +
"already locked.");
lockID = Guid.NewGuid();
Locks.Add(lockID, SlotMarker);
return new ThreadGuardLock(this);
}
}
private void ReleaseLock()
{
lock (sync)
{
if (lockID == Guid.Empty)
throw new InvalidOperationException("This instance cannot " +
"be unlocked because no lock currently exists.");
object currentLock;
Locks.TryGetValue(lockID, out currentLock);
if (currentLock == SlotMarker)
{
Locks.Remove(lockID);
lockID = Guid.Empty;
}
else
throw new InvalidOperationException("Unlock must be invoked " +
"from same thread that invoked Lock.");
}
}
public bool IsLocked
{
get
{
lock (sync)
{
return (lockID != Guid.Empty);
}
}
}
protected static Dictionary<Guid, object> Locks
{
get
{
if (locks == null)
locks = new Dictionary<Guid, object>();
return locks;
}
}
#region Lock Implementation
class ThreadGuardLock : ILock
{
private ThreadGuard guard;
public ThreadGuardLock(ThreadGuard guard)
{
this.guard = guard;
}
public void Dispose()
{
guard.ReleaseLock();
}
}
#endregion
}
There's a lot going on here but I'll break it down for you:
Current locks (per thread) are held in a [ThreadStatic] field which provides type-safe, thread-local storage. The field is shared across instances of the ThreadGuard, but each instance uses its own key (Guid).
The two main operations are GetLock, which verifies that no lock has already been taken and then adds its own lock, and ReleaseLock, which verifies that the lock exists for the current thread (because remember, locks is ThreadStatic) and removes it if that condition is met, otherwise throws an exception.
The last operation, BeginGuardedOperation, is intended to be used by classes that own ThreadGuard instances. It's basically an assertion of sorts, it verifies that the currently-executed thread owns whichever lock is assigned to this ThreadGuard, and throws if the condition isn't met.
There's also an ILock interface (which doesn't do anything except derive from IDisposable), and a disposable inner ThreadGuardLock to implement it, which holds a reference to the ThreadGuard that created it and calls its ReleaseLock method when disposed. Note that ReleaseLock is private, so the ThreadGuardLock.Dispose is the only public access to the release function, which is good - we only want a single point of entry for acquisition and release.
To use the ThreadGuard, you would include it in another class:
public class MyGuardedClass
{
private int id;
private string name;
private ThreadGuard guard = new ThreadGuard();
public MyGuardedClass()
{
}
public ILock Lock()
{
return guard.GetLock();
}
public override string ToString()
{
return string.Format("[ID: {0}, Name: {1}]", id, name);
}
public int ID
{
get { return id; }
set
{
guard.BeginGuardedOperation();
id = value;
}
}
public string Name
{
get { return name; }
set
{
guard.BeginGuardedOperation();
name = value;
}
}
}
All this does is use the BeginGuardedOperation method as an assertion, as described earlier. Note that I'm not attempting to protect read-write conflicts, only multiple-write conflicts. If you want reader-writer synchronization then you'd need to either require the same lock for reading (probably not so good), use an additional lock in MyGuardedClass (the most straightforward solution) or alter the ThreadGuard to expose and acquire a true "lock" using the Monitor class (be careful).
And here's a test program to play with:
class Program
{
static void Main(string[] args)
{
MyGuardedClass c = new MyGuardedClass();
RunTest(c, TestNoLock);
RunTest(c, TestWithLock);
RunTest(c, TestWithDisposedLock);
RunTest(c, TestWithCrossThreading);
Console.ReadLine();
}
static void RunTest(MyGuardedClass c, Action<MyGuardedClass> testAction)
{
try
{
testAction(c);
Console.WriteLine("SUCCESS: Result = {0}", c);
}
catch (Exception ex)
{
Console.WriteLine("FAIL: {0}", ex.Message);
}
}
static void TestNoLock(MyGuardedClass c)
{
c.ID = 1;
c.Name = "Test1";
}
static void TestWithLock(MyGuardedClass c)
{
using (c.Lock())
{
c.ID = 2;
c.Name = "Test2";
}
}
static void TestWithDisposedLock(MyGuardedClass c)
{
using (c.Lock())
{
c.ID = 3;
}
c.Name = "Test3";
}
static void TestWithCrossThreading(MyGuardedClass c)
{
using (c.Lock())
{
c.ID = 4;
c.Name = "Test4";
ThreadPool.QueueUserWorkItem(s => RunTest(c, cc => cc.ID = 5));
Thread.Sleep(2000);
}
}
}
As the code (hopefully) implies, only the TestWithLock method completely succeeds. The TestWithCrossThreading method partially succeeds - the worker thread fails, but the main thread has no trouble (which, again, is the desired behaviour here).
This isn't intended to be production-ready code, but it should give you the basic idea of what has to be done in order to both (a) prevent cross-thread calls and (b) allow any thread to take ownership of the object as long as nothing else is using it.
Lets redisgn your class to make it actually work like transaction.
using (var transaction = account.BeginTransaction())
{
transaction.Name = "blah";
transaction.Date = DateTime.Now;
transaction.Comit();
}
Changes will not be propagated until commit is called.
In commit you can take a lock and set the properties on the target object.
You can override AddOne and RemoveOne to take a boolean flag that is set to true if it's being called from a lock. I don't see any other way.
You can also play with the ExecutionContext class if you want to know something about the current execution context. You can get the current context by calling ExecutionContext.Capture().
using thread local storage you can store the entering and exiting of a lock.
If your requirement is that the lock must be acquired for the duration of either method AddOne() or RemoveOne(), then why not simply acquire the lock inside each method? It shouldn't be a problem if the caller has already acquired the lock for you.
However, if your requirement is that the lock must be acquired before calling AddOne() and RemoveOne() together (because other concurrent operations performed on the instance are potentially unsafe), then maybe you should consider changing the public interface so that locking can be handled internally without concerning client code with the details.
One possible way to accomplish the later would be to provide methods for Begin- and End-Changes that have to be called before and after AddOne and RemoveOne. An exception should be raised if AddOne or RemoveOne is called outside of the Begin-End scope.
I ran into this same problem and created a helper class that looks like this:
public class BusyLock : IDisposable
{
private readonly Object _lockObject = new Object();
private int _lockCount;
public bool IsBusy
{
get { return _lockCount > 0; }
}
public IDisposable Enter()
{
if (!Monitor.TryEnter(_lockObject, TimeSpan.FromSeconds(1.0)))
throw new InvalidOperationException("Cannot begin operation as system is already busy");
Interlocked.Increment(ref _lockCount);
return this;
}
public bool TryEnter(out IDisposable busyLock)
{
if (Monitor.TryEnter(_lockObject))
{
busyLock = this;
Interlocked.Increment(ref _lockCount);
return true;
}
busyLock = null;
return false;
}
#region IDisposable Members
public void Dispose()
{
if (_lockCount > 0)
{
Monitor.Exit(_lockObject);
Interlocked.Decrement(ref _lockCount);
}
}
#endregion
}
You can then create an instance wrapped like this:
public sealed class AutomationManager
{
private readonly BusyLock _automationLock = new BusyLock();
public IDisposable AutomationLock
{
get { return _automationLock.Enter(); }
}
public bool IsBusy
{
get { return _automationLock.IsBusy; }
}
}
And use it like this:
public void DoSomething()
{
using (AutomationLock)
{
//Do important busy stuff here
}
}
For my particular case, I only wanted an enforcing lock (two threads shouldn't ever try to acquire the lock at the same time if they're well-behaved), so I throw an exception. You can easily modify it to perform more typical locking and still take advantage of the IsBusy.