{"record":{"id":"820ed0ac7e3629fc","repo":"apache/shardingsphere","slug":"400","errorCode":"400","errorMessage":"Load datetime from database failed.","messagePattern":"Load datetime from database failed\\.","errorType":"exception","errorClass":"DatetimeLoadingException","httpStatus":null,"severity":"critical","filePath":"kernel/time-service/type/database/src/main/java/org/apache/shardingsphere/timeservice/type/database/DatabaseTimestampService.java","lineNumber":60,"sourceCode":"public final class DatabaseTimestampService implements TimestampService {\n    \n    private DataSource dataSource;\n    \n    private DatabaseType storageType;\n    \n    @Override\n    public void init(final Properties props) {\n        dataSource = DataSourcePoolCreator.create(new YamlDataSourceConfigurationSwapper().swapToDataSourcePoolProperties(\n                props.entrySet().stream().collect(Collectors.toMap(entry -> entry.getKey().toString(), Entry::getValue))));\n        storageType = DatabaseTypeEngine.getStorageType(dataSource);\n    }\n    \n    @Override\n    public Timestamp getTimestamp() {\n        try {\n            return loadDatetime(dataSource, DatabaseTypedSPILoader.getService(TimestampLoadingSQLProvider.class, storageType).getTimestampLoadingSQL());\n        } catch (final SQLException ex) {\n            throw new DatetimeLoadingException(ex);\n        }\n    }\n    \n    private Timestamp loadDatetime(final DataSource dataSource, final String datetimeLoadingSQL) throws SQLException {\n        try (\n                Connection connection = dataSource.getConnection();\n                PreparedStatement preparedStatement = connection.prepareStatement(datetimeLoadingSQL)) {\n            try (ResultSet resultSet = preparedStatement.executeQuery()) {\n                resultSet.next();\n                return (Timestamp) resultSet.getObject(1);\n            }\n        }\n    }\n    \n    @Override\n    public String getType() {\n        return \"Database\";\n    }","sourceCodeStart":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/apache/shardingsphere/blob/e952770a215630a3659c75d64369168cd3e26b82/kernel/time-service/type/database/src/main/java/org/apache/shardingsphere/timeservice/type/database/DatabaseTimestampService.java#L42-L78","documentation":"DatabaseTimestampService loads the system time by executing a dialect-specific SQL statement (from the TimestampLoadingSQLProvider SPI) against a configured backend datasource. If that query fails — connection failure, wrong storage type resolution, or a result row that cannot be cast to Timestamp — it wraps the SQLException in DatetimeLoadingException (error code 400).","triggerScenarios":"Configuring the time service with type=database (props: url/username/password or jdbc-url style keys), then starting the proxy/JDBC instance so getTimestamp() runs the loading SQL and hits an SQLException: bad URL, unreachable DB, missing SPI provider for the resolved storage type, or a query returning a non-Timestamp/empty result.","commonSituations":"Typos in the time-service datasource URL; the backend user lacking SELECT permission on the timestamp source (e.g. MySQL's NOW() query or pg_catalog); a database type with no TimestampLoadingSQLProvider registered; network/firewall blocks between proxy and the time database; the selected row's first column not being a timestamp.","solutions":["Verify the configured time-service datasource URL, credentials, and reachability with a plain JDBC client from the same host.","Check that the resolved storage type has a TimestampLoadingSQLProvider SPI on the classpath and that its SQL runs manually against the backend.","Grant the backend user permission to execute the timestamp-loading SQL and ensure it returns exactly one Timestamp value.","If the environment cannot host a time DB, switch the time service to another type (e.g. system clock) via configuration.","Inspect the cause chain of DatetimeLoadingException — it carries the original SQLException with the driver's real error."],"exampleFix":"// before (server.yaml fragment with broken url)\ntime-service:\n  type: Database\n  props:\n    url: jdbc:mysql://127.0.0.1:3307/timestamp_db   # wrong port\n    username: root\n    password: root\n\n// after\ntime-service:\n  type: Database\n  props:\n    url: jdbc:mysql://127.0.0.1:3306/timestamp_db   # verified reachable\n    username: root\n    password: root","handlingStrategy":"retry","validationCode":"// before starting: verify the time-service datasource is reachable\ntry (Connection c = DriverManager.getTimeServiceConnection == null ? null : DriverManager.getConnection(url, user, pwd);\n     Statement s = c.createStatement(); ResultSet r = s.executeQuery(\"SELECT CURRENT_TIMESTAMP\")) {\n    r.next();\n} // mirrored pre-flight check for the configured url/user/password","typeGuard":null,"tryCatchPattern":"try { Timestamp ts = timestampService.getTimestamp(); } catch (final DatetimeLoadingException ex) { SQLException cause = (SQLException) ex.getCause(); /* inspect cause: connectivity, permission, or SQL provider */ }","preventionTips":["Pre-flight the time-service JDBC URL and credentials from the same host before startup.","Confirm a TimestampLoadingSQLProvider SPI exists for the backend database type.","Grant the backend user rights on the timestamp-loading SQL and ensure it returns one Timestamp row.","Monitor proxy startup logs for DatetimeLoadingException and fix before traffic flows."],"tags":["configuration","time-service","database","spi","connection"],"backgroundTag":null,"analyzedSha":"e952770a215630a3659c75d64369168cd3e26b82","analyzedAt":"2026-08-14T13:54:53.392Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}