Spring Questions and Answers – Transactions and Retrying

This set of Java Spring Questions & Answers for entrance exams focuses on “Transactions and Retrying”.

1. Transaction capabilities are built on top of the first class support already provided by the core Spring framework.
a) True
b) False
View Answer

Answer: a
Explanation: Where relevant, Spring Batch surfaces the configuration so that you can control it.

2. Spring core framework provides first-class support for transactions.
a) Transaction
b) TransactionManager
c) All of the mentioned
d) None of the mentioned
View Answer

Answer: b
Explanation: You simply wire up a TransactionManager and give Spring Batch a reference, just as you would in any regular JdbcTemplate or HibernateTemplate solution.

3. The batch.xml file establishes a:-
a) BasicDataSource
b) DataSourceTransactionManager
c) All of the mentioned
d) None of the mentioned
View Answer

Answer: c
Explanation: The TransactionManager and BasicDataSource were then wired to the JobRepository, which was in turn wired to the JobLauncher, which you used to launch all jobs thus far.
advertisement
advertisement

4. Spring Batch will, by default, try to pluck the:-
a) PlatformTransactionManager
b) transactionManager
c) All of the mentioned
d) None of the mentioned
View Answer

Answer: c
Explanation: The transaction manager reference can be specified, but in your solutions, it wasn’t required because Spring Batch will, by default, try to pluck the PlatformTransactionManager named transactionManager from the context and use it.

5. Spring Batch excels in the robustness it surfaces as simple configuration options for the edge and failure cases.
a) True
b) False
View Answer

Answer: a
Explanation: If a write fails on an ItemWriter, or some other exception occurs in processing, Spring Batch will rollback the transaction.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

6. Element to configure this for the step:-
a) no-rollback-exception-classes
b) no-rollback-exception
c) no-rollback
d) none of the mentioned
View Answer

Answer: a
Explanation: You can use the no-rollback-exception-classes element to configure this for the step. The value is a list of Exception classes that should not cause the transaction to roll back.

7. no-rollback-exception-classes element Exception classes that should not cause the transaction to roll back.

advertisement
<step id = "step2">
   <tasklet>
           <chunk reader="reader" writer="writer" commit-interval="10" />
           <no-rollback-exception-classes>
           <include class="com.yourdomain.exceptions.YourBusinessException"/>
           </no-rollback-exception-classes>
   </tasklet>
</step>

a) True
b) False
View Answer

Answer: a
Explanation: You can use the no-rollback-exception-classes element to configure this for the step. The value is a list of Exception classes that should not cause the transaction to roll back.
advertisement

8. You want to work with a resource that may fail when you try to read from or write to it.
a) TransactionManager
b) Transaction
c) Spring batch retry
d) All of the mentioned
View Answer

Answer: c
Explanation: Use Spring Batch retry capabilities to systematically retry the read or write.

9. Some invocations will fail but may be retried with some likelihood of success in a transactional scenario.
a) True
b) False
View Answer

Answer: b
Explanation: For example, an update to the database resulting in org.springframework.dao.DeadlockLoserDataAccessException might be usefully retried.

10. You can specify exception classes on which to retry the operation.

<step id = "step23">
       <tasklet transaction-manager="transactionManager">
            <chunk reader="csvFileReader" writer="jdbcItemWriter" commit-interval="10" retry-limit="3" cache-capacity="10">
           <retryable-exception-classes>
           <include class="org.springframework.dao.DeadlockLoserDataAccessException"/>
           </retryable-exception-classes> 
           </chunk>
       </tasklet>
</step>

a) True
b) False
View Answer

Answer: a
Explanation: As with the rollback exceptions, you can delimit this list of exceptions with newlines or commas.

11. You can leverage Spring Batch support for retries and recovery in your own code.
a) True
b) False
View Answer

Answer: a
Explanation: For example, you can have a custom ItemWriter in which retry functionality is desired or even an entire service interface for which retry support is desired.

12. The template that (much like its various other Template cousins) isolates your logic from the nuances of retries and instead enables you to write the code as though you were only going to attempt it once.
a) Retry
b) RetryTemplate
c) All of the mentioned
d) None of the mentioned
View Answer

Answer: b
Explanation: Spring Batch supports these scenarios through the RetryTemplate that (much like its various other Template cousins) isolates your logic from the nuances of retries and instead enables you to write the code as though you were only going to attempt it once.

13. The RetryTemplate supports many use cases, with convenient APIs to wrap.
a) True
b) false
View Answer

Answer: a
Explanation: Otherwise tedious retry/fail/recover cycles in concise, single-method invocations.

14. The RetryTemplate itself is configured in the Spring context, although it’s trivial to create in code.
a) True
b) False
View Answer

Answer: a
Explanation: I declare it in the Spring context only because there is some surface area for configuration when creating the object, and I try to let Spring handle the configuration.

15. One of the more useful settings for the RetryTemplate is the :-
a) BackOff
b) Back
c) BackOffPolicy
d) All of the mentioned
View Answer

Answer: c
Explanation: BackOffPolicy dictates how long the RetryTemplate should back off between retries.

Sanfoundry Global Education & Learning Series – Java Spring.
To practice all areas of Java Spring for entrance exams, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.