We explore advanced EJB concepts, including Singleton startup locks, asynchronous MDB message processing, and transaction rollback rules.
8.1. Singleton Concurrency Locks & MDB JMS queues
Definition: Singleton beans maintain a global shared state, while Message-Driven Beans (MDBs) process JMS queue messages asynchronously.
Core Working Principles:
- @Lock: Singleton concurrency locks (WRITE exclusive vs READ shared) manage thread safety.
- MDB: An asynchronous listener bean that implements the MessageListener interface to process queue messages.
@MessageDriven(...)
public class LoggerBean implements MessageListener {
public void onMessage(Message msg) {
// Async background logging
}
}
5-Marks Exam Scoring Points
- Singleton EJB represents exactly one instance shared across the entire application.
- LockType.WRITE blocks concurrent threads; LockType.READ allows concurrent reads.
- MDB runs on a background thread, preventing page load delays for clients.
- Decouples client request loops from slow background execution processes.
8.2. Container-Managed Transactions (CMT) Rollback Rules
Definition: CMT allows the EJB container to automatically start, commit, or rollback database transactions based on exceptions.
Core Working Principles:
- REQUIRED: The default transaction attribute; starts or joins a transaction.
- System Exceptions: Runtime exceptions (e.g. NullPointerException) trigger an automatic database rollback.
- Application Exceptions: Checked exceptions (e.g. IOException) do not trigger rollbacks unless configured.
5-Marks Exam Scoring Points
- Container handles transaction boundaries automatically using annotations.
- Runtime exceptions trigger an automatic database rollback.
- Checked exceptions do not trigger rollbacks automatically.