Skip to content

Commit

Permalink
added close() for connection
Browse files Browse the repository at this point in the history
  • Loading branch information
HarshDaryani896 committed May 7, 2024
1 parent 5fbfc91 commit a891cb7
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
19 changes: 18 additions & 1 deletion pgjdbc/src/main/java/com/yugabyte/ysql/YBDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Properties;

import org.postgresql.CustomDriver;
import org.postgresql.core.QueryExecutor;
import org.postgresql.jdbc.PgConnection;
import static org.postgresql.Driver.*;

Expand All @@ -28,6 +29,22 @@ public Connection makeConnection(String url, Properties properties) throws SQLEx
}
return new PgConnection(hostSpecs(properties), user(properties), database(properties),
properties, url);
}
}

/**
* <B>Note:</B> even though {@code Statement} is automatically closed when it is garbage
* collected, it is better to close it explicitly to lower resource consumption.
* The spec says that calling close on a closed connection is a no-op.
*
* {@inheritDoc}
*/
@Override
public void close(QueryExecutor queryExecutor) throws SQLException {

String host = queryExecutor.getHostSpec().getHost();
if (host != null) {
LoadBalanceManager.decrementConnectionCount(host);
}
}

}
11 changes: 11 additions & 0 deletions pgjdbc/src/main/java/org/postgresql/CustomDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.sql.SQLException;
import java.util.Properties;

import org.postgresql.core.QueryExecutor;

/**
* An interface for any custom driver to implement. connect() invokes the
* implemented method while processing a new connection request.
Expand All @@ -21,4 +23,13 @@ public interface CustomDriver {
*/
Connection makeConnection(String url, Properties properties) throws SQLException;

/**
* <B>Note:</B> even though {@code Statement} is automatically closed when it is garbage
* collected, it is better to close it explicitly to lower resource consumption.
* The spec says that calling close on a closed connection is a no-op.
*
* {@inheritDoc}
*/
public void close(QueryExecutor queryExecutor) throws SQLException;

}
13 changes: 13 additions & 0 deletions pgjdbc/src/main/java/org/postgresql/DefaultCustomDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import org.postgresql.core.QueryExecutor;
import org.postgresql.jdbc.PgConnection;
import static org.postgresql.Driver.*;

Expand All @@ -23,4 +25,15 @@ public Connection makeConnection(String url, Properties properties) throws SQLEx
properties, url);
}

/**
* <B>Note:</B> even though {@code Statement} is automatically closed when it is garbage
* collected, it is better to close it explicitly to lower resource consumption.
* The spec says that calling close on a closed connection is a no-op.
*
* {@inheritDoc}
*/
@Override
public void close(QueryExecutor queryExecutor) throws SQLException {
//No op
}
}
8 changes: 2 additions & 6 deletions pgjdbc/src/main/java/org/postgresql/jdbc/PgConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import static org.postgresql.util.internal.Nullness.castNonNull;

import org.postgresql.CustomDriver;
import org.postgresql.Driver;
import org.postgresql.PGNotification;
import org.postgresql.PGProperty;
Expand Down Expand Up @@ -773,12 +774,7 @@ public void close() throws SQLException {
releaseTimer();
queryExecutor.close();
openStackTrace = null;
if (Driver.custDriver instanceof YBDriver ) {
String host = queryExecutor.getHostSpec().getHost();
if (loadBalancer != null && host != null) {
LoadBalanceManager.decrementConnectionCount(host);
}
}
Driver.custDriver.close(queryExecutor);
}

public void setLoadBalancer(LoadBalancer lb) {
Expand Down

0 comments on commit a891cb7

Please sign in to comment.