I have several questions regarding the use of Singleton Bean.
First let me describe the situation. There's a web-application that will have many clients sending and receiving data to/from it. Some of this data must be persisted in database, but I need most of it to be available on the server for quick access and to take the strain off database.
I'm going to use Singleton Bean for that shared space and here are my questions:
If client_1 tries to update his data in singleton member that is currently LOCKED by client_2 writing to it, what exactly happens to client_1 data? Do Ineed to prepere for such case, or is it like a stack and his write operation will take place after client_2 finished his?
If I use hashmap, which keys will correspond to individual writing clients, would it be safe to place a @Lock(READ)
on a set
method which can only update existing records and not add new ones. The way I see it, since every client could only write to his own record there's no need to block others from updating their own records at the same time.
client_1 will wait to client_2 complete then itself update the data
hashmap is not thread safe you might also be interested in to take a look one of below:
private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
>
> private final Lock readLock = readWriteLock.readLock();
>
> private final Lock writeLock = readWriteLock.writeLock();
>
> private final List<E> list = new ArrayList<>();
>
> public void set(E o)
> {
> writeLock.lock();
> try
> {
> list.add(o);
> System.out.println("Adding element by thread"+Thread.currentThread().getName());
> }
> finally
> {
> writeLock.unlock();
> }
> }