From: Chen Huajun <chenhj@cn.fujitsu.com>
To: Dave Cramer <pg@fastcrypt.com>
Cc: kaprikorn07 <bharath.spyk@gmail.com>
Cc: List <pgsql-jdbc@postgresql.org>
Subject: Re: performance problem of Failover Datasource?
Date: Fri, 14 Dec 2012 21:29:33 +0800
Message-ID: <50CB29BD.6090903@cn.fujitsu.com> (raw)
In-Reply-To: <50CA959E.4060603@cn.fujitsu.com>
References: <4DFA5A86.9030301@acm.org>
<itdn5g$eu7$1@dough.gmane.org>
<4DFA671F.2050005@acm.org>
<4DFA9704.4000209@hogranch.com>
<4DFAB403.5010309@acm.org>
<4DFAB5B8.5010603@hogranch.com>
<4DFAB9E2.4070905@acm.org>
<1334056170219-5629626.post@n5.nabble.com>
<1355303340886-5736270.post@n5.nabble.com>
<50C9B102.2060208@cn.fujitsu.com>
<CADK3HH+TLPjrrJ=cQWqZ+0B5o96xR+sTVcvqhcDU4CjyryKG2g@mail.gmail.com>
<50CA959E.4060603@cn.fujitsu.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-jdbc>
Hi
I had make the patch,Please check it.
and I had run the testcases,the result is same as before modified.
--
Best Regards,
Chen Huajun
--
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-jdbcAttachments:pgjdbc_optimization_for_mutiServerUrl.patch (text/plain, 3.4K) downloaddiff --git a/org/postgresql/core/ConnectionFactory.java b/org/postgresql/core/ConnectionFactory.javaindex a351619..e2ba588 100644--- a/org/postgresql/core/ConnectionFactory.java+++ b/org/postgresql/core/ConnectionFactory.java@@ -8,7 +8,10 @@
*/
package org.postgresql.core;
+import java.util.Collections;+import java.util.HashSet;
import java.util.Properties;
+import java.util.Set;
import java.sql.SQLException;
import org.postgresql.util.HostSpec;
@@ -32,6 +35,11 @@
{ "3", new org.postgresql.core.v3.ConnectionFactoryImpl() },
{ "2", new org.postgresql.core.v2.ConnectionFactoryImpl() },
};
+ + /**+ * List of alive servers.+ */+ private static final Set<String> aliveServers = Collections.synchronizedSet(new HashSet<String>());
/**
* Establishes and initializes a new connection.
@@ -54,6 +62,12 @@
public static ProtocolConnection openConnection(HostSpec[] hostSpecs, String user, String database, Properties info, Logger logger) throws SQLException {
String protoName = info.getProperty("protocolVersion");
+ // Adjust the order of hostSpecs to try the most suitable server first.+ if(hostSpecs.length > 1)+ {+ hostSpecs = adjustHostSpecsOrder(hostSpecs);+ }+
for (int i = 0; i < versions.length; ++i)
{
String versionProtoName = (String) versions[i][0];
@@ -62,10 +76,30 @@
ConnectionFactory factory = (ConnectionFactory) versions[i][1];
ProtocolConnection connection = factory.openConnectionImpl(hostSpecs, user, database, info, logger);
- if (connection != null)- return connection;+ if (connection != null) {+ if (hostSpecs.length > 1) {+ String hostSpecKey = connection.getHostSpec().toString();++ // Remove servers which can not be connected from the list of alive servers+ for (int j = 0; j < hostSpecs.length; j++) {+ if (hostSpecKey.equals(hostSpecs[j].toString()))+ break;++ aliveServers.remove(hostSpecs[j].toString());+ }+ // Add the current server to the list of alive servers+ aliveServers.add(hostSpecKey);+ }+ return connection;+ }
}
+ if (hostSpecs.length > 1) {+ // Remove servers which can not be connected from the list of alive servers+ for (int j = 0; j < hostSpecs.length; j++) {+ aliveServers.remove(hostSpecs[j].toString());+ }+ }
throw new PSQLException(GT.tr("A connection could not be made using the requested protocol {0}.", protoName),
PSQLState.CONNECTION_UNABLE_TO_CONNECT);
}
@@ -86,4 +120,26 @@
* than protocol version incompatibility.
*/
public abstract ProtocolConnection openConnectionImpl(HostSpec[] hostSpecs, String user, String database, Properties info, Logger logger) throws SQLException;
+ + + /**+ * Adjust the order of servers in hostSpecs, internally move first found alive server to head.+ */+ private static HostSpec[] adjustHostSpecsOrder(HostSpec[] hostSpecs) {+ if (aliveServers.isEmpty()+ || aliveServers.contains(hostSpecs[0].toString()))+ return hostSpecs;++ HostSpec[] result = hostSpecs.clone();++ for (int i = 1; i < hostSpecs.length; i++) {+ if (aliveServers.contains(hostSpecs[i].toString())) {+ result[0] = hostSpecs[i];+ result[i] = hostSpecs[0];+ break;+ }+ }++ return result;+ }
}=
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-jdbc@postgresql.org
Cc: chenhj@cn.fujitsu.com, pg@fastcrypt.com, bharath.spyk@gmail.com
Subject: Re: performance problem of Failover Datasource?
In-Reply-To: <50CB29BD.6090903@cn.fujitsu.com>
References: <4DFA5A86.9030301@acm.org> <itdn5g$eu7$1@dough.gmane.org> <4DFA671F.2050005@acm.org> <4DFA9704.4000209@hogranch.com> <4DFAB403.5010309@acm.org> <4DFAB5B8.5010603@hogranch.com> <4DFAB9E2.4070905@acm.org> <1334056170219-5629626.post@n5.nabble.com> <1355303340886-5736270.post@n5.nabble.com> <50C9B102.2060208@cn.fujitsu.com> <CADK3HH+TLPjrrJ=cQWqZ+0B5o96xR+sTVcvqhcDU4CjyryKG2g@mail.gmail.com> <50CA959E.4060603@cn.fujitsu.com> <50CB29BD.6090903@cn.fujitsu.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox