Permalink
Browse files

perf: execute "SET application_name" if name has changed only

This improves cases when application is setting the same name again and again (e.g. glassfish3).

See http://www.postgresql.org/message-id/flat/CADK3HHJKRq09otwqtv3rtnDh9h_LqFvBz+BHKgHjZgtnybvF1g@mail.gmail.com

closes #537
  • Loading branch information...
vlsi committed Jun 22, 2016
1 parent b4c45ca commit 893c1a41bedf56017473194113d335d280992851
@@ -186,4 +186,10 @@
* @return backend timezone in java format.
*/
TimeZone getTimeZone();
/**
* Returns application_name connection property.
* @return application_name connection property
*/
String getApplicationName();
}
@@ -234,6 +234,11 @@ public TimeZone getTimeZone() {
return TimeZone.getDefault();
}
@Override
public String getApplicationName() {
return "";
}
private String serverVersion;
private int serverVersionNum = 0;
private int cancelPid;
@@ -245,6 +245,18 @@ public TimeZone getTimeZone() {
return timeZone;
}
void setApplicationName(String applicationName) {
this.applicationName = applicationName;
}
@Override
public String getApplicationName() {
if (applicationName == null) {
return "";
}
return applicationName;
}
/**
* True if server uses integers for date and time fields. False if server uses double.
*/
@@ -279,4 +291,9 @@ public TimeZone getTimeZone() {
* TimeZone of the current connection (TimeZone backend parameter)
*/
private TimeZone timeZone;
/**
* application_name connection property
*/
private String applicationName;
}
@@ -2091,6 +2091,9 @@ protected void processResults(ResultHandler handler, int flags) throws IOExcepti
if ("TimeZone".equals(name)) {
protoConnection.setTimeZone(TimestampUtils.parseBackendTimeZone(value));
}
if ("application_name".equals(name)) {
protoConnection.setApplicationName(value);
}
break;
}
@@ -1385,6 +1385,10 @@ public void setClientInfo(String name, String value) throws SQLClientInfoExcepti
if (value == null) {
value = "";
}
final String oldValue = protoConnection.getApplicationName();
if (value.equals(oldValue)) {
return;
}
try {
StringBuilder sql = new StringBuilder("SET application_name = '");
@@ -1435,11 +1439,13 @@ public void setClientInfo(Properties properties) throws SQLClientInfoException {
public String getClientInfo(String name) throws SQLException {
checkClosed();
_clientInfo.put("ApplicationName", protoConnection.getApplicationName());
return _clientInfo.getProperty(name);
}
public Properties getClientInfo() throws SQLException {
checkClosed();
_clientInfo.put("ApplicationName", protoConnection.getApplicationName());
return _clientInfo;
}
@@ -56,6 +56,22 @@ public void testSetAppName() throws SQLException {
assertEquals("my app", _conn.getClientInfo().getProperty("ApplicationName"));
}
public void testExplicitSetAppNameNotificationIsParsed() throws SQLException {
if (!TestUtil.haveMinimumServerVersion(_conn, "9.0")) {
return;
}
String appName = "test-42";
Statement s = _conn.createStatement();
s.execute("set application_name='" + appName + "'");
s.close();
assertEquals("application_name was set to " + appName + ", and it should be visible via "
+ "con.getClientInfo", appName, _conn.getClientInfo("ApplicationName"));
assertEquals("application_name was set to " + appName + ", and it should be visible via "
+ "con.getClientInfo", appName, _conn.getClientInfo().get("ApplicationName"));
}
public void testSetAppNameProps() throws SQLException {
if (!TestUtil.haveMinimumServerVersion(_conn, "9.0")) {
return;

0 comments on commit 893c1a4

Please sign in to comment.