Skip to content

Commit

Permalink
perf: execute "SET application_name" if name has changed only
Browse files Browse the repository at this point in the history
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 893c1a4
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,10 @@ public interface ProtocolConnection {
* @return backend timezone in java format.
*/
TimeZone getTimeZone();

/**
* Returns application_name connection property.
* @return application_name connection property
*/
String getApplicationName();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
6 changes: 6 additions & 0 deletions pgjdbc/src/main/java/org/postgresql/jdbc/PgConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '");
Expand Down Expand Up @@ -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;
}

Expand Down
16 changes: 16 additions & 0 deletions pgjdbc/src/test/java/org/postgresql/test/jdbc4/ClientInfoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 893c1a4

Please sign in to comment.