Re: performance problem of Failover Datasource?

From: Florent Guillaume <fg(at)nuxeo(dot)com>
To: Chen Huajun <chenhj(at)cn(dot)fujitsu(dot)com>
Cc: Scott Harrington <scotth01(at)sns-usa(dot)com>, "pgsql-jdbc(at)postgresql(dot)org" <pgsql-jdbc(at)postgresql(dot)org>
Subject: Re: performance problem of Failover Datasource?
Date: 2012-12-26 11:17:14
Message-ID: CAF-4BpONOd3s8DA7q855fWrQOGs9HS5tFmFToVvOtAUHu9fvRw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

On Tue, Dec 25, 2012 at 2:45 PM, Chen Huajun <chenhj(at)cn(dot)fujitsu(dot)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

In response to

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message Chen Huajun 2012-12-26 11:45:28 Re: performance problem of Failover Datasource?
Previous Message Chen Huajun 2012-12-25 13:45:57 Re: performance problem of Failover Datasource?