Java Synchronized Block for .class

The snippet synchronized(X.class) uses the class instance as a monitor. As there is only one class instance (the object representing the class metadata at runtime) one thread can be in this block. With synchronized(this) the block is guarded by the instance. For every instance only one thread may enter the block. synchronized(X.class) is used to … Read more

Intermittent log4net RollingFileAppender locked file issue

Try adding <lockingModel type=”log4net.Appender.FileAppender+MinimalLock” /> to your <appender /> element. There is some performance impact because this means that log4net will lock the file, write to it, and unlock it for each write operation (as opposed to the default behavior, which acquires and holds onto the lock for a long time). One implication of the … Read more

Re-entrant locks in C#

No, not as long as you are locking on the same object. The recursive code effectively already has the lock and so can continue unhindered. lock(object) {…} is shorthand for using the Monitor class. As Marc points out, Monitor allows re-entrancy, so repeated attempts to lock on an object on which the current thread already … Read more

MySQL: Transactions vs Locking Tables

Locking tables prevents other DB users from affecting the rows/tables you’ve locked. But locks, in and of themselves, will NOT ensure that your logic comes out in a consistent state. Think of a banking system. When you pay a bill online, there’s at least two accounts affected by the transaction: Your account, from which the … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)