locking

Hibernate Locking DB2 File

We're using Hibernate in our Spring web app to do reads, as well as DML with a DB2 database. This problem only occurs during an update. If we blow away the lock on the DB2 side, it appears that any updates are not syncing with the database (even though I do session.flush() and session.clear() after an update is performed). This really...

Python: single instance of program

So, is there a Pythonic way to have only one instance of program running? The only reasonable solution I've come up with is trying to run it as a server on some port, then second program trying to bind to same port - fails. But it's not really a great idea, maybe there's something more lightweight than this? (Take into consideration...

Downgrading ReaderWriterLockSlim UpgradeableReadLock to simple ReadLock

The documentation of ReaderWriterLockSlim.EnterUpgradeableReadLock says: A thread in upgradeable mode can downgrade to read mode or upgrade to write mode. How do I downgrade the lock to a read lock? The documentation does't tell... [Edit:] I'm not trying to get the write lock. I just want to downgrade the upgradeable lock to a rea...

Does using ReaderWriterLockSlim cause a memory barrier?

If I am using ReaderWriterLockSlim to acquire read/write locks, do I need to make my variables volatile or use Interlocked.Increment? For example, would the code in the Add method below work fine, or does it need enhancement? public class AppendableList<T> { // semi-immutable; supports appending only private T[] data = new T[16]; ...

Pessimistic lock in T-SQL

If i SELECT a row for updating in MS SQL Server, and want to have it locked till i either update or cancel, which option is better :- 1) Use a query hint like UPDLOCK 2) Use REPEATABLE READ isolation level for the transaction 3) any other option. Thanks, Chak. ...

How to find what state ManualResetEvent is in?

I am using an instance of ManualResetEvent to control thread access to a resource but I'm running into problems with it. Does anyone know how I can find out during debugging what the state of the object is? That is to say I would like to know if the ManualResetEvent is currently blocking any threads and maybe even how many and which th...

Select unlocked row in Postgresql

Is there a way to select rows in Postgresql that aren't locked? I have a multi-threaded app that will do: Select... order by id desc limit 1 for update on a table. If multiple threads run this query, they both try to pull back the same row. One gets the row lock, the other blocks and then fails after the first one updates the row....

Re-entrant locks in C#

Will the following code result in a deadlock using C# on .NET? class MyClass { private object lockObj = new object(); public void Foo() { lock(lockObj){ Bar(); } } public void Bar() { lock(lockObj){ // Do something } } } ...

Python: Lock directory access under windows

Hi, I'd like to be able to lock directory access under windows. The following code work greatly with file or directory under POSIX system: def flock(fd, blocking=False, exclusive=False): if exclusive: flags = fcntl.LOCK_EX else: flags = fcntl.LOCK_SH if not blocking: flags |= fcntl.LOCK_NB fcntl....

We need to lock a .NET Int32 when reading it in a multithreaded code?

I was reading the following article: http://msdn.microsoft.com/en-us/magazine/cc817398.aspx "Solving 11 Likely Problems In Your Multithreaded Code" by Joe Duffy And it raised me a question: "We need to lock a .NET Int32 when reading it in a multithreaded code?" I understand that if it was an Int64 in a 32-bit SO it could tear, as it is...

ReaderWriterLockSlim vs. Monitor

I have an IDictionary<TKey,TValue> implementation that internally holds n other Dictionary<TKey, TValue> and distributes that insertions by the HashCode of the key to the invidual sub-dictionaries. With 16 sub-dictionaries, the number of collisions is pretty low on a 4-core machine. For parallel insertions, i locked the Add-method with ...

How to properly lock a value type?

I was reading about threading and about locking. It is common practise that you can't (well should not) lock a value type. So the question is, what is the recommended way of locking a value type? I know there's a few ways to go about doing one thing but I haven't seen an example. Although there was a good thread on MSDN forums but I can...

Tuning SQL Server 2008 for web applications

In one of the Stackoverflow podcasts, I remember Jeff Atwood saying that there was a configuration option in SQL Server 2008 which cuts down on locking, and was kind of an alternative to using "with (nolock)" in all your queries. Does anybody know how to enable the feature he was talking about, possibly even Jeff himself. I'm looking a...

File Locking (Read/Write) in ASP.NET Application

I have two ASP.NET web application. One is responsible for processing some info and writing to a log file, and the other application is reponsible for reading the log file and displays the information based on user request. Here's my code for the Writer public static void WriteLog(String PathToLogFile, String Message) { Mutex FileL...

C# concurrency, locking and dictionary objects

I have a bunch of DB entities that are loaded into DB objects. The same DB entity may be loaded into more than DB object. Periodically a DB entity will require special processing. This processing must be performed by one thread at a time. Locking is in order here. EDIT: Important note: the process calls a slow web service. This ...

SQL 2005: NOLOCK hint dramatically increases reads. WTF!?

I have a stored procedure that does a lot more reads when the NOLOCK hint is added to the query. I'm baffled - does anyone know why, please? Details: The query is: SELECT * FROM dbo.<table-name> WITH (NOLOCK). It was doing 40,000 reads and there are less than 2,000 rows. I established that most of these reads are caused by 3 TEXT co...

How do I replace the contents of an existing XML file in C#.NET?

When trying to replace the content of an XML file in C#.NET with a snippet like this: string file = Path.GetTempFileName(); // pretend this is a real file string tmpFile = Path.GetTempFileName(); using (var writer = XmlWriter.Create(File.Create(tmpFile))) { writer.WriteStartElement("root"); for (int i = 0; i < 100; i++) { ...

SQL Server 2005: Key-Range Locks in Read Committed Transaction Isolation Level?

I'm helping troubleshoot some deadlocking in a .NET application that uses SQL Server 2005. I have the XML data from the trace below. What really puzzles me is the RangeX-X lock on PK_Exp_Experience_PriorFirm when the transaction isolation level is read committed. Everything I've read indicates that you only get a key-range lock of you ...

Locking HttpRuntime.Cache for lazy loading

We have a website running .NET 2.0 and have started using the ASP.Net HttpRuntime.Cache to store the results of frequent data lookups to cut down our database access. Snippet: lock (locker) { if (HttpRuntime.Cache[cacheKey] == null) { HttpRuntime.Cache.Insert(cacheKey, GetSomeDataToCache(), null, DateTime.Today.AddDa...

Multithreaded Resource Access - Where Do I Put My Locks?

I have threaded code where each thread needs to write to the same file. To prevent concurrency issues, I am using a Lock object. My question is whether I am using the Lock correctly. If I set the lock from within each thread, is that lock global or only specific to that specific thread? Basically, should I create a Lock first and pas...