| Online Eiffel Documentation

EiffelStudio

# 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 access through DB_CONTROL 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 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

Every modification you do in the database are usually not directly saved. This enables to limit damage caused by mishandlings. You can then manage database modification through 2 commands:

- Commit saves the changes in the database.
- Rollback restores the database content corresponding to last commitment.

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.

See Also
Database connection
Implementation