This topic describes the error handling for MySQL-compatible applications.
OceanBase Database is fully compatible with MySQL Database starting from V1.0. We try to ensure that the error codes, SQLSTATE, and error messages are consistent with those of MySQL Database. Therefore, we recommend that you handle application errors in the same way as MySQL Database. However, OceanBase Database is a distributed database, which is fundamentally different from a standalone MySQL Database. Some errors that occur in a distributed environment cannot be expressed by the error codes of MySQL Database. Generally, an error code greater than 4000 indicates an error code unique to OceanBase Database. An error code less than or equal to 4000 indicates an error code compatible with MySQL Database. For more information about the error codes of OceanBase Database in MySQL tenant mode, see Overview of error messages.
In addition to the errors returned by the database server, an application may also receive errors returned by the driver library. For example, an error may be returned when a database connection fails. OceanBase Database is fully compatible with MySQL Database in terms of error codes. Therefore, the error codes of OceanBase Database are the same as those of MySQL Database. The values of these error codes range from 2000 to 3000.
Obtain error information
Different drivers provide different APIs to obtain error information. For more information about the drivers, see OceanBase Database documentation and MySQL Database documentation.
Java driver
If a Java application using JDBC fails to execute an SQL statement, it can capture the SQLException exception, which contains the error code, SQLSTATE, and error message. For more information about the SQLException exception, see JDBC API documentation.
You can capture the SQLException exception by using the following methods:
- Call the getErrorCode() method to obtain the error code.
- Call the getSQLState() method to obtain the SQLSTATE.
- Call the getMessage() method to obtain the error message.
libmysql C driver
If a C application using the libmysql driver fails to execute an SQL statement, it can obtain the error information by using the following methods:
- Call the mysql_errno() method to obtain the error code.
- Call the mysql_sqlstate() method to obtain the SQLSTATE.
- Call the mysql_error() method to obtain the error message.
Error handling
For most error codes of OceanBase Database in MySQL tenant mode, you do not need to handle the errors. You can directly return the errors to the client. For some error codes, you can use the following four strategies based on the scenarios and error types:
Strategy 1: Handle errors based on the statement type.
If a timeout error occurs when you execute a
SELECTstatement, you can retry the statement because theSELECTstatement has no side effects.If a timeout error occurs when you execute an
UPDATEstatement in a remote or distributed environment, you cannot determine whether the statement is executed successfully. Therefore, you need to roll back the transaction. If theUPDATEstatement is idempotent, you can retry the statement.If a timeout error occurs when you execute a
COMMITorROLLBACKstatement, you need to directly return the error to the client because the transaction state is unknown.
Strategy 2: Roll back the current transaction and retry the transaction.
Strategy 3: Retry the current statement.
Strategy 4: Close the current connection and then rebuild the connection.
Error codes unique to OceanBase Database
The following table lists the error codes unique to OceanBase Database. You can handle these error codes based on the strategies in the table.
| Error code | SQLSTATE | Description | Statement execution result | Transaction state | Strategy |
|---|---|---|---|---|---|
| 4012 | HY000 | The statement execution times out. | Unknown | Unknown | Strategy 1 |
| 4038 | HY000 | The current replica is not the leader. This error may occur when the leader is switched. | Failed | Unchanged | Strategy 3 |
| 4138 | HY000 | The snapshot version is too old. | Failed | Unchanged | Strategy 3 |
| 4225 | HY000 | The partition does not exist. This error may occur when the partition is migrated. | Failed | Unchanged | Strategy 3 |
| 6002 | 40000 | The transaction is rolled back by the system. | Failed | Rolled back | Strategy 2 |
| 6231 | HY000 | The replica data is unreadable. This error may occur when the replica is migrated. | Failed | Unchanged | Strategy 3 |
| 6235 | 25000 | The current transaction cannot be serialized. This error may occur when there are transaction conflicts in the Repeatable Read (RR) or Serializable isolation level. For more information about transaction isolation levels, see Overview of transaction isolation levels. |
Failed | Unchanged | Strategy 2 |
| 8001~8004 | 08004 | The connection cannot be recovered | Failed | Unknown | Strategy 4 |
Error codes compatible with MySQL Database
For error codes less than 4000, handle them in the same way as MySQL Database. Pay special attention to the following error codes.
| Error code | SQLSTATE | Description | Strategy |
|---|---|---|---|
| 1205 | HY000 | Deadlock detected | Roll back the current transaction and retry |
| 1213 | 40001 | Lock wait timeout | Roll back the current transaction and retry |
