Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: restructuring try blocks in dialects for exception handling #799

Merged
merged 1 commit into from
Dec 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,32 @@ public class AuroraMysqlDialect extends MysqlDialect {

@Override
public boolean isDialect(final Connection connection) {
try (final Statement stmt = connection.createStatement();
final ResultSet rs = stmt.executeQuery("SHOW VARIABLES LIKE 'aurora_version'")) {
Statement stmt = null;
ResultSet rs = null;
try {
stmt = connection.createStatement();
rs = stmt.executeQuery("SHOW VARIABLES LIKE 'aurora_version'");
if (rs.next()) {
// If variable with such name is presented then it means it's an Aurora cluster
return true;
}
} catch (final SQLException ex) {
// ignore
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException ex) {
// ignore
}
}
if (rs != null) {
try {
rs.close();
} catch (SQLException ex) {
// ignore
}
}
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,34 +54,67 @@
return false;
}

Statement stmt = null;
ResultSet rs = null;
boolean hasExtensions = false;
boolean hasTopology = false;
try {
try (final Statement stmt = connection.createStatement();
final ResultSet rs = stmt.executeQuery(extensionsSql)) {
if (rs.next()) {
final boolean auroraUtils = rs.getBoolean("aurora_stat_utils");
LOGGER.finest(() -> String.format("auroraUtils: %b", auroraUtils));
if (auroraUtils) {
hasExtensions = true;
}
stmt = connection.createStatement();
rs = stmt.executeQuery(extensionsSql);
if (rs.next()) {
final boolean auroraUtils = rs.getBoolean("aurora_stat_utils");
LOGGER.finest(() -> String.format("auroraUtils: %b", auroraUtils));
if (auroraUtils) {
hasExtensions = true;
}
}

try (final Statement stmt = connection.createStatement();
final ResultSet rs = stmt.executeQuery(topologySql)) {
if (rs.next()) {
LOGGER.finest(() -> "hasTopology: true");
hasTopology = true;
} catch (SQLException ex) {
// ignore
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException ex) {
// ignore
}
}

return hasExtensions && hasTopology;

if (rs != null) {
try {
rs.close();
} catch (SQLException ex) {
// ignore
}
}
}
if (!hasExtensions) {
return false;
}
try {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think, as optimization, we can add the following code here:

if (!hasExtensions) {
return false;
}

stmt = connection.createStatement();
rs = stmt.executeQuery(topologySql);
if (rs.next()) {
LOGGER.finest(() -> "hasTopology: true");
hasTopology = true;
}
} catch (final SQLException ex) {
// ignore
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException ex) {
// ignore
}
}
if (rs != null) {
try {
rs.close();
} catch (SQLException ex) {
// ignore
}
}
}
return false;
return hasExtensions && hasTopology;

Check warning on line 117 in wrapper/src/main/java/software/amazon/jdbc/dialect/AuroraPgDialect.java

View workflow job for this annotation

GitHub Actions / qodana

Constant conditions & exceptions

Condition `hasExtensions` is always `true`
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,11 @@ public String getServerVersionQuery() {

@Override
public boolean isDialect(final Connection connection) {
try (final Statement stmt = connection.createStatement();
final ResultSet rs = stmt.executeQuery(this.getServerVersionQuery())) {
Statement stmt = null;
ResultSet rs = null;
try {
stmt = connection.createStatement();
rs = stmt.executeQuery(this.getServerVersionQuery());
while (rs.next()) {
final String columnValue = rs.getString(1);
if (columnValue != null && columnValue.toLowerCase().contains("mariadb")) {
Expand All @@ -67,6 +70,21 @@ public boolean isDialect(final Connection connection) {
}
} catch (final SQLException ex) {
// ignore
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException ex) {
// ignore
}
}
if (rs != null) {
try {
rs.close();
} catch (SQLException ex) {
// ignore
}
}
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,11 @@ public String getServerVersionQuery() {

@Override
public boolean isDialect(final Connection connection) {
try (final Statement stmt = connection.createStatement();
final ResultSet rs = stmt.executeQuery(this.getServerVersionQuery())) {
Statement stmt = null;
ResultSet rs = null;
try {
stmt = connection.createStatement();
rs = stmt.executeQuery(this.getServerVersionQuery());
while (rs.next()) {
final int columnCount = rs.getMetaData().getColumnCount();
for (int i = 1; i <= columnCount; i++) {
Expand All @@ -75,6 +78,21 @@ public boolean isDialect(final Connection connection) {
}
} catch (final SQLException ex) {
// ignore
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException ex) {
// ignore
}
}
if (rs != null) {
try {
rs.close();
} catch (SQLException ex) {
// ignore
}
}
}
return false;
}
Expand Down
26 changes: 21 additions & 5 deletions wrapper/src/main/java/software/amazon/jdbc/dialect/PgDialect.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,31 @@ public String getServerVersionQuery() {

@Override
public boolean isDialect(final Connection connection) {
Statement stmt = null;
ResultSet rs = null;
try {
try (final Statement stmt = connection.createStatement();
final ResultSet rs = stmt.executeQuery("SELECT 1 FROM pg_proc LIMIT 1")) {
if (rs.next()) {
return true;
}
stmt = connection.createStatement();
rs = stmt.executeQuery("SELECT 1 FROM pg_proc LIMIT 1");
if (rs.next()) {
return true;
}
} catch (final SQLException ex) {
// ignore
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException ex) {
// ignore
}
}
if (rs != null) {
try {
rs.close();
} catch (SQLException ex) {
// ignore
}
}
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,29 @@ public class RdsMultiAzDbClusterMysqlDialect extends MysqlDialect {

@Override
public boolean isDialect(final Connection connection) {
try (final Statement stmt = connection.createStatement();
final ResultSet rs = stmt.executeQuery(TOPOLOGY_QUERY)) {
Statement stmt = null;
ResultSet rs = null;
try {
stmt = connection.createStatement();
rs = stmt.executeQuery(TOPOLOGY_QUERY);
return rs.next();
} catch (final SQLException ex) {
// ignore
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException ex) {
// ignore
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException ex) {
// ignore
}
}
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,29 @@ public ExceptionHandler getExceptionHandler() {

@Override
public boolean isDialect(final Connection connection) {
try (final Statement stmt = connection.createStatement();
final ResultSet rs = stmt.executeQuery(FETCH_WRITER_NODE_QUERY)) {
Statement stmt = null;
ResultSet rs = null;
try {
stmt = connection.createStatement();
rs = stmt.executeQuery(FETCH_WRITER_NODE_QUERY);
return rs.next();
} catch (final SQLException ex) {
// ignore
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException ex) {
// ignore
}
}
if (rs != null) {
try {
rs.close();
} catch (SQLException ex) {
// ignore
}
}
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@ public boolean isDialect(final Connection connection) {
if (!super.isDialect(connection)) {
return false;
}
try (final Statement stmt = connection.createStatement();
final ResultSet rs = stmt.executeQuery(this.getServerVersionQuery())) {
Statement stmt = null;
ResultSet rs = null;

try {
stmt = connection.createStatement();
rs = stmt.executeQuery(this.getServerVersionQuery());
while (rs.next()) {
final int columnCount = rs.getMetaData().getColumnCount();
for (int i = 1; i <= columnCount; i++) {
Expand All @@ -46,6 +50,21 @@ public boolean isDialect(final Connection connection) {
}
} catch (final SQLException ex) {
// ignore
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException ex) {
// ignore
}
}
if (rs != null) {
try {
rs.close();
} catch (SQLException ex) {
// ignore
}
}
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,37 @@ public boolean isDialect(final Connection connection) {
if (!super.isDialect(connection)) {
return false;
}
Statement stmt = null;
ResultSet rs = null;

try {
try (final Statement stmt = connection.createStatement();
final ResultSet rs = stmt.executeQuery(extensionsSql)) {
while (rs.next()) {
final boolean rdsTools = rs.getBoolean("rds_tools");
final boolean auroraUtils = rs.getBoolean("aurora_stat_utils");
LOGGER.finest(() -> String.format("rdsTools: %b, auroraUtils: %b", rdsTools, auroraUtils));
if (rdsTools && !auroraUtils) {
return true;
}
stmt = connection.createStatement();
rs = stmt.executeQuery(extensionsSql);
while (rs.next()) {
final boolean rdsTools = rs.getBoolean("rds_tools");
final boolean auroraUtils = rs.getBoolean("aurora_stat_utils");
LOGGER.finest(() -> String.format("rdsTools: %b, auroraUtils: %b", rdsTools, auroraUtils));
if (rdsTools && !auroraUtils) {
return true;
}
}
} catch (final SQLException ex) {
// ignore
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException ex) {
// ignore
}
}
if (rs != null) {
try {
rs.close();
} catch (SQLException ex) {
// ignore
}
}
}
return false;
}
Expand Down
Loading