Skip to content

Commit

Permalink
Replace FBSQLException with general JDBC SQLException implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
mrotteveel committed Oct 9, 2023
1 parent 8277ae8 commit 7e07ed0
Show file tree
Hide file tree
Showing 24 changed files with 199 additions and 208 deletions.
6 changes: 5 additions & 1 deletion src/docs/asciidoc/release_notes.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,8 @@ The JDBC API specifies _"This `byte` array contains up to `length` consecutive b
+
The JDBC API does not specify what should happen if the requested position is beyond the end-of-blob.
The modified implementation returns an empty array, but given this is unspecified behaviour, we reserve the option to change this in the future to throw an exception instead.
* `FBResultSetNotUpdatableException` now extends `SQLNonTransientException` instead of `FBSQLException`.
* Jaybird no longer throws any instances of `FBSQLException`.
[#removal-of-deprecated-classes-and-packages]
=== Removal of deprecated classes and packages
Expand Down Expand Up @@ -1093,9 +1095,10 @@ The following methods had their visibility reduced:

The following classes have been removed in Jaybird 6 without deprecation:

* `FbLocalDatabaseFactory` -- unused since removal of LOCAL protocol implementation in Jaybird 5
* `FbLocalDatabaseFactory` -- unused since removal of LOCAL protocol implementation in Jaybird 5.
* `DatatypeCoder.RawDateTimeStruct` (semi-internal API) -- use one of the `java.time` types (`LocalDateTime`, `LocalDate` or `LocalTime`).
Though this class was publicly accessible through `ResultSet.getObject/updateObject` and `PreparedStatement.setObject`, it is internal API, and we expect it is unlikely to be actually used in user code.
* `FBDriverConsistencyCheckException` -- unused since the changes to client info properties.

The following classes are no longer accessible in Jaybird 6:

Expand Down Expand Up @@ -1424,6 +1427,7 @@ The following classes have been deprecated and will be removed in Jaybird 7:
Previous versions of `GDSFactoryPlugin` declared `throws GDSException` for some methods, but now `throws SQLException`.
To retain some semblance of backwards-compatibility, this class was retrofitted to extend `SQLException`.
It may get removed in Jaybird 7 or later.
* `FBSQLException` -- use `SQLException`

[#removal-of-deprecated-constants-7]
==== Removal of deprecated constants
Expand Down
6 changes: 3 additions & 3 deletions src/main/org/firebirdsql/jdbc/AbstractFieldMetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.Map;

import static org.firebirdsql.jdbc.JavaTypeNameConstants.*;
import static org.firebirdsql.jdbc.SQLStateConstants.SQL_STATE_INVALID_PARAM_TYPE;
import static org.firebirdsql.util.FirebirdSupportInfo.supportInfoFor;

/**
Expand Down Expand Up @@ -143,9 +144,8 @@ protected final String getFieldClassName(int field) throws SQLException {
case Types.BOOLEAN -> BOOLEAN_CLASS_NAME;
case Types.NULL, Types.OTHER -> OBJECT_CLASS_NAME;
case Types.ROWID -> ROW_ID_CLASS_NAME;
default -> throw new FBSQLException(
"Field %d has unknown JDBC SQL type: %s".formatted(field, getFieldType(field)),
SQLStateConstants.SQL_STATE_INVALID_PARAM_TYPE);
default -> throw new SQLException("Field %d has unknown JDBC SQL type: %s"
.formatted(field, getFieldType(field)), SQL_STATE_INVALID_PARAM_TYPE);
};
}

Expand Down
13 changes: 7 additions & 6 deletions src/main/org/firebirdsql/jdbc/FBCallableStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import java.sql.*;
import java.util.*;

import static org.firebirdsql.jdbc.SQLStateConstants.SQL_STATE_NO_RESULT_SET;

/**
* Implementation of {@link java.sql.CallableStatement}.
*
Expand Down Expand Up @@ -249,8 +251,9 @@ public ResultSet executeQuery() throws SQLException {
notifyStatementStarted();
prepareFixedStatement(procedureCall.getSQL(isSelectableProcedure()));

if (!internalExecute(!isSelectableProcedure()))
throw new FBSQLException("No resultset for sql", SQLStateConstants.SQL_STATE_NO_RESULT_SET);
if (!internalExecute(!isSelectableProcedure())) {
throw new SQLNonTransientException("No resultset for sql", SQL_STATE_NO_RESULT_SET);
}

getResultSet();
setRequiredTypes();
Expand Down Expand Up @@ -1091,8 +1094,7 @@ public ResultSet getGeneratedKeys() throws SQLException {
*/
protected void assertHasData(ResultSet rs) throws SQLException {
if (rs == null) {
throw new SQLException("Current statement has no data to return",
SQLStateConstants.SQL_STATE_NO_RESULT_SET);
throw new SQLException("Current statement has no data to return", SQL_STATE_NO_RESULT_SET);
}
// check if we have a row, and try to move to the first position.
if (rs.getRow() == 0) {
Expand All @@ -1103,8 +1105,7 @@ protected void assertHasData(ResultSet rs) throws SQLException {

// check if we still have no row and throw an exception in this case.
if (rs.getRow() == 0) {
throw new SQLException("Current statement has no data to return",
SQLStateConstants.SQL_STATE_NO_RESULT_SET);
throw new SQLException("Current statement has no data to return", SQL_STATE_NO_RESULT_SET);
}
}

Expand Down
48 changes: 26 additions & 22 deletions src/main/org/firebirdsql/jdbc/FBConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
import static java.lang.System.Logger.Level.WARNING;
import static java.util.stream.Collectors.toMap;
import static org.firebirdsql.gds.ISCConstants.fb_cancel_abort;
import static org.firebirdsql.jdbc.SQLStateConstants.SQL_STATE_CONNECTION_CLOSED;
import static org.firebirdsql.jdbc.SQLStateConstants.SQL_STATE_GENERAL_ERROR;
import static org.firebirdsql.jdbc.SQLStateConstants.SQL_STATE_INVALID_TX_STATE;
import static org.firebirdsql.jdbc.SQLStateConstants.SQL_STATE_TX_ACTIVE;

/**
* The class {@code FBConnection} is a handle to a {@link FBManagedConnection} and implements {@link Connection}.
Expand Down Expand Up @@ -126,7 +130,7 @@ public void setHoldability(int holdability) throws SQLException {
protected void checkValidity() throws SQLException {
if (isClosed()) {
throw new SQLNonTransientConnectionException("This connection is closed and cannot be used now",
SQLStateConstants.SQL_STATE_CONNECTION_CLOSED);
SQL_STATE_CONNECTION_CLOSED);
}
}

Expand Down Expand Up @@ -251,7 +255,8 @@ public void setTransactionParameters(int isolationLevel, TransactionParameterBuf
try (LockCloseable ignored = withLock()) {
checkValidity();
if (mc.isManagedEnvironment()) {
throw new FBSQLException("Cannot set transaction parameters in managed environment.");
throw new SQLException("Cannot set transaction parameters in managed environment.",
SQL_STATE_GENERAL_ERROR);
}

mc.setTransactionParameters(isolationLevel, tpb);
Expand All @@ -264,7 +269,8 @@ public void setTransactionParameters(TransactionParameterBuffer tpb) throws SQLE
checkValidity();
if (getLocalTransaction().inTransaction()) {
// TODO More specific exception, jaybird error code
throw new FBSQLException("Cannot set transaction parameters when transaction is already started.");
throw new SQLException("Cannot set transaction parameters when transaction is already started.",
SQL_STATE_TX_ACTIVE);
}

mc.setTransactionParameters(tpb);
Expand Down Expand Up @@ -359,7 +365,8 @@ public void setManagedEnvironment(boolean managedConnection) throws SQLException
public boolean getAutoCommit() throws SQLException {
try (LockCloseable ignored = withLock()) {
if (isClosed()) {
throw new FBSQLException("You cannot getAutoCommit on an unassociated closed connection.");
throw new SQLNonTransientConnectionException(
"You cannot getAutoCommit on an unassociated closed connection.", SQL_STATE_CONNECTION_CLOSED);
}
return txCoordinator.getAutoCommit();
}
Expand All @@ -369,13 +376,12 @@ public boolean getAutoCommit() throws SQLException {
public void commit() throws SQLException {
try (LockCloseable ignored = withLock()) {
if (isClosed()) {
throw new FBSQLException(
"You cannot commit a closed connection.",
SQLStateConstants.SQL_STATE_CONNECTION_CLOSED);
throw new SQLNonTransientConnectionException("You cannot commit a closed connection.",
SQL_STATE_CONNECTION_CLOSED);
}

if (mc.inDistributedTransaction()) {
throw new FBSQLException("Connection enlisted in distributed transaction", SQLStateConstants.SQL_STATE_INVALID_TX_STATE);
throw new SQLException("Connection enlisted in distributed transaction", SQL_STATE_INVALID_TX_STATE);
}

txCoordinator.commit();
Expand All @@ -387,13 +393,12 @@ public void commit() throws SQLException {
public void rollback() throws SQLException {
try (LockCloseable ignored = withLock()) {
if (isClosed()) {
throw new FBSQLException(
"You cannot rollback closed connection.",
SQLStateConstants.SQL_STATE_CONNECTION_CLOSED);
throw new SQLNonTransientConnectionException("You cannot rollback closed connection.",
SQL_STATE_CONNECTION_CLOSED);
}

if (mc.inDistributedTransaction()) {
throw new FBSQLException("Connection enlisted in distributed transaction", SQLStateConstants.SQL_STATE_INVALID_TX_STATE);
throw new SQLException("Connection enlisted in distributed transaction", SQL_STATE_INVALID_TX_STATE);
}

txCoordinator.rollback();
Expand Down Expand Up @@ -443,7 +448,7 @@ public void close() throws SQLException {
try {
setAutoCommit(true);
} catch (SQLException e) {
if (!SQLStateConstants.SQL_STATE_CONNECTION_CLOSED.equals(e.getSQLState())) {
if (!SQL_STATE_CONNECTION_CLOSED.equals(e.getSQLState())) {
chainBuilder.append(e);
}
}
Expand Down Expand Up @@ -550,8 +555,9 @@ public void setReadOnly(boolean readOnly) throws SQLException {
checkValidity();
if (getLocalTransaction().inTransaction() && !mc.isManagedEnvironment()) {
// TODO More specific exception, jaybird error code
throw new FBSQLException("Calling setReadOnly(boolean) method " +
"is not allowed when transaction is already started.");
throw new SQLException(
"Calling setReadOnly(boolean) method is not allowed when transaction is already started.",
SQL_STATE_TX_ACTIVE);
}
mc.setReadOnly(readOnly);
}
Expand Down Expand Up @@ -859,12 +865,11 @@ public Savepoint setSavepoint() throws SQLException {
private void setSavepoint(FBSavepoint savepoint) throws SQLException {
if (getAutoCommit()) {
throw new SQLException("Connection.setSavepoint() method cannot be used in auto-commit mode.",
SQLStateConstants.SQL_STATE_INVALID_TX_STATE);
SQL_STATE_INVALID_TX_STATE);
}

if (mc.inDistributedTransaction()) {
throw new SQLException("Connection enlisted in distributed transaction",
SQLStateConstants.SQL_STATE_INVALID_TX_STATE);
throw new SQLException("Connection enlisted in distributed transaction", SQL_STATE_INVALID_TX_STATE);
}

txCoordinator.ensureTransaction();
Expand Down Expand Up @@ -917,7 +922,7 @@ public void rollback(Savepoint savepoint) throws SQLException {
checkValidity();
if (getAutoCommit()) {
throw new SQLException("Connection.rollback(Savepoint) method cannot be used in auto-commit mode.",
SQLStateConstants.SQL_STATE_INVALID_TX_STATE);
SQL_STATE_INVALID_TX_STATE);
}

// TODO The error message and actual condition do not match
Expand All @@ -926,8 +931,7 @@ public void rollback(Savepoint savepoint) throws SQLException {
}

if (mc.inDistributedTransaction()) {
throw new SQLException("Connection enlisted in distributed transaction",
SQLStateConstants.SQL_STATE_INVALID_TX_STATE);
throw new SQLException("Connection enlisted in distributed transaction", SQL_STATE_INVALID_TX_STATE);
}

if (!fbSavepoint.isValid()) {
Expand All @@ -946,7 +950,7 @@ public void releaseSavepoint(Savepoint savepoint) throws SQLException {
checkValidity();
if (getAutoCommit()) {
throw new SQLException("Connection.releaseSavepoint() method cannot be used in auto-commit mode.",
SQLStateConstants.SQL_STATE_INVALID_TX_STATE);
SQL_STATE_INVALID_TX_STATE);
}

// TODO The error message and actual condition do not match
Expand Down

This file was deleted.

7 changes: 5 additions & 2 deletions src/main/org/firebirdsql/jdbc/FBDriverPropertyManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.sql.SQLException;
import java.util.*;

import static org.firebirdsql.jdbc.SQLStateConstants.SQL_STATE_GENERAL_ERROR;

/**
* Manager of the DPB properties.
*/
Expand Down Expand Up @@ -54,8 +56,9 @@ static Map<String, String> normalize(Map<String, String> props) throws SQLExcept
String primaryName = property != null ? property.name() : propName;
boolean hasDuplicate = property != null && result.containsKey(primaryName);
if (hasDuplicate) {
throw new FBSQLException("Specified properties contain multiple references to a property: "
+ "primary name " + primaryName + ", current name: " + propName);
throw new SQLException(
"Specified properties contain multiple references to a property: primary name %s, current name: %s"
.formatted(primaryName, propName), SQL_STATE_GENERAL_ERROR);
}
String propValue = entry.getValue();
result.put(primaryName, propValue);
Expand Down
Loading

0 comments on commit 7e07ed0

Please sign in to comment.