Skip to content
AITroveRead. Build. Understand.
Make this comfortable

Java DatabaseMetaData: check features before depending on them

Last updated: 5 Oct 20265 min read
tutorial
AdvancedBy AITrove Editorial

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.

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

Java
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
diagnostics and metadata
jdbc-driver-capability-check
Storage details