Seminar 09 - Transactions and Error Handling
In this seminar you will fix and remedy some broken or omitted things
Task 0: Update your repository
For this seminar, you will work with the same forked repository as previously. We assume you already have the upstream
remote set from Seminar 03 (if not, please follow task 0.1 from that seminar).
0.1: Update your main
branch
First, you need to fetch new changes from the upstream
repository and update your local main
branch:
git switch main
git pull upstream main
Then you need to update the main branch in your forked repository:
git push origin main
0.2: Create a new branch for this seminar
Again, each pair creates a new branch on a single computer as they are working together as one person during the seminar.
Pair 1 creates a new branch seminar09-pair1
:
git switch -c seminar09-pair1
Pair 2 creates a new branch seminar09-pair2
:
git switch -c seminar09-pair2
0.3: Go through code changes
The structure and composition of the code base changed slightly since the last seminar. Familiarise yourself with these changes. They mainly consists of database layer (transactional management), logging, and exception reporting.
Task 1: You may have to go nuclear
Start the application, if you get an error – which you likely are if you’ve previously started the app on this system. If that happen, do not forget to insert dev
data into database again, similarly as in the previous seminar Task 3.
Task 2: Transactional Import
The application currently does not use transactions. This is a problem, as the application can be left in an inconsistent state if an error occurs during the import process. To fix this, you need to add transaction management to the import process.
New class appeared – TransactionExecutor
. Discover what methods this class has. Use this class and its TransactionExecutor#executeInTransaction
in a proper place. Think carefully about the right place.
Task 3: Add validator
The customer requests to validate employees by their age. From now on, no employee below age 18 cannot be inserted in the application.
Task 3.1: Add test of that validator
To be sure that the validator works, add a test for it. Before you start, think about how it should be tested.
Time tests are quite tricky, so you might want to use some mocking. To do so, your validator must be able to accept a Clock
instance.
Task 4: Error handling
Another area which is not covered by the current implementation of Employee Records is error handling. Every application needs to deal with the following types of errors
- Expected runtime errors
- Unexpected recoverable errors
- Unexpected non-recoverable errors
Discuss examples of each type
Task 4.1: Display validation errors
Add employee validation to add/edit action dialog, displaying possible validation errors. User should be able to address these errors without losing current input data.
Task 4.2: Handle all errors
The application currently contains an implementation of Thread.UncaughtExceptionHandler
which globally handles any uncaught exception. However there are few remaining issues regarding exception handling
- Currently
ApplicationErrorHandler
handles all exceptions as fatal - Currently
ApplicationErrorHandler
displays only a generic error message - In some cases
FatalStorageException
should be used instead of regularStorageException
Improve the implementation of ApplicationErrorHandler
to address the above-mentioned issues.
Hint: Arbitrary exceptions should be always considered unrecoverable (fatal). Instance of ApplicationException
is unrecoverable if and only if it also implements FatalError
interface.