Database control
Use the DB_CONTROL class to check or change database status and behavior. The main operations you are likely to use are:
- Handling database errors.
- Connecting to the database.
- Committing changes in the database.
Handling database errors
Every EiffelStore interface class has an is_ok feature. This enables to check directly if the last database operation has been successful.
When an error is detected, you can use DB_CONTROL to obtain further information about the error:
- error_message provides a description of the error that occurred.
- error_code returns a code corresponding to the error type. This code enables to handle specific errors within your code without parsing the error_message.
- warning_message provides a warning message about the last transaction performed.
Once you have handled your error, for instance by displaying the error_message on screen, you can call reset to perform new database transactions.
Managing database connection
DB_CONTROL lets you connect, check your connection, and disconnect from the database.
The following example sum up these capabilities: session_control: DB_CONTROL
...
session_control.connect
if session_control.is_connected then
-- Perform your transactions here.
session_control.disconnect
end
Committing changes in the database
With many database systems, modifications you make to the database are usually not saved immediately. Rather, changes are accumulated in a "transaction". At some point the entire transaction is committed to the database (i.e., the changes are actually made to the data) or the entire transaction is rolled back (i.e., absolutely no changes are made to the data.) You can manage database modification through 2 commands:
- Commit saves the changes in the database.
- Rollback restores the database content to its state after the most recent commit.
The following example illustrates the use of these commands: session_control: DB_CONTROL
...
from
until
transaction_completed or else not session_control.is_ok
loop
-- Perform your transaction here.
end
if session_control.is_ok then
session_control.commit
else
session_control.rollback
end
The loop performs a multi-step transaction. If transaction is not carried out entirely, the database could stay in an invalid state: this code ensures that database remains in a valid state.
Caution: Some databases can be in an auto-commit mode. Furthermore, some special database commands can automatically commit database changes.