diff --git a/java-bigquery/google-cloud-bigquery-jdbc/.gitignore b/java-bigquery/google-cloud-bigquery-jdbc/.gitignore index 0af2440505c9..615215d6f476 100644 --- a/java-bigquery/google-cloud-bigquery-jdbc/.gitignore +++ b/java-bigquery/google-cloud-bigquery-jdbc/.gitignore @@ -1,3 +1,4 @@ drivers/** target-it/** -*logs*/** \ No newline at end of file +*logs*/** +**/ITBigQueryJDBCLocalTest.java \ No newline at end of file diff --git a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/exception/BigQueryConversionException.java b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/exception/BigQueryConversionException.java index 90e758b05eeb..3b45b9c06124 100644 --- a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/exception/BigQueryConversionException.java +++ b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/exception/BigQueryConversionException.java @@ -16,14 +16,18 @@ package com.google.cloud.bigquery.exception; +import com.google.cloud.bigquery.jdbc.BigQueryJdbcCustomLogger; import java.sql.SQLException; /** * Exception for errors that occur when the driver cannot convert a value from one type to another. */ public class BigQueryConversionException extends SQLException { + private static final BigQueryJdbcCustomLogger LOG = + new BigQueryJdbcCustomLogger(BigQueryConversionException.class.getName()); public BigQueryConversionException(String message, Throwable cause) { super(message, cause); + LOG.severe(message, this); } } diff --git a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/exception/BigQueryJdbcCoercionException.java b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/exception/BigQueryJdbcCoercionException.java index 185ef54bb1da..d60e61e126db 100644 --- a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/exception/BigQueryJdbcCoercionException.java +++ b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/exception/BigQueryJdbcCoercionException.java @@ -17,6 +17,7 @@ package com.google.cloud.bigquery.exception; import com.google.api.core.InternalApi; +import com.google.cloud.bigquery.jdbc.BigQueryJdbcCustomLogger; /** * Thrown to indicate that the coercion was attempted but couldn't be performed successfully because @@ -24,6 +25,8 @@ */ @InternalApi public class BigQueryJdbcCoercionException extends RuntimeException { + private static final BigQueryJdbcCustomLogger LOG = + new BigQueryJdbcCustomLogger(BigQueryJdbcCoercionException.class.getName()); /** * Construct a new exception with the specified cause. @@ -32,5 +35,6 @@ public class BigQueryJdbcCoercionException extends RuntimeException { */ public BigQueryJdbcCoercionException(Exception cause) { super("Coercion error", cause); + LOG.severe("Coercion error", this); } } diff --git a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/exception/BigQueryJdbcCoercionNotFoundException.java b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/exception/BigQueryJdbcCoercionNotFoundException.java index b4eafb2ee583..8a12da311c21 100644 --- a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/exception/BigQueryJdbcCoercionNotFoundException.java +++ b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/exception/BigQueryJdbcCoercionNotFoundException.java @@ -17,6 +17,7 @@ package com.google.cloud.bigquery.exception; import com.google.api.core.InternalApi; +import com.google.cloud.bigquery.jdbc.BigQueryJdbcCustomLogger; /** * Thrown to indicate that the current TypeCoercer can not perform the coercion as the Coercion @@ -24,6 +25,8 @@ */ @InternalApi public class BigQueryJdbcCoercionNotFoundException extends RuntimeException { + private static final BigQueryJdbcCustomLogger LOG = + new BigQueryJdbcCustomLogger(BigQueryJdbcCoercionNotFoundException.class.getName()); /** * Construct a new exception. @@ -36,5 +39,6 @@ public BigQueryJdbcCoercionNotFoundException(Class source, Class target) { String.format( "Coercion not found for [%s -> %s] conversion", source.getCanonicalName(), target.getCanonicalName())); + LOG.severe(this.getMessage(), this); } } diff --git a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/exception/BigQueryJdbcException.java b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/exception/BigQueryJdbcException.java index 72a22aba618a..fff602891200 100644 --- a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/exception/BigQueryJdbcException.java +++ b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/exception/BigQueryJdbcException.java @@ -17,9 +17,12 @@ package com.google.cloud.bigquery.exception; import com.google.cloud.bigquery.BigQueryException; +import com.google.cloud.bigquery.jdbc.BigQueryJdbcCustomLogger; import java.sql.SQLException; public class BigQueryJdbcException extends SQLException { + private static final BigQueryJdbcCustomLogger LOG = + new BigQueryJdbcCustomLogger(BigQueryJdbcException.class.getName()); private BigQueryException bigQueryException = null; /** @@ -29,6 +32,7 @@ public class BigQueryJdbcException extends SQLException { */ public BigQueryJdbcException(String message) { super(message); + LOG.severe(message, this); } /** @@ -38,16 +42,19 @@ public BigQueryJdbcException(String message) { */ public BigQueryJdbcException(InterruptedException ex) { super(ex); + LOG.severe(ex.getMessage(), this); } /** * Constructs a new BigQueryJdbcException from BigQueryException * + * @param message Specific message that is being added to the Exception. * @param ex The BigQueryException to be thrown. */ - public BigQueryJdbcException(BigQueryException ex) { - super(ex); + public BigQueryJdbcException(String message, BigQueryException ex) { + super(message, ex); this.bigQueryException = ex; + LOG.severe(ex.getMessage(), this); } /** @@ -58,6 +65,7 @@ public BigQueryJdbcException(BigQueryException ex) { */ public BigQueryJdbcException(String message, Throwable cause) { super(message, cause); + LOG.severe(message, this); } /** @@ -68,6 +76,7 @@ public BigQueryJdbcException(String message, Throwable cause) { */ public BigQueryJdbcException(Throwable cause) { super(cause); + LOG.severe(cause.getMessage(), this); } public BigQueryException getBigQueryException() { diff --git a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/exception/BigQueryJdbcRuntimeException.java b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/exception/BigQueryJdbcRuntimeException.java index 38e5171be40f..1f52347cb8f4 100644 --- a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/exception/BigQueryJdbcRuntimeException.java +++ b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/exception/BigQueryJdbcRuntimeException.java @@ -16,8 +16,13 @@ package com.google.cloud.bigquery.exception; +import com.google.cloud.bigquery.jdbc.BigQueryJdbcCustomLogger; + public class BigQueryJdbcRuntimeException extends RuntimeException { + private static final BigQueryJdbcCustomLogger LOG = + new BigQueryJdbcCustomLogger(BigQueryJdbcRuntimeException.class.getName()); + /** * Constructs a new BigQueryJdbcRuntimeException with the given message. * @@ -25,6 +30,7 @@ public class BigQueryJdbcRuntimeException extends RuntimeException { */ public BigQueryJdbcRuntimeException(String message) { super(message); + LOG.severe(message, this); } /** @@ -34,6 +40,7 @@ public BigQueryJdbcRuntimeException(String message) { */ public BigQueryJdbcRuntimeException(Throwable ex) { super(ex); + LOG.severe(ex.getMessage(), this); } /** @@ -44,5 +51,11 @@ public BigQueryJdbcRuntimeException(Throwable ex) { */ public BigQueryJdbcRuntimeException(String message, InterruptedException ex) { super(message, ex); + LOG.severe(message, this); + } + + public BigQueryJdbcRuntimeException(String message, Throwable ex) { + super(message, ex); + LOG.severe(message, this); } } diff --git a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/exception/BigQueryJdbcSqlFeatureNotSupportedException.java b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/exception/BigQueryJdbcSqlFeatureNotSupportedException.java index 8c93d8764b3a..0c4493b29dc3 100644 --- a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/exception/BigQueryJdbcSqlFeatureNotSupportedException.java +++ b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/exception/BigQueryJdbcSqlFeatureNotSupportedException.java @@ -17,9 +17,13 @@ package com.google.cloud.bigquery.exception; import com.google.cloud.bigquery.BigQueryException; +import com.google.cloud.bigquery.jdbc.BigQueryJdbcCustomLogger; import java.sql.SQLFeatureNotSupportedException; public class BigQueryJdbcSqlFeatureNotSupportedException extends SQLFeatureNotSupportedException { + private static final BigQueryJdbcCustomLogger LOG = + new BigQueryJdbcCustomLogger(BigQueryJdbcSqlFeatureNotSupportedException.class.getName()); + /** * Constructs a new BigQueryJdbcSqlFeatureNotSupportedException with the given message. * @@ -27,6 +31,7 @@ public class BigQueryJdbcSqlFeatureNotSupportedException extends SQLFeatureNotSu */ public BigQueryJdbcSqlFeatureNotSupportedException(String message) { super(message); + LOG.severe(message, this); } /** @@ -36,5 +41,6 @@ public BigQueryJdbcSqlFeatureNotSupportedException(String message) { */ public BigQueryJdbcSqlFeatureNotSupportedException(BigQueryException ex) { super(ex); + LOG.severe(ex.getMessage(), this); } } diff --git a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/exception/BigQueryJdbcSqlSyntaxErrorException.java b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/exception/BigQueryJdbcSqlSyntaxErrorException.java index 99edcd0c543c..8a9c3abb392e 100644 --- a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/exception/BigQueryJdbcSqlSyntaxErrorException.java +++ b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/exception/BigQueryJdbcSqlSyntaxErrorException.java @@ -17,6 +17,7 @@ package com.google.cloud.bigquery.exception; import com.google.cloud.bigquery.BigQueryException; +import com.google.cloud.bigquery.jdbc.BigQueryJdbcCustomLogger; import java.sql.SQLSyntaxErrorException; /** @@ -25,6 +26,9 @@ * rules. */ public class BigQueryJdbcSqlSyntaxErrorException extends SQLSyntaxErrorException { + private static final BigQueryJdbcCustomLogger LOG = + new BigQueryJdbcCustomLogger(BigQueryJdbcSqlSyntaxErrorException.class.getName()); + /** * Constructs a new BigQueryJdbcSqlSyntaxErrorException from BigQueryException * @@ -32,5 +36,11 @@ public class BigQueryJdbcSqlSyntaxErrorException extends SQLSyntaxErrorException */ public BigQueryJdbcSqlSyntaxErrorException(BigQueryException ex) { super(ex.getMessage(), "Incorrect SQL syntax."); + LOG.severe(ex.getMessage(), this); + } + + public BigQueryJdbcSqlSyntaxErrorException(String message, BigQueryException ex) { + super(message, ex); + LOG.severe(message, this); } } diff --git a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryArrowResultSet.java b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryArrowResultSet.java index 1d7d89e3f1f1..af041dc2a649 100644 --- a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryArrowResultSet.java +++ b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryArrowResultSet.java @@ -108,7 +108,7 @@ private BigQueryArrowResultSet( try { this.arrowDeserializer = new ArrowDeserializer(arrowSchema); } catch (IOException ex) { - throw new BigQueryJdbcException(ex); + throw new BigQueryJdbcException("IOException during ArrowDeserializer creation", ex); } } } @@ -215,8 +215,11 @@ public boolean next() throws SQLException { checkClosed(); if (this.isNested) { if (this.currentNestedBatch == null || this.currentNestedBatch.getNestedRecords() == null) { - throw new IllegalStateException( - "currentNestedBatch/JsonStringArrayList can not be null working with the nested record"); + IllegalStateException ex = + new IllegalStateException( + "currentNestedBatch/JsonStringArrayList can not be null working with the nested record"); + LOG.severe(ex.getMessage(), ex); + throw ex; } if (this.nestedRowIndex < (this.toIndexExclusive - 1)) { /* Check if there's a next record in the array which can be read */ @@ -283,12 +286,16 @@ private Object getObjectInternal(int columnIndex) throws SQLException { // BigQuery doesn't support multidimensional arrays, so // just the default row num column (1) and the actual column (2) is supposed to be read if (!(columnIndex == 1 || columnIndex == 2)) { - - throw new IllegalArgumentException( - "Column index is required to be 1 or 2 for nested arrays"); + IllegalArgumentException ex = + new IllegalArgumentException("Column index is required to be 1 or 2 for nested arrays"); + LOG.severe(ex.getMessage(), ex); + throw ex; } if (this.currentNestedBatch.getNestedRecords() == null) { - throw new IllegalStateException("JsonStringArrayList cannot be null for nested records."); + IllegalStateException ex = + new IllegalStateException("JsonStringArrayList cannot be null for nested records."); + LOG.severe(ex.getMessage(), ex); + throw ex; } // For Arrays the first column is Index, ref: // https://docs.oracle.com/javase/7/docs/api/java/sql/Array.html#getResultSet() diff --git a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryBaseArray.java b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryBaseArray.java index c9ad5c3691c5..4cff4db9d379 100644 --- a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryBaseArray.java +++ b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryBaseArray.java @@ -71,36 +71,24 @@ public final int getBaseType() { @Override public final Object getArray(Map> map) throws SQLException { - BigQueryJdbcSqlFeatureNotSupportedException ex = - new BigQueryJdbcSqlFeatureNotSupportedException(CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED); - LOG.severe(ex, CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED); - throw ex; + throw new BigQueryJdbcSqlFeatureNotSupportedException(CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED); } @Override public final Object getArray(long index, int count, Map> map) throws SQLException { - BigQueryJdbcSqlFeatureNotSupportedException ex = - new BigQueryJdbcSqlFeatureNotSupportedException(CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED); - LOG.severe(ex, CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED); - throw ex; + throw new BigQueryJdbcSqlFeatureNotSupportedException(CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED); } @Override public final ResultSet getResultSet(Map> map) throws SQLException { - BigQueryJdbcSqlFeatureNotSupportedException ex = - new BigQueryJdbcSqlFeatureNotSupportedException(CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED); - LOG.severe(ex, CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED); - throw ex; + throw new BigQueryJdbcSqlFeatureNotSupportedException(CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED); } @Override public final ResultSet getResultSet(long index, int count, Map> map) throws SQLException { - BigQueryJdbcSqlFeatureNotSupportedException ex = - new BigQueryJdbcSqlFeatureNotSupportedException(CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED); - LOG.severe(ex, CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED); - throw ex; + throw new BigQueryJdbcSqlFeatureNotSupportedException(CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED); } protected Object getArrayInternal(int fromIndex, int toIndexExclusive) { @@ -119,7 +107,7 @@ protected void ensureValid() throws IllegalStateException { LOG.finest("++enter++"); if (!this.valid) { IllegalStateException ex = new IllegalStateException(INVALID_ARRAY); - LOG.severe(ex, INVALID_ARRAY); + LOG.severe(INVALID_ARRAY, ex); throw ex; } } @@ -146,7 +134,7 @@ protected Tuple createRange(long index, int count, int size) String.format( "The array index is out of range: %d, number of elements: %d.", index + count, size)); - LOG.severe(ex, ex.getMessage()); + LOG.severe(ex.getMessage(), ex); throw ex; } long toIndex = normalisedFromIndex + count; diff --git a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryBaseResultSet.java b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryBaseResultSet.java index 7b37bf0dcecc..d63b72bb6c93 100644 --- a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryBaseResultSet.java +++ b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryBaseResultSet.java @@ -109,7 +109,6 @@ public void close() { protected SQLException createCoercionException( int columnIndex, Class targetClass, Exception cause) throws SQLException { - LOG.severe(cause, "Coercion failed"); checkClosed(); StandardSQLTypeName type; String typeName; @@ -127,7 +126,7 @@ protected SQLException createCoercionException( SQLException ex = new SQLException( "For a nested ResultSet from an Array, columnIndex must be 1 or 2.", cause); - LOG.severe(ex, "For a nested ResultSet from an Array, columnIndex must be 1 or 2."); + LOG.severe(ex.getMessage(), ex); throw ex; } } else { @@ -149,7 +148,7 @@ private StandardSQLTypeName getStandardSQLTypeName(int columnIndex) throws SQLEx } else if (columnIndex == 2) { if (this.schema == null || this.schema.getFields().isEmpty()) { SQLException ex = new SQLException("Schema not available for nested result set."); - LOG.severe(ex, "Schema not available for nested result set."); + LOG.severe("Schema not available for nested result set.", ex); throw ex; } Field arrayField = this.schema.getFields().get(0); @@ -157,7 +156,7 @@ private StandardSQLTypeName getStandardSQLTypeName(int columnIndex) throws SQLEx } else { SQLException ex = new SQLException("For a nested ResultSet from an Array, columnIndex must be 1 or 2."); - LOG.severe(ex, "For a nested ResultSet from an Array, columnIndex must be 1 or 2."); + LOG.severe("For a nested ResultSet from an Array, columnIndex must be 1 or 2.", ex); throw ex; } } else { @@ -165,7 +164,7 @@ private StandardSQLTypeName getStandardSQLTypeName(int columnIndex) throws SQLEx || columnIndex > this.schemaFieldList.size() || columnIndex < 1) { SQLException ex = new SQLException("Invalid column index: " + columnIndex); - LOG.severe(ex, "Invalid column index: " + columnIndex); + LOG.severe("Invalid column index: " + columnIndex, ex); throw ex; } Field field = this.schemaFieldList.get(columnIndex - 1); @@ -228,7 +227,9 @@ protected int getColumnIndex(String columnLabel) throws SQLException { LOG.finest("++enter++"); checkClosed(); if (columnLabel == null) { - throw new SQLException("Column label cannot be null"); + SQLException ex = new SQLException("Column label cannot be null"); + LOG.severe(ex.getMessage(), ex); + throw ex; } // use schema to get the column index, add 1 for SQL index return this.schemaFieldList.getIndex(columnLabel) + 1; diff --git a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryBaseStruct.java b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryBaseStruct.java index 1e247cdf0e11..4dd5676c5436 100644 --- a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryBaseStruct.java +++ b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryBaseStruct.java @@ -42,18 +42,12 @@ abstract class BigQueryBaseStruct implements java.sql.Struct { @Override public final String getSQLTypeName() throws SQLException { - BigQueryJdbcSqlFeatureNotSupportedException ex = - new BigQueryJdbcSqlFeatureNotSupportedException(CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED); - LOG.severe(ex, CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED); - throw ex; + throw new BigQueryJdbcSqlFeatureNotSupportedException(CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED); } @Override public final Object[] getAttributes(Map> map) throws SQLException { - BigQueryJdbcSqlFeatureNotSupportedException ex = - new BigQueryJdbcSqlFeatureNotSupportedException(CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED); - LOG.severe(ex, CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED); - throw ex; + throw new BigQueryJdbcSqlFeatureNotSupportedException(CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED); } static boolean isStruct(Field currentSchema) { @@ -97,7 +91,7 @@ public String toString() { sb.append("}"); return sb.toString(); } catch (SQLException e) { - LOG.severe(e, "Error converting struct to string"); + LOG.severe("Error converting struct to string", e); return "{ \"error\": \"Error converting struct to string: " + e.getMessage() + "\" }"; } } diff --git a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryCallableStatement.java b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryCallableStatement.java index 4de22e64e94d..0a3fc131e62a 100644 --- a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryCallableStatement.java +++ b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryCallableStatement.java @@ -940,7 +940,9 @@ public void registerOutParameter(int paramIndex, int sqlType) throws SQLExceptio BigQueryParameterHandler.BigQueryStatementParameterType.OUT, -1); } catch (Exception e) { - throw new SQLException(e); + SQLException ex = new SQLException(e); + LOG.severe("Failed to registerOutParameter", ex); + throw ex; } } @@ -957,7 +959,9 @@ public void registerOutParameter(String paramName, int sqlType) throws SQLExcept BigQueryParameterHandler.BigQueryStatementParameterType.OUT, -1); } catch (Exception e) { - throw new SQLException(e); + SQLException ex = new SQLException(e); + LOG.severe("Failed to registerOutParameter", ex); + throw ex; } } @@ -968,8 +972,11 @@ public void registerOutParameter(int paramIndex, int sqlType, int scale) throws "registerOutParameter: paramIndex %s, sqlType %s, scale %s", paramIndex, sqlType, scale); checkClosed(); if (sqlType != Types.NUMERIC && sqlType != Types.DECIMAL) { - throw new IllegalArgumentException( - String.format("registerOutParameter: Invalid sqlType passed in %s", sqlType)); + IllegalArgumentException ex = + new IllegalArgumentException( + String.format("registerOutParameter: Invalid sqlType passed in %s", sqlType)); + LOG.severe(ex.getMessage(), ex); + throw ex; } try { this.parameterHandler.setParameter( @@ -979,7 +986,9 @@ public void registerOutParameter(int paramIndex, int sqlType, int scale) throws BigQueryParameterHandler.BigQueryStatementParameterType.OUT, scale); } catch (Exception e) { - throw new SQLException(e); + SQLException ex = new SQLException(e); + LOG.severe("Failed to registerOutParameter", ex); + throw ex; } } @@ -1001,8 +1010,11 @@ public void registerOutParameter(String paramName, int sqlType, int scale) throw "registerOutParameter: paramIndex %s, sqlType %s, scale %s", paramName, sqlType, scale); checkClosed(); if (sqlType != Types.NUMERIC && sqlType != Types.DECIMAL) { - throw new IllegalArgumentException( - String.format("registerOutParameter: Invalid sqlType passed in %s", sqlType)); + IllegalArgumentException ex = + new IllegalArgumentException( + String.format("registerOutParameter: Invalid sqlType passed in %s", sqlType)); + LOG.severe(ex.getMessage(), ex); + throw ex; } try { this.parameterHandler.setParameter( @@ -1012,7 +1024,9 @@ public void registerOutParameter(String paramName, int sqlType, int scale) throw BigQueryParameterHandler.BigQueryStatementParameterType.OUT, scale); } catch (Exception e) { - throw new SQLException(e); + SQLException ex = new SQLException(e); + LOG.severe("Failed to registerOutParameter", ex); + throw ex; } } diff --git a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryConnection.java b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryConnection.java index 816d0a8547df..d2b7c9602309 100644 --- a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryConnection.java +++ b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryConnection.java @@ -191,9 +191,12 @@ public class BigQueryConnection extends BigQueryNoOpsConnection { } else if (parts.length == 1) { this.defaultDataset = DatasetId.of(parts[0]); } else { - throw new IllegalArgumentException( - "DefaultDataset format is invalid. Supported options are datasetId or" - + " projectId.datasetId"); + IllegalArgumentException ex = + new IllegalArgumentException( + "DefaultDataset format is invalid. Supported options are datasetId or" + + " projectId.datasetId"); + LOG.severe(ex.getMessage(), ex); + throw ex; } } this.location = ds.getLocation(); @@ -267,6 +270,7 @@ String getLibraryVersion(Class libraryClass) { version = props.getProperty("version.jdbc"); } } catch (IOException e) { + LOG.warning("Failed to load dependencies.properties"); return DEFAULT_VERSION; } @@ -297,8 +301,7 @@ BigQueryReadClient getBigQueryReadClient() { this.bigQueryReadClient = getBigQueryReadClientConnection(); } } catch (IOException e) { - LOG.severe(e, "Failed to initialize BigQueryReadClient"); - throw new BigQueryJdbcRuntimeException(e); + throw new BigQueryJdbcRuntimeException("Failed to initialize BigQueryReadClient", e); } return this.bigQueryReadClient; } @@ -309,8 +312,7 @@ BigQueryWriteClient getBigQueryWriteClient() { this.bigQueryWriteClient = getBigQueryWriteClientConnection(); } } catch (IOException e) { - LOG.severe(e, "Failed to initialize BigQueryWriteClient"); - throw new BigQueryJdbcRuntimeException(e); + throw new BigQueryJdbcRuntimeException("Failed to initialize BigQueryWriteClient", e); } return this.bigQueryWriteClient; } @@ -538,8 +540,7 @@ private void beginTransaction() { } this.transactionStarted = true; } catch (InterruptedException ex) { - LOG.severe(ex, "Failed to begin transaction"); - throw new BigQueryJdbcRuntimeException(ex); + throw new BigQueryJdbcRuntimeException("Failed to begin transaction", ex); } } @@ -744,9 +745,12 @@ public void commit() { checkClosed(); checkIfEnabledSession("commit"); if (!isTransactionStarted()) { - throw new IllegalStateException( - "Cannot commit without an active transaction. Please set setAutoCommit to false to start" - + " a transaction."); + IllegalStateException ex = + new IllegalStateException( + "Cannot commit without an active transaction. Please set setAutoCommit to false to start" + + " a transaction."); + LOG.severe(ex.getMessage(), ex); + throw ex; } commitTransaction(); if (!getAutoCommit()) { @@ -760,9 +764,12 @@ public void rollback() throws SQLException { checkClosed(); checkIfEnabledSession("rollback"); if (!isTransactionStarted()) { - throw new IllegalStateException( - "Cannot rollback without an active transaction. Please set setAutoCommit to false to" - + " start a transaction."); + IllegalStateException ex = + new IllegalStateException( + "Cannot rollback without an active transaction. Please set setAutoCommit to false to" + + " start a transaction."); + LOG.severe(ex.getMessage(), ex); + throw ex; } try { QueryJobConfiguration transactionRollbackJobConfig = @@ -776,8 +783,7 @@ public void rollback() throws SQLException { beginTransaction(); } } catch (InterruptedException | BigQueryException ex) { - LOG.severe(ex, "Failed to rollback transaction"); - throw new BigQueryJdbcException(ex); + throw new BigQueryJdbcException("Failed to rollback transaction", ex); } } @@ -852,11 +858,9 @@ public void close() throws SQLException { } this.openStatements.clear(); } catch (ConcurrentModificationException ex) { - LOG.severe(ex, "Concurrent modification during close"); - throw new BigQueryJdbcException(ex); + throw new BigQueryJdbcException("Concurrent modification during close", ex); } catch (InterruptedException e) { - LOG.severe(e, "Interrupted during close"); - throw new BigQueryJdbcRuntimeException(e); + throw new BigQueryJdbcRuntimeException("Interrupted during close", e); } this.isClosed = true; } @@ -868,14 +872,20 @@ public boolean isClosed() { private void checkClosed() { if (isClosed()) { - throw new IllegalStateException("This " + getClass().getName() + " has been closed"); + IllegalStateException ex = + new IllegalStateException("This " + getClass().getName() + " has been closed"); + LOG.severe(ex.getMessage(), ex); + throw ex; } } private void checkIfEnabledSession(String methodName) { if (!this.enableSession) { - throw new IllegalStateException( - String.format("Session needs to be enabled to use %s method.", methodName)); + IllegalStateException ex = + new IllegalStateException( + String.format("Session needs to be enabled to use %s method.", methodName)); + LOG.severe(ex.getMessage(), ex); + throw ex; } } @@ -1060,7 +1070,7 @@ private void commitTransaction() { commitJob.waitFor(); this.transactionStarted = false; } catch (InterruptedException ex) { - throw new BigQueryJdbcRuntimeException(ex); + throw new BigQueryJdbcRuntimeException("Interrupted during commitTransaction", ex); } } diff --git a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryDaemonPollingTask.java b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryDaemonPollingTask.java index 328c040d20d4..3c065517f00e 100644 --- a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryDaemonPollingTask.java +++ b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryDaemonPollingTask.java @@ -113,13 +113,10 @@ else if (referenceQueueJsonRs != null) { reference.clear(); } } else { - BigQueryJdbcRuntimeException ex = new BigQueryJdbcRuntimeException("Null Reference Queue"); - LOG.severe(ex, "Null Reference Queue"); - throw ex; + throw new BigQueryJdbcRuntimeException("Null Reference Queue"); } } catch (InterruptedException ex) { - LOG.severe(ex, "Interrupted in GC daemon task"); - throw new BigQueryJdbcRuntimeException(ex); + throw new BigQueryJdbcRuntimeException("Interrupted in GC daemon task", ex); } } } diff --git a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryDatabaseMetaData.java b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryDatabaseMetaData.java index 131ef3041eee..e81601bd77ab 100644 --- a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryDatabaseMetaData.java +++ b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryDatabaseMetaData.java @@ -1251,9 +1251,11 @@ List listMatchingProcedureIdsFromDatasets( for (Dataset dataset : datasetsToScan) { if (Thread.currentThread().isInterrupted()) { - logger.warning( - "Interrupted during submission of routine listing tasks for catalog: " + catalogParam); - throw new InterruptedException("Interrupted while listing routines"); + InterruptedException ex = + new InterruptedException( + "Interrupted while listing routines for catalog: " + catalogParam); + logger.severe(ex.getMessage(), ex); + throw ex; } final DatasetId currentDatasetId = dataset.getDatasetId(); Callable> listCallable = @@ -1281,10 +1283,12 @@ List listMatchingProcedureIdsFromDatasets( for (Future> listFuture : listRoutineFutures) { if (Thread.currentThread().isInterrupted()) { - logger.warning( - "Interrupted while collecting routine list results for catalog: " + catalogParam); listRoutineFutures.forEach(f -> f.cancel(true)); - throw new InterruptedException("Interrupted while collecting routine lists"); + InterruptedException ex = + new InterruptedException( + "Interrupted while collecting routine lists for catalog: " + catalogParam); + logger.severe(ex.getMessage(), ex); + throw ex; } try { List listedRoutines = listFuture.get(); @@ -1327,8 +1331,10 @@ List fetchFullRoutineDetailsForIds( for (RoutineId procId : procedureIdsToGet) { if (Thread.currentThread().isInterrupted()) { - logger.warning("Interrupted during submission of getRoutine detail tasks."); - throw new InterruptedException("Interrupted while submitting getRoutine tasks"); + InterruptedException ex = + new InterruptedException("Interrupted while submitting getRoutine tasks"); + logger.severe(ex.getMessage(), ex); + throw ex; } final RoutineId currentProcId = procId; Callable getCallable = @@ -1350,9 +1356,11 @@ List fetchFullRoutineDetailsForIds( for (Future getFuture : getRoutineFutures) { if (Thread.currentThread().isInterrupted()) { - logger.warning("Interrupted while collecting getRoutine detail results."); getRoutineFutures.forEach(f -> f.cancel(true)); // Cancel remaining - throw new InterruptedException("Interrupted while collecting Routine details"); + InterruptedException ex = + new InterruptedException("Interrupted while collecting Routine details"); + logger.severe(ex.getMessage(), ex); + throw ex; } try { Routine fullRoutine = getFuture.get(); @@ -1382,8 +1390,10 @@ void submitProcedureArgumentProcessingJobs( for (Routine fullRoutine : fullRoutines) { if (Thread.currentThread().isInterrupted()) { - logger.warning("Interrupted during submission of argument processing tasks."); - throw new InterruptedException("Interrupted while submitting argument processing jobs"); + InterruptedException ex = + new InterruptedException("Interrupted while submitting argument processing jobs"); + logger.severe(ex.getMessage(), ex); + throw ex; } if (fullRoutine != null) { if ("PROCEDURE".equalsIgnoreCase(fullRoutine.getRoutineType())) { @@ -2639,8 +2649,7 @@ public ResultSet getPrimaryKeys(String catalog, String schema, String table) thr String formattedSql = replaceSqlParameters(sql, catalog, schema, table); return this.statement.executeQuery(formattedSql); } catch (SQLException e) { - LOG.severe(e, "Error executing getPrimaryKeys"); - throw new BigQueryJdbcException(e); + throw new BigQueryJdbcException("Error executing getPrimaryKeys", e); } } @@ -2655,8 +2664,7 @@ public ResultSet getImportedKeys(String catalog, String schema, String table) String formattedSql = replaceSqlParameters(sql, catalog, schema, table); return this.statement.executeQuery(formattedSql); } catch (SQLException e) { - LOG.severe(e, "Error executing getImportedKeys"); - throw new BigQueryJdbcException(e); + throw new BigQueryJdbcException("Error executing getImportedKeys", e); } } @@ -2671,8 +2679,7 @@ public ResultSet getExportedKeys(String catalog, String schema, String table) String formattedSql = replaceSqlParameters(sql, catalog, schema, table); return this.statement.executeQuery(formattedSql); } catch (SQLException e) { - LOG.severe(e, "Error executing getExportedKeys"); - throw new BigQueryJdbcException(e); + throw new BigQueryJdbcException("Error executing getExportedKeys", e); } } @@ -2701,8 +2708,7 @@ public ResultSet getCrossReference( foreignTable); return this.statement.executeQuery(formattedSql); } catch (SQLException e) { - LOG.severe(e, "Error executing getCrossReference"); - throw new BigQueryJdbcException(e); + throw new BigQueryJdbcException("Error executing getCrossReference", e); } } @@ -5265,7 +5271,7 @@ private void loadDriverVersionProperties() { String errorMessage = "Could not find dependencies.properties. Driver version information is unavailable."; IllegalStateException ex = new IllegalStateException(errorMessage); - LOG.severe(ex, errorMessage); + LOG.severe(errorMessage, ex); throw ex; } props.load(input); @@ -5274,7 +5280,7 @@ private void loadDriverVersionProperties() { String errorMessage = "The property version.jdbc not found or empty in dependencies.properties."; IllegalStateException ex = new IllegalStateException(errorMessage); - LOG.severe(ex, errorMessage); + LOG.severe(errorMessage, ex); throw ex; } parsedDriverVersion.compareAndSet(null, versionString.trim()); @@ -5294,7 +5300,7 @@ private void loadDriverVersionProperties() { + " unavailable. Error: " + e.getMessage(); IllegalStateException ex = new IllegalStateException(errorMessage, e); - LOG.severe(ex, errorMessage); + LOG.severe(errorMessage, ex); throw ex; } } diff --git a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryDriver.java b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryDriver.java index 4d09ac3e0270..e62d93fca0ed 100644 --- a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryDriver.java +++ b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryDriver.java @@ -66,7 +66,7 @@ public class BigQueryDriver implements Driver { } catch (SQLException e) { ExceptionInInitializerError ex = new ExceptionInInitializerError("Registering driver failed: " + e.getMessage()); - LOG.severe(ex, ex.getMessage()); + LOG.severe(ex.getMessage(), ex); throw ex; } LoadBalancerRegistry.getDefaultRegistry().register(new PickFirstLoadBalancerProvider()); @@ -101,7 +101,7 @@ public static BigQueryDriver getRegisteredDriver() throws IllegalStateException IllegalStateException ex = new IllegalStateException( "Driver is not registered (or it has not been registered using Driver.register() method)"); - LOG.severe(ex, ex.getMessage()); + LOG.severe(ex.getMessage(), ex); throw ex; } @@ -133,8 +133,7 @@ public Connection connect(String url, Properties info) throws SQLException { try { BigQueryJdbcUrlUtility.parseUrl(connectionUri); } catch (BigQueryJdbcRuntimeException e) { - LOG.severe(e, "Failed to parse connection URL"); - throw new BigQueryJdbcException(e.getMessage(), e); + throw new BigQueryJdbcException("Failed to parse connection URL", e); } DataSource ds = DataSource.fromUrl(connectionUri); @@ -193,9 +192,7 @@ public Connection connect(String url, Properties info) throws SQLException { public boolean acceptsURL(String url) throws SQLException { LOG.finest("++enter++"); if (url == null || url.isEmpty()) { - BigQueryJdbcException ex = new BigQueryJdbcException("Connection URL is null."); - LOG.severe(ex, ex.getMessage()); - throw ex; + throw new BigQueryJdbcException("Connection URL is null."); } return url.startsWith("jdbc:bigquery:"); } diff --git a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcCustomLogger.java b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcCustomLogger.java index 31b8b8ba7a88..6932f9b1a2a8 100644 --- a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcCustomLogger.java +++ b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcCustomLogger.java @@ -21,14 +21,14 @@ import java.util.logging.LogRecord; import java.util.logging.Logger; -class BigQueryJdbcCustomLogger extends Logger { +public class BigQueryJdbcCustomLogger extends Logger { protected BigQueryJdbcCustomLogger(String name, String resourceBundleName) { super(name, resourceBundleName); this.setParent(BigQueryJdbcRootLogger.getRootLogger()); } - BigQueryJdbcCustomLogger(String name) { + public BigQueryJdbcCustomLogger(String name) { this(name, null); this.setParent(BigQueryJdbcRootLogger.getRootLogger()); } @@ -48,7 +48,8 @@ private void logWithCaller(Level level, Throwable thrown, Supplier msgSu for (StackTraceElement element : stackTrace) { String className = element.getClassName(); - if (!className.equals(BigQueryJdbcCustomLogger.class.getName())) { + if (!className.equals(BigQueryJdbcCustomLogger.class.getName()) + && !className.startsWith("com.google.cloud.bigquery.exception.")) { sourceClass = className; sourceMethod = element.getMethodName(); break; @@ -102,11 +103,11 @@ void severe(String format, Object... args) { logWithCaller(Level.SEVERE, () -> String.format(format, args)); } - void severe(Throwable thrown, String msg) { + public void severe(String msg, Throwable thrown) { logWithCaller(Level.SEVERE, thrown, () -> msg); } - void severe(Throwable thrown, String format, Object... args) { + public void severe(String format, Throwable thrown, Object... args) { logWithCaller(Level.SEVERE, thrown, () -> String.format(format, args)); } } diff --git a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcOAuthUtility.java b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcOAuthUtility.java index 2ee4277e7726..f49f503a6dec 100644 --- a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcOAuthUtility.java +++ b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcOAuthUtility.java @@ -121,7 +121,7 @@ static Map parseOAuthProperties(DataSource ds, String callerClas try { authType = AuthType.fromValue(ds.getOAuthType()); } catch (NumberFormatException exception) { - LOG.severe(exception, OAUTH_TYPE_ERROR_MESSAGE); + LOG.severe(OAUTH_TYPE_ERROR_MESSAGE, exception); throw new IllegalArgumentException(OAUTH_TYPE_ERROR_MESSAGE); } oauthProperties.put(BigQueryJdbcUrlUtility.OAUTH_TYPE_PROPERTY_NAME, String.valueOf(authType)); @@ -293,7 +293,9 @@ static GoogleCredentials getCredentials( credentials = getExternalAccountAuthCredentials(authProperties, callerClassName); break; default: - throw new IllegalStateException(OAUTH_TYPE_ERROR_MESSAGE); + IllegalStateException ex = new IllegalStateException(OAUTH_TYPE_ERROR_MESSAGE); + LOG.severe(ex.getMessage(), ex); + throw ex; } return getServiceAccountImpersonatedCredentials( @@ -369,7 +371,6 @@ private static GoogleCredentials getGoogleServiceAccountCredentials( builder = ServiceAccountCredentials.newBuilder().setClientEmail(pvtEmail).setPrivateKey(key); } else { - LOG.severe("No valid Service Account credentials provided."); throw new BigQueryJdbcRuntimeException("No valid credentials provided."); } @@ -383,8 +384,8 @@ private static GoogleCredentials getGoogleServiceAccountCredentials( overrideProperties.get(BigQueryJdbcUrlUtility.UNIVERSE_DOMAIN_OVERRIDE_PROPERTY_NAME)); } } catch (URISyntaxException | IOException e) { - LOG.severe("Validation failure for Service Account credentials."); - throw new BigQueryJdbcRuntimeException(e); + throw new BigQueryJdbcRuntimeException( + "Validation failure for Service Account credentials.", e); } LOG.info("GoogleCredentials instantiated. Auth Method: Service Account."); return builder.build(); @@ -459,7 +460,6 @@ private static GoogleCredentials getGoogleUserAccountCredentials( Matcher m = p.matcher(response); if (!m.find()) { - LOG.severe("Could not retrieve the code for user auth"); throw new BigQueryJdbcRuntimeException("Could not retrieve the code for user auth"); } code = m.group(); @@ -469,15 +469,13 @@ private static GoogleCredentials getGoogleUserAccountCredentials( socket.close(); serverSocket.close(); } else { - LOG.severe("User auth only supported in desktop environments"); throw new BigQueryJdbcRuntimeException("User auth only supported in desktop environments"); } return getCredentialsFromCode(userAuthorizer, code, callerClassName); } catch (IOException | URISyntaxException ex) { - LOG.severe( - "Failed to establish connection using User Account authentication: %s", ex.getMessage()); - throw new BigQueryJdbcRuntimeException(ex); + throw new BigQueryJdbcRuntimeException( + "Failed to establish connection using User Account authentication", ex); } } @@ -516,7 +514,8 @@ static GoogleCredentials getPreGeneratedTokensCredentials( return getPreGeneratedRefreshTokenCredentials( authProperties, overrideProperties, callerClassName); } catch (URISyntaxException ex) { - throw new BigQueryJdbcRuntimeException(ex); + throw new BigQueryJdbcRuntimeException( + "URISyntaxException during getPreGeneratedTokensCredentials", ex); } } else { return getPreGeneratedAccessTokenCredentials( @@ -571,8 +570,8 @@ private static GoogleCredentials getApplicationDefaultCredentials(String callerC return credentials; } catch (IOException exception) { - // TODO throw exception - throw new BigQueryJdbcRuntimeException("Application default credentials not found."); + throw new BigQueryJdbcRuntimeException( + "Application default credentials not found.", exception); } } @@ -621,11 +620,14 @@ private static GoogleCredentials getExternalAccountAuthCredentials( return ExternalAccountCredentials.fromStream( new ByteArrayInputStream(jsonObject.toString().getBytes())); } else { - throw new IllegalArgumentException( - "Insufficient info provided for external authentication"); + IllegalArgumentException ex = + new IllegalArgumentException("Insufficient info provided for external authentication"); + LOG.severe(ex.getMessage(), ex); + throw ex; } } catch (IOException e) { - throw new BigQueryJdbcRuntimeException(e); + throw new BigQueryJdbcRuntimeException( + "IOException during getExternalAccountAuthCredentials", e); } } @@ -678,10 +680,12 @@ private static GoogleCredentials getServiceAccountImpersonatedCredentials( try { impersonationLifetimeInt = Integer.parseInt(impersonationLifetime); } catch (NumberFormatException e) { - LOG.severe("Invalid value for ServiceAccountImpersonationTokenLifetime."); - throw new IllegalArgumentException( - "Invalid value for ServiceAccountImpersonationTokenLifetime: must be a positive integer.", - e); + IllegalArgumentException ex = + new IllegalArgumentException( + "Invalid value for ServiceAccountImpersonationTokenLifetime: must be a positive integer.", + e); + LOG.severe(ex.getMessage(), ex); + throw ex; } return ImpersonatedCredentials.create( @@ -708,7 +712,9 @@ static PrivateKey privateKeyFromPkcs8(String privateKeyPkcs8) { Reader reader = new StringReader(privateKeyPkcs8); PemReader.Section section = readFirstSectionAndClose(reader, "PRIVATE KEY"); if (section == null) { - throw new IOException("Invalid PKCS#8 data."); + IOException ex = new IOException("Invalid PKCS#8 data."); + LOG.severe(ex.getMessage(), ex); + throw ex; } byte[] bytes = section.getBase64DecodedBytes(); PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(bytes); @@ -739,7 +745,9 @@ static AuthType fromValue(int value) { return authType; } } - throw new IllegalStateException(OAUTH_TYPE_ERROR_MESSAGE + ": " + value); + IllegalStateException ex = new IllegalStateException(OAUTH_TYPE_ERROR_MESSAGE + ": " + value); + LOG.severe(ex.getMessage(), ex); + throw ex; } } } diff --git a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcProxyUtility.java b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcProxyUtility.java index e80538be3384..7c495e801537 100644 --- a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcProxyUtility.java +++ b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcProxyUtility.java @@ -76,7 +76,7 @@ static Map parseProxyProperties(DataSource ds, String callerClas String.format( "Illegal port number provided %s. Please provide a valid port number.", proxyPort)); - LOG.severe(ex, ex.getMessage()); + LOG.severe(ex.getMessage(), ex); throw ex; } proxyProperties.put(BigQueryJdbcUrlUtility.PROXY_PORT_PROPERTY_NAME, proxyPort); @@ -97,9 +97,7 @@ static Map parseProxyProperties(DataSource ds, String callerClas new IllegalArgumentException( "Both ProxyHost and ProxyPort parameters need to be specified. No defaulting behavior" + " occurs."); - LOG.severe( - ex, - "Both ProxyHost and ProxyPort parameters need to be specified. No defaulting behavior occurs."); + LOG.severe(ex.getMessage(), ex); throw ex; } boolean isMissingProxyUidOrPwdWhenAuthSet = @@ -108,8 +106,7 @@ static Map parseProxyProperties(DataSource ds, String callerClas IllegalArgumentException ex = new IllegalArgumentException( "Both ProxyUid and ProxyPwd parameters need to be specified for authentication."); - LOG.severe( - ex, "Both ProxyUid and ProxyPwd parameters need to be specified for authentication."); + LOG.severe(ex.getMessage(), ex); throw ex; } boolean isProxyAuthSetWithoutProxySettings = proxyUid != null && proxyHost == null; @@ -117,9 +114,7 @@ static Map parseProxyProperties(DataSource ds, String callerClas IllegalArgumentException ex = new IllegalArgumentException( "Proxy authentication provided via connection string with no proxy host or port set."); - LOG.severe( - ex, - "Proxy authentication provided via connection string with no proxy host or port set."); + LOG.severe(ex.getMessage(), ex); throw ex; } return proxyProperties; @@ -207,8 +202,8 @@ private static HttpTransportFactory getHttpTransportFactory( .setSSLSocketFactory(sslSocketFactory) .build()); } catch (IOException | GeneralSecurityException e) { - LOG.severe(e, "Failed to configure SSL TrustStore for HTTP transport"); - throw new BigQueryJdbcRuntimeException(e); + throw new BigQueryJdbcRuntimeException( + "Failed to configure SSL TrustStore for HTTP transport", e); } } addAuthToProxyIfPresent(proxyProperties, httpClientBuilder, callerClassName); @@ -297,8 +292,8 @@ public ProxiedSocketAddress proxyFor(SocketAddress socketAddress) { .sslContext(grpcSslContext); } catch (IOException | GeneralSecurityException e) { - LOG.severe(e, "Failed to configure SSL TrustStore for GRPC channel"); - throw new BigQueryJdbcRuntimeException(e); + throw new BigQueryJdbcRuntimeException( + "Failed to configure SSL TrustStore for GRPC channel", e); } } return managedChannelBuilder; diff --git a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcTypeMappings.java b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcTypeMappings.java index b95ac0230264..8ccd2a23dcc0 100644 --- a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcTypeMappings.java +++ b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcTypeMappings.java @@ -33,6 +33,8 @@ @InternalApi class BigQueryJdbcTypeMappings { + private static final BigQueryJdbcCustomLogger LOG = + new BigQueryJdbcCustomLogger(BigQueryJdbcTypeMappings.class.getName()); static final Map> standardSQLToJavaTypeMapping = ImmutableMap.ofEntries( diff --git a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcUrlUtility.java b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcUrlUtility.java index 98ec15a94ebf..afc300a6d97c 100644 --- a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcUrlUtility.java +++ b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcUrlUtility.java @@ -706,11 +706,8 @@ private static Map parseUrlInternal(String url) { // Some tools can pass unknown keys. In order not to break compatibility, throw // an exception only with incorrect format, otherwise log an error. if (kv.length != 2) { - BigQueryJdbcRuntimeException ex = - new BigQueryJdbcRuntimeException( - String.format("Wrong value or unknown setting: %s", safeRef)); - LOG.severe(ex, ex.getMessage()); - throw ex; + throw new BigQueryJdbcRuntimeException( + String.format("Wrong value or unknown setting: %s", safeRef)); } else { LOG.warning("Wrong value or unknown setting: %s", safeRef); continue; @@ -757,16 +754,15 @@ static boolean convertIntToBoolean(String value, String propertyName) { } } catch (NumberFormatException ex) { - LOG.severe( - ex, - "Invalid value for %s. For Boolean connection properties, use 0 for false and 1 for true.", - propertyName); - throw new IllegalArgumentException( - String.format( - "Invalid value for %s. For Boolean connection properties, use 0 for false and 1 for" - + " true.", - propertyName), - ex); + IllegalArgumentException e = + new IllegalArgumentException( + String.format( + "Invalid value for %s. For Boolean connection properties, use 0 for false and 1 for" + + " true.", + propertyName), + ex); + LOG.severe(e.getMessage(), e); + throw e; } if (integerValue == 1) { return true; @@ -779,7 +775,7 @@ static boolean convertIntToBoolean(String value, String propertyName) { "Invalid value for %s. For Boolean connection properties, use 0 for false and 1 for" + " true.", propertyName)); - LOG.severe(ex, ex.getMessage()); + LOG.severe(ex.getMessage(), ex); throw ex; } } diff --git a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJsonResultSet.java b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJsonResultSet.java index da2ade028e9f..c59061b25467 100644 --- a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJsonResultSet.java +++ b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJsonResultSet.java @@ -137,8 +137,11 @@ public boolean next() throws SQLException { // We are working with the nested record, the cursor would have been // populated. if (this.cursor == null || this.cursor.getArrayFieldValueList() == null) { - throw new IllegalStateException( - "Cursor/ArrayFieldValueList can not be null working with the nested record"); + IllegalStateException ex = + new IllegalStateException( + "Cursor/ArrayFieldValueList can not be null working with the nested record"); + LOG.severe(ex.getMessage(), ex); + throw ex; } // Check if there's a next record in the array which can be read if (this.nestedRowIndex < (this.toIndexExclusive - 1)) { @@ -172,10 +175,11 @@ public boolean next() throws SQLException { // Cursor has been advanced return true; - } catch (InterruptedException ex) { + } catch (InterruptedException e) { + throw new BigQueryJdbcRuntimeException( "Error occurred while advancing the cursor. This could happen when connection is closed while we call the next method", - ex); + e); } } } @@ -236,12 +240,17 @@ private FieldValue getObjectInternal(int columnIndex) throws SQLException { // BigQuery doesn't support multidimensional arrays, so just the default row // num column (1) and the actual column (2) is supposed to be read if (!validIndexForNestedResultSet) { - throw new IllegalArgumentException( - "Column index is required to be 1 or 2 for the nested arrays"); + IllegalArgumentException ex = + new IllegalArgumentException( + "Column index is required to be 1 or 2 for the nested arrays"); + LOG.severe(ex.getMessage(), ex); + throw ex; } if (this.cursor.getArrayFieldValueList() == null || this.cursor.getArrayFieldValueList().get(this.nestedRowIndex) == null) { - throw new IllegalStateException("ArrayFieldValueList cannot be null"); + IllegalStateException ex = new IllegalStateException("ArrayFieldValueList cannot be null"); + LOG.severe(ex.getMessage(), ex); + throw ex; } // For Arrays the first column is Index, ref: diff --git a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryParameterHandler.java b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryParameterHandler.java index 06d72ad2b7c5..8daaf99b62a8 100644 --- a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryParameterHandler.java +++ b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryParameterHandler.java @@ -66,7 +66,7 @@ QueryJobConfiguration.Builder configureParameters( QueryParameterValue.of(parameterValue, sqlType)); } } catch (NullPointerException e) { - LOG.severe(e, "Null parameter mapping encountered."); + LOG.severe("Null parameter mapping encountered.", e); if (e.getMessage().contains("Null type")) { throw new BigQueryJdbcException("One or more parameters missing in Prepared statement.", e); } @@ -106,7 +106,7 @@ private void checkValidIndex(int parameterIndex) { if (parameterIndex > this.parametersArraySize) { IndexOutOfBoundsException ex = new IndexOutOfBoundsException("All parameters already provided."); - LOG.severe(ex, "All parameters already provided."); + LOG.severe("All parameters already provided.", ex); throw ex; } } @@ -157,7 +157,7 @@ void setParameter( if (paramName == null || paramName.isEmpty()) { IllegalArgumentException ex = new IllegalArgumentException("paramName cannot be null or empty"); - LOG.severe(ex, "paramName cannot be null or empty"); + LOG.severe("paramName cannot be null or empty", ex); throw ex; } BigQueryJdbcParameter parameter = null; diff --git a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryPooledConnection.java b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryPooledConnection.java index f3f5e2286536..99dea20e21ce 100644 --- a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryPooledConnection.java +++ b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryPooledConnection.java @@ -65,7 +65,9 @@ boolean isListenerPooled(ConnectionEventListener l) { public synchronized Connection getConnection() throws SQLException { LOG.finest("++enter++"); if (inUse) { - throw new SQLException("PooledConnection is already in use."); + SQLException ex = new SQLException("PooledConnection is already in use."); + LOG.severe(ex.getMessage(), ex); + throw ex; } inUse = true; // Return a wrapper around the underlying physical connection. @@ -140,14 +142,20 @@ public synchronized void fireConnectionError(SQLException e) { @Override public void addStatementEventListener(StatementEventListener arg0) { - throw new UnsupportedOperationException( - "Method 'addStatementEventListener' is not supported by the BQ Driver"); + UnsupportedOperationException ex = + new UnsupportedOperationException( + "Method 'addStatementEventListener' is not supported by the BQ Driver"); + LOG.severe(ex.getMessage(), ex); + throw ex; } @Override public void removeStatementEventListener(StatementEventListener arg0) { - throw new UnsupportedOperationException( - "Method 'removeStatementEventListener' is not supported by the BQ Driver"); + UnsupportedOperationException ex = + new UnsupportedOperationException( + "Method 'removeStatementEventListener' is not supported by the BQ Driver"); + LOG.severe(ex.getMessage(), ex); + throw ex; } // Inner class: Connection Wrapper around the actual physical Connection diff --git a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryPreparedStatement.java b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryPreparedStatement.java index e38a2f809ec0..356578e8dd27 100644 --- a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryPreparedStatement.java +++ b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryPreparedStatement.java @@ -98,7 +98,7 @@ public ResultSet executeQuery() throws SQLException { jobConfiguration = this.parameterHandler.configureParameters(jobConfiguration); runQuery(this.currentQuery, jobConfiguration.build()); } catch (InterruptedException ex) { - throw new BigQueryJdbcRuntimeException(ex); + throw new BigQueryJdbcRuntimeException("Interrupted during executeQuery", ex); } return getCurrentResultSet(); } @@ -113,7 +113,7 @@ public long executeLargeUpdate() throws SQLException { jobConfiguration = this.parameterHandler.configureParameters(jobConfiguration); runQuery(this.currentQuery, jobConfiguration.build()); } catch (InterruptedException ex) { - throw new BigQueryJdbcRuntimeException(ex); + throw new BigQueryJdbcRuntimeException("Interrupted during executeLargeUpdate", ex); } return this.currentUpdateCount; } @@ -134,7 +134,7 @@ public boolean execute() throws SQLException { jobConfiguration = this.parameterHandler.configureParameters(jobConfiguration); runQuery(this.currentQuery, jobConfiguration.build()); } catch (InterruptedException ex) { - throw new BigQueryJdbcRuntimeException(ex); + throw new BigQueryJdbcRuntimeException("Interrupted during execute", ex); } return getCurrentResultSet() != null; } @@ -291,8 +291,7 @@ public int[] executeBatch() throws SQLException { return insertArray; } catch (DescriptorValidationException | IOException | InterruptedException e) { - LOG.severe(e, "Failed to execute batch with Write API"); - throw new BigQueryJdbcRuntimeException(e); + throw new BigQueryJdbcRuntimeException("Failed to execute batch with Write API", e); } } else { @@ -320,11 +319,9 @@ public int[] executeBatch() throws SQLException { } return result; } catch (InterruptedException ex) { - LOG.severe(ex, "Interrupted during individual INSERT batch"); - throw new BigQueryJdbcRuntimeException(ex); + throw new BigQueryJdbcRuntimeException("Interrupted during individual INSERT batch", ex); } catch (SQLException e) { - LOG.severe(e, "SQL error during individual INSERT batch"); - throw new BigQueryJdbcException(e); + throw new BigQueryJdbcException("SQL error during individual INSERT batch", e); } } } @@ -370,14 +367,11 @@ private long bulkInsertWithWriteAPI(BigQueryWriteClient bigQueryWriteClient) jsonArray = new JsonArray(); } } else { - BigQueryJdbcException ex = - new BigQueryJdbcException("Mismatch between field count and parameter count."); - LOG.severe(ex, "Mismatch between field count and parameter count."); - throw ex; + throw new BigQueryJdbcException("Mismatch between field count and parameter count."); } } } catch (BigQueryJdbcException e) { - throw new RuntimeException(e); + throw new BigQueryJdbcException("BigQueryJdbcException during bulkInsertWithWriteAPI", e); } long rowCount = bulkInsertWriter.cleanup(bigQueryWriteClient); @@ -390,9 +384,7 @@ private long bulkInsertWithWriteAPI(BigQueryWriteClient bigQueryWriteClient) BatchCommitWriteStreamsResponse commitResponse = bigQueryWriteClient.batchCommitWriteStreams(commitRequest); if (commitResponse.hasCommitTime() == false) { - BigQueryJdbcException ex = new BigQueryJdbcException("Error committing the streams"); - LOG.severe(ex, "Error committing the streams"); - throw ex; + throw new BigQueryJdbcException("Error committing the streams"); } LOG.finest("Commit called."); return rowCount; @@ -403,11 +395,8 @@ private void setInsertMetadata(QueryStatistics statistics) throws SQLException { if (!statistics.getStatementType().equals(StatementType.INSERT) || statistics.getSchema() == null || statistics.getReferencedTables().stream().distinct().count() > 1) { - BigQueryJdbcException ex = - new BigQueryJdbcException( - "Use java.sql.Statement.executeBatch() for heterogeneous DML batches"); - LOG.severe(ex, "Use java.sql.Statement.executeBatch() for heterogeneous DML batches"); - throw ex; + throw new BigQueryJdbcException( + "Use java.sql.Statement.executeBatch() for heterogeneous DML batches"); } this.insertSchema = statistics.getSchema(); diff --git a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryStatement.java b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryStatement.java index ac2ee99fdb53..c08a18fea3a5 100644 --- a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryStatement.java +++ b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryStatement.java @@ -241,8 +241,7 @@ public ResultSet executeQuery(String sql) throws SQLException { setDestinationDatasetAndTableInJobConfig(getJobConfig(sql).build()); runQuery(sql, jobConfiguration); } catch (InterruptedException ex) { - LOG.severe(ex, "Interrupted during executeQuery"); - throw new BigQueryJdbcException(ex); + throw new BigQueryJdbcException("Interrupted during executeQuery", ex); } if (!isSingularResultSet()) { @@ -261,8 +260,7 @@ public long executeLargeUpdate(String sql) throws SQLException { QueryJobConfiguration.Builder jobConfiguration = getJobConfig(sql); runQuery(sql, jobConfiguration.build()); } catch (InterruptedException ex) { - LOG.severe(ex, "Interrupted during executeLargeUpdate"); - throw new BigQueryJdbcRuntimeException(ex); + throw new BigQueryJdbcRuntimeException("Interrupted during executeLargeUpdate", ex); } if (this.currentUpdateCount == -1) { throw new BigQueryJdbcException( @@ -299,7 +297,7 @@ public boolean execute(String sql) throws SQLException { } runQuery(sql, jobConfiguration); } catch (InterruptedException ex) { - throw new BigQueryJdbcRuntimeException(ex); + throw new BigQueryJdbcRuntimeException("Interrupted during execute", ex); } return getCurrentResultSet() != null; } @@ -313,9 +311,10 @@ StatementType getStatementType(QueryJobConfiguration queryJobConfiguration) thro job = bigQuery.create(JobInfo.of(dryRunJobConfiguration)); } catch (BigQueryException ex) { if (ex.getMessage().contains("Syntax error")) { - throw new BigQueryJdbcSqlSyntaxErrorException(ex); + throw new BigQueryJdbcSqlSyntaxErrorException( + "BigQueryException during getStatementType", ex); } - throw new BigQueryJdbcException(ex); + throw new BigQueryJdbcException("BigQueryException during getStatementType", ex); } QueryStatistics statistics = job.getStatistics(); return statistics.getStatementType(); @@ -346,9 +345,10 @@ QueryStatistics getQueryStatistics(QueryJobConfiguration queryJobConfiguration) return job.getStatistics(); } catch (BigQueryException ex) { if (ex.getMessage().contains("Syntax error")) { - throw new BigQueryJdbcSqlSyntaxErrorException(ex); + throw new BigQueryJdbcSqlSyntaxErrorException( + "BigQueryException during getQueryStatistics", ex); } - throw new BigQueryJdbcException(ex); + throw new BigQueryJdbcException("BigQueryException during getQueryStatistics", ex); } } @@ -416,7 +416,9 @@ public int getQueryTimeout() { @Override public void setQueryTimeout(int seconds) { if (seconds < 0) { - throw new IllegalArgumentException("Query Timeout should be >= 0."); + IllegalArgumentException ex = new IllegalArgumentException("Query Timeout should be >= 0."); + LOG.severe(ex.getMessage(), ex); + throw ex; } this.queryTimeout = seconds; } @@ -442,7 +444,7 @@ public void cancel() throws SQLException { || e.getMessage().contains("Error: 3848323"))) { LOG.warning("Attempted to cancel a job that was already done: " + jobId); } else { - throw new BigQueryJdbcException(e); + throw new BigQueryJdbcException("Failed to cancel job", e); } } } @@ -604,12 +606,12 @@ void runQuery(String query, QueryJobConfiguration jobConfiguration) SqlType queryType = getQueryType(jobConfiguration, statementType); handleQueryResult(query, executeResult.tableResult, queryType); } catch (InterruptedException ex) { - throw new BigQueryJdbcRuntimeException(ex); + throw new BigQueryJdbcRuntimeException("Interrupted during runQuery", ex); } catch (BigQueryException ex) { if (ex.getMessage().contains("Syntax error")) { - throw new BigQueryJdbcSqlSyntaxErrorException(ex); + throw new BigQueryJdbcSqlSyntaxErrorException("BigQueryException during runQuery", ex); } - throw new BigQueryJdbcException(ex); + throw new BigQueryJdbcException("BigQueryException during runQuery", ex); } } @@ -1395,7 +1397,10 @@ public void addBatch(String sql) throws SQLException { } SqlType sqlType = getQueryType(QueryJobConfiguration.newBuilder(sql).build(), null); if (!SqlType.DML.equals(sqlType)) { - throw new IllegalArgumentException("addBatch currently supports DML operations."); + IllegalArgumentException ex = + new IllegalArgumentException("addBatch currently supports DML operations."); + LOG.severe(ex.getMessage(), ex); + throw ex; } this.batchQueries.add(sql); } diff --git a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryTypeCoercionUtility.java b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryTypeCoercionUtility.java index 9a4dc21304e4..ec1ec140975a 100644 --- a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryTypeCoercionUtility.java +++ b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryTypeCoercionUtility.java @@ -39,6 +39,8 @@ @InternalApi class BigQueryTypeCoercionUtility { + private static final BigQueryJdbcCustomLogger LOG = + new BigQueryJdbcCustomLogger(BigQueryTypeCoercionUtility.class.getName()); static BigQueryTypeCoercer INSTANCE; @@ -256,8 +258,11 @@ public Time coerce(FieldValue fieldValue) { long millis = TimeUnit.NANOSECONDS.toMillis(localTime.toNanoOfDay()); return new Time(millis); } catch (java.time.format.DateTimeParseException e) { - throw new IllegalArgumentException( - "Cannot parse the value " + strTime + " to java.sql.Time", e); + IllegalArgumentException ex = + new IllegalArgumentException( + "Cannot parse the value " + strTime + " to java.sql.Time", e); + LOG.severe(ex.getMessage(), ex); + throw ex; } } } diff --git a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/DataSource.java b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/DataSource.java index 471c38767501..02142363866e 100644 --- a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/DataSource.java +++ b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/DataSource.java @@ -368,18 +368,12 @@ public DataSource() {} @Override public Connection getConnection() throws SQLException { if (getURL() == null) { - BigQueryJdbcException ex = - new BigQueryJdbcException( - "Connection URL is null. Please specify a valid Connection URL to get Connection."); - LOG.severe(ex, ex.getMessage()); - throw ex; + throw new BigQueryJdbcException( + "Connection URL is null. Please specify a valid Connection URL to get Connection."); } if (!BigQueryDriver.getRegisteredDriver().acceptsURL(getURL())) { - BigQueryJdbcException ex = - new BigQueryJdbcException( - "The URL " + getURL() + " is invalid. Please specify a valid Connection URL. "); - LOG.severe(ex, ex.getMessage()); - throw ex; + throw new BigQueryJdbcException( + "The URL " + getURL() + " is invalid. Please specify a valid Connection URL. "); } return DriverManager.getConnection(getURL(), createProperties()); } @@ -973,11 +967,14 @@ public Boolean getUseStatelessQueryMode() { public void setJobCreationMode(Integer jobCreationMode) { if (jobCreationMode != null && !VALID_JOB_CREATION_MODES.contains(jobCreationMode)) { - throw new IllegalArgumentException( - String.format( - "Invalid value for %s. Use 1 for JOB_CREATION_REQUIRED and 2 for" - + " JOB_CREATION_OPTIONAL.", - BigQueryJdbcUrlUtility.JOB_CREATION_MODE_PROPERTY_NAME)); + IllegalArgumentException ex = + new IllegalArgumentException( + String.format( + "Invalid value for %s. Use 1 for JOB_CREATION_REQUIRED and 2 for" + + " JOB_CREATION_OPTIONAL.", + BigQueryJdbcUrlUtility.JOB_CREATION_MODE_PROPERTY_NAME)); + LOG.severe(ex.getMessage(), ex); + throw ex; } this.jobCreationMode = jobCreationMode; } diff --git a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/PooledConnectionDataSource.java b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/PooledConnectionDataSource.java index 66a957a06f84..8a86c8805cdb 100644 --- a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/PooledConnectionDataSource.java +++ b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/PooledConnectionDataSource.java @@ -24,6 +24,8 @@ import javax.sql.PooledConnection; public class PooledConnectionDataSource extends DataSource implements ConnectionPoolDataSource { + private static final BigQueryJdbcCustomLogger LOG = + new BigQueryJdbcCustomLogger(PooledConnectionDataSource.class.getName()); private PooledConnectionListener connectionPoolManager = null; Connection bqConnection = null; @@ -62,6 +64,9 @@ public PooledConnectionListener getConnectionPoolManager() { @Override public PooledConnection getPooledConnection(String arg0, String arg1) throws SQLException { - throw new UnsupportedOperationException("This operation is not supported by the driver"); + UnsupportedOperationException ex = + new UnsupportedOperationException("This operation is not supported by the driver"); + LOG.severe(ex.getMessage(), ex); + throw ex; } } diff --git a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/PooledConnectionListener.java b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/PooledConnectionListener.java index 9f3b210443e0..1534a6a0b798 100644 --- a/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/PooledConnectionListener.java +++ b/java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/PooledConnectionListener.java @@ -92,9 +92,12 @@ public void connectionClosed(ConnectionEvent event) { if (eventSource == null || !(eventSource instanceof BigQueryPooledConnection) || !(eventSource.getClass().isAssignableFrom(BigQueryPooledConnection.class))) { - throw new IllegalArgumentException( - "Invalid ConnectionEvent source passed to connectionClosed. Expecting" - + " BigQueryPooledConnection."); + IllegalArgumentException ex = + new IllegalArgumentException( + "Invalid ConnectionEvent source passed to connectionClosed. Expecting" + + " BigQueryPooledConnection."); + LOG.severe(ex.getMessage(), ex); + throw ex; } BigQueryPooledConnection bqPooledConnection = (BigQueryPooledConnection) eventSource; addConnection(bqPooledConnection); @@ -108,9 +111,12 @@ public void connectionErrorOccurred(ConnectionEvent event) { if (eventSource == null || !(eventSource instanceof BigQueryPooledConnection) || !(eventSource.getClass().isAssignableFrom(BigQueryPooledConnection.class))) { - throw new IllegalArgumentException( - "Invalid ConnectionEvent source passed to connectionClosed. Expecting" - + " BigQueryPooledConnection."); + IllegalArgumentException ex = + new IllegalArgumentException( + "Invalid ConnectionEvent source passed to connectionClosed. Expecting" + + " BigQueryPooledConnection."); + LOG.severe(ex.getMessage(), ex); + throw ex; } BigQueryPooledConnection bqPooledConnection = (BigQueryPooledConnection) eventSource; removeConnection(bqPooledConnection); diff --git a/java-bigquery/google-cloud-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcCustomLoggerTest.java b/java-bigquery/google-cloud-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcCustomLoggerTest.java index ee38c3bfdeec..ce192baece10 100644 --- a/java-bigquery/google-cloud-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcCustomLoggerTest.java +++ b/java-bigquery/google-cloud-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcCustomLoggerTest.java @@ -82,7 +82,7 @@ public void testLogWithCallerInference() { @Test public void testLogWithException() { Exception ex = new Exception("Test exception"); - logger.severe(ex, "Error occurred: %s", "detail"); + logger.severe("Error occurred: %s", ex, "detail"); List records = testHandler.getRecords(); assertEquals(1, records.size()); diff --git a/java-bigquery/google-cloud-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcOAuthUtilityTest.java b/java-bigquery/google-cloud-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcOAuthUtilityTest.java index 3630f86133dc..9176bb83d7be 100644 --- a/java-bigquery/google-cloud-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcOAuthUtilityTest.java +++ b/java-bigquery/google-cloud-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcOAuthUtilityTest.java @@ -111,7 +111,7 @@ public void testInvalidTokenUriForAuthType0() { BigQueryJdbcOAuthUtility.getCredentials(oauthProperties, overrideProperties, false, null); Assertions.fail(); } catch (BigQueryJdbcRuntimeException e) { - assertThat(e.getMessage()).contains("java.net.URISyntaxException"); + assertThat(e.getMessage()).contains("Validation failure"); } } diff --git a/java-bigquery/google-cloud-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/it/ITBigQueryJDBCTest.java b/java-bigquery/google-cloud-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/it/ITBigQueryJDBCTest.java index 316444aebb75..bc6f74f304f7 100644 --- a/java-bigquery/google-cloud-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/it/ITBigQueryJDBCTest.java +++ b/java-bigquery/google-cloud-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/it/ITBigQueryJDBCTest.java @@ -309,7 +309,7 @@ public void testInvalidQuery() throws SQLException { bigQueryStatement.executeQuery(query); Assertions.fail(); } catch (BigQueryJdbcException e) { - assertTrue(e.getMessage().contains("SELECT * must have a FROM clause")); + assertTrue(e.getCause().getMessage().contains("SELECT * must have a FROM clause")); } }