Java Spring Multiple Choice Questions & Answers (MCQs) on “Setting the Transaction Attribute”.
1. A transaction propagation behavior can be specified by the:-
a) propagation
b) consistency
c) isolation
d) All of the mentioned
Answer: a
Clarification: A transaction propagation behavior can be specified by the propagation transaction attribute.
2. Transaction propagation behavior are defined in the:-
a) org.springframework.transaction.Transaction
b) org.springframework.transaction.TransactionDefinition
c) all of the mentioned
d) none of the mentioned
Answer: b
Clarification: These behaviors are defined in the org.springframework.transaction.TransactionDefinition interface.
3. If there’s an existing transaction in progress, the current method should run within this transaction.
a) Required
b) REQUIRES NEW
c) SUPPORTS
d) NOT SUPPORTED
Answer: a
Clarification: If there’s an existing transaction in progress, the current method should run within this transaction. Otherwise, it should start a new transaction and run within its own transaction.
4. The current method must start a new transaction and run within its own transaction.
a) Required
b) REQUIRES NEW
c) SUPPORTS
d) NOT SUPPORTED
Answer: b
Clarification: The current method must start a new transaction and run within its own transaction. If there’s an existing transaction in progress, it should be suspended.
5. If there’s an existing transaction in progress, the current method can run within this transaction.
a) Required
b) REQUIRES NEW
c) SUPPORTS
d) NOT SUPPORTED
Answer: c
Clarification: Otherwise, it is not necessary to run within a transaction.
6. The current method should not run within a transaction.
a) Required
b) REQUIRES NEW
c) SUPPORTS
d) NOT SUPPORTED
Answer: d
Clarification: The current method should not run within a transaction. If there’s an existing transaction in progress, it should be suspended.
7. The current method must run within a transaction.
a) Required
b) MANDATORY
c) SUPPORTS
d) NOT SUPPORTED
Answer: b
Clarification: The current method must run within a transaction. If there’s no existing transaction in progress, an exception will be thrown.
8. The current method should not run within a transaction. If there’s an existing transaction in progress, an exception will be thrown.
a) Required
b) MANDATORY
c) SUPPORTS
d) NEVER
Answer: d
Clarification: The current method should not run within a transaction. If there’s an existing transaction in progress, an exception will be thrown.
9. If there’s an existing transaction in progress, the current method should run within the nested transaction.
a) Required
b) MANDATORY
c) NESTED
d) NEVER
Answer: c
Clarification: If there’s an existing transaction in progress, the current method should run within the nested transaction (supported by the JDBC 3.0 save point feature) of this transaction. Otherwise, it should start a new transaction and run within its own transaction.
10. For two transactions T1 and T2, T1 reads a field that has been updated by T2 but not yet committed.
a) Dirty Read
b) Nonrepeatable read
c) Phantom read
d) Lost Updates
Answer: a
Clarification: Later, if T2 rolls back, the field read by T1 will be temporary and invalid.
11. For two transactions T1 and T2, T1 reads a field and then T2 updates the field.
a) Dirty Read
b) Nonrepeatable read
c) Phantom read
d) Lost Updates
Answer: b
Clarification: Later, if T1 reads the same field again, the value will be different.
12. For two transactions T1 and T2, T1 reads some rows from a table and then T2 inserts new rows into the table.
a) Dirty Read
b) Nonrepeatable read
c) Phantom read
d) Lost Updates
Answer: c
Clarification: Later, if T1 reads the same table again, there will be additional rows.
13. For two transactions T1 and T2, they both select a row for update, and based on the state of that row, make an update to it.
a) Dirty Read
b) Nonrepeatable read
c) Phantom read
d) Lost Updates
Answer: d
Clarification: Thus, one overwrites the other when the second transaction to commit should have waited until the first one committed before performing its selection.
14. Isolation Levels Supported by Spring:-
a) DEFAULT
b) READ COMMITTED
c) READ UNCOMMITTED
d) All of the mentioned
Answer: d
Clarification: Isolation Description
DEFAULT Uses the default isolation level of the underlying database. For most databases,
the default isolation level is READ COMMITTED.
READ UNCOMMITTED Allows a transaction to read uncommitted changes by other transactions. The
dirty read, nonrepeatable read, and phantom read problems may occur.
READ COMMITTED Allows a transaction to read only those changes that have been committed by
other transactions. The dirty read problem can be avoided, but the nonrepeatable
read and phantom read problems may still occur.
REPEATABLE READ Ensures that a transaction can read identical values from a field multiple times.
For the duration of this transaction, updates made by other transactions to this
field are prohibited. The dirty read and nonrepeatable read problems can be
avoided, but the phantom read problem may still occur.
SERIALIZABLE Ensures that a transaction can read identical rows from a table multiple times. For
the duration of this transaction, inserts, updates, and deletes made by other
transactions to this table are prohibited. All the concurrency problems can be
avoided, but the performance will be low.
15. The exceptions that cause a transaction to roll back or not can be specified by attribute:-
a) rollback
b) commit
c) serialize
d) none of the mentioned
Answer: a
Clarification: Any exceptions not explicitly specified in this attribute will be handled by the default rollback rule (i.e., rolling back for unchecked exceptions and not rolling back for checked exceptions).