How to get around OptimisticLockException or StaleObjectStateException
Tech-Today

How to get around OptimisticLockException or StaleObjectStateException


The 2 errors above are common specially when dealing with a multi-threaded application. For example in my case I have several rabbit mq listeners. For those that are not familiar with it, it's a server where one language can send a message to be consumed by another but not limited to that. For example communication between .net and java.

This multi-threaded application access a set of web services that update parts of a database. The problem is messages arriving in the rabbit mq bus are asynchronous and are not really aware of each other. So there might be a time that 2 different requests that access the same part of a database at the same time.

Working with this I've remembered 2 different solutions plus I've learned another one:

1.) Using synchronized keyword in a method. This is what I've used, because fortunately all my web service calls are in a single class. Basically it locks the method and only 1 thread can access the method at a time.
public synchronized ClientResponse syncWebServiceCall(
WebResource webResource) {
// code here
}

2.) The next is a statement block, similar to no 1 except that you used a mutex object to lock. The mutex object can be the object where the method is defined or another object. In this way we don't necessarily lock the whole method.
public ClientResponse syncWebServiceCall(
WebResource webResource) {
synchronized(this) {
// code
}
}

3.) The last one. Which I've just learned. Using ReentrantLock: http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/ReentrantLock.html Modifying the example:
class X {
private final ReentrantLock lock = new ReentrantLock();

public ClientResponse syncWebServiceCall() {
lock.lock(); // block until condition holds
try {
// ... method body
} finally {
lock.unlock()
}
}
}




- Avoid The Jsf Prerenderview On Crud Edit Mode
Long time ago I learned that the usual approach in CRUD is list, add, edit, delete. And right now I'm implementing the same thing in JavaEE6, Glassfish, JSF 2.1, Primefaces. So what I did was: 1.) Create a single backing bean that will handle all...

- Transfer Domain From 1and1 Package Into Another
There are 2 major steps that we have to do for this: 1.) Unlock the domain that we want to transfer a.) login to 1and1.com (by default Packages tab is selected) b.) in the Packages tab, select the package where the domain you want to transfer is in c.)...

- Updating The Status Message Dynamically Of An Eclipse Rcp Application By Using A Worker Thread
Objective: -To create an eclipse application that will dynamically update the action bar's status message from a worker thread. Updating a part of the screen in another thread is a good practice, since it will not make your screen blink, also the...

- How To Setup Cvs Server And Client On A Windows Machine.
How to setup cvs server and client on a windows machine. Requirements: 1.) http://www.wincvs.org/ - download wincvs it has cvsnt included in the zip file Or 1.1) http://www.tortoisecvs.org/download.shtml = download TortoiseCVS Instructions: 0.) Stop the...

- Cvs [update Aborted]: Cvs Server: Connect To 127.0.0.1(127.0.0.1):2402 Failed: No Connection Could Be Made Because The Target Machine Actively Refused
This error can be corrected by starting the CVSNT Lock Service. It can be access in Start -> Programs -> CSVNT -> CSVNT Control Panel...



Tech-Today








.