Re: performance problem of Failover Datasource? - Mailing list pgsql-jdbc

From Florent Guillaume
Subject Re: performance problem of Failover Datasource?
Date
Msg-id CAF-4BpONOd3s8DA7q855fWrQOGs9HS5tFmFToVvOtAUHu9fvRw@mail.gmail.com
Whole thread Raw
In response to Re: performance problem of Failover Datasource?  (Chen Huajun <chenhj@cn.fujitsu.com>)
Responses Re: performance problem of Failover Datasource?  (Chen Huajun <chenhj@cn.fujitsu.com>)
List pgsql-jdbc
On Tue, Dec 25, 2012 at 2:45 PM, Chen Huajun <chenhj@cn.fujitsu.com> wrote:
> Now ,Could you explain the the detail of following issues?
>> At first glance, there are a couple of issues:
>>
>> 1. Double-Checked Locking in reportHostStatus, which is bad form
>
> I knows a problem of Double-Checked Locking,while used in singleton pattern
> as following.
>
>     public static Singleton getInstance() {
>                 if (instance == null) {
>                     synchronized (Singleton.class) {
>                         if (instance == null) {
>                             instance = new Singleton();
>                         }
>                     }
>                 }
>                 return instance;
>             }
>
> because JVM would run "instance = new Singleton(); " as that :
>
> mem = allocate();
> instance = mem;
> ctorSingleton(instance);
>
> Do you think my code has the same problem or just it looks ugly?

Double-checked locking generally is incorrect, and does not work. It
ONLY works if you're double-checking a volatile variable and using
Java >= 5.
Please read http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html


>> 3. No need for 'volatile' if you're also using 'synchronized'
>
> 'synchronized' is only used for write,'volatile' is for read.

This sentence shows you're confused about synchronization and
multi-threading in Java.

> It's for performance and is a bit complex.(It may be a excessive design)
> I worry about JVM will optimize the following code
>
>   HashMap<String,HostStatus> newHostStatusMap = (HashMap<String,
> HostStatus>) hostStatusCache.clone();
>   newHostStatusMap.put(hostSpecKey, hostStatus);
>   hostStatusCache=newHostStatusMap;
> as:
>   hostStatusCache=(HashMap<String, HostStatus>) hostStatusCache.clone();
>   hostStatusCache.put(hostSpecKey, hostStatus);
>
> do you know if it will happen?

Of course it can. The JVM is free to reorder a lot of things while
respecting the Java Memory Model.

All this means you shouldn't try to play games with the JVM, just use
a basic lock or synchronization primitive where you need it.

Florent

--
Florent Guillaume, Director of R&D, Nuxeo
Open Source, Java EE based, Enterprise Content Management (ECM)
http://www.nuxeo.com   http://www.nuxeo.org   +33 1 40 33 79 87


pgsql-jdbc by date:

Previous
From: Chen Huajun
Date:
Subject: Re: performance problem of Failover Datasource?
Next
From: Chen Huajun
Date:
Subject: Re: performance problem of Failover Datasource?