DatabaseMetaData reports capabilities of a particular JDBC connection. A feature needed by an algorithm should be checked against the deployed driver and database, then verified in integration tests.
Java DatabaseMetaData: check features before depending on them
Operational contract
The helper checks savepoint support and whether cursors can be closed at commit. It raises SQLFeatureNotSupportedException when either required feature is unavailable. The result describes this connection's reported capabilities; it is not proof that every edge case works on every server version. Run capability checks when a pool or data source is configured rather than on every request. Changes of driver, connection settings, or database version can alter behavior, so keep a real integration test for each required feature.
Failure case
A shipment workflow requires partial rollback. It fails at startup if the target driver reports no savepoints, rather than reaching the first production conflict and discovering the feature is absent. A separate test still verifies rollback semantics against that database.
Java code
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
public class JdbcFeatureGate {
public static void requireWorkflowFeatures(Connection connection) throws SQLException {
DatabaseMetaData metadata = connection.getMetaData();
if (!metadata.supportsSavepoints())
throw new SQLFeatureNotSupportedException("Savepoints required");
if (!metadata.supportsResultSetHoldability(ResultSet.CLOSE_CURSORS_AT_COMMIT))
throw new SQLFeatureNotSupportedException("Close-at-commit cursors required");
}
}Performance and ownership cost
Metadata calls can perform driver or database work and should usually be paid once per configured data source, not once per row. The method retains O(1) Java state. Its result is a compatibility gate, not a substitute for database-backed tests.
Common Mistakes
- Do not assume every JDBC driver implements optional features.
- Do not use a metadata flag as proof of correct deployment semantics without tests.
- Do not query metadata in a tight per-row loop.
Connected lessons
- Java JDBC savepoints: roll back one optional step inside a transaction
- Java JDBC cursor holdability: choose what commit does to a ResultSet
- Java connection pools: borrowed handles, rollback and admission timeout
- Java SQLWarning: inspect nonfatal database diagnostics before reuse
- Java SQLException chains: preserve SQLState and linked failures
- Java JDBC boundary contracts quiz
- Advanced Java
