Transaction Management Questions:
1- what is repeatable read ?
https://javacmlearning.blogspot.com/2019/09/what-is-repeatable-read-in-spring.html
2-
Effective Spring Transaction Management
what are the impotent aspect of transaction management.
https://javacmlearning.blogspot.com/2019/09/effective-spring-transaction-management.html3-what are data base transaction and application transaction?
Database transaction:
a single logical unit of work which is accessing and modifying the content of db.it could be said like for performing any logical function running sequence of queries.
What are Application Transactions ?
An application transaction is sequence of application actions that are considered as a single logical unit.
4. What is Transaction Management ?
creating transaction objects and managing their durability and atomicity.
ACID property
5. How to implement Transactions Management in Spring Boot ?
In Spring Boot Transaction Management is implemented using Transactional annotation. Transaction is a cross cutting concern and it is implemented using AOP in Spring Boot.
Spring Boot implicitly creates a proxy for the transaction annotated methods. So for such methods the proxy acts like a wrapper which takes care of creating a transaction at the beginning of the method call and committing the transaction after the method is executed.
6. What are the different types of Transaction Propagations?
The different types of Transaction Propagation for Spring Boot are as follows-
- REQUIRED
- SUPPORTS
- NOT_SUPPORTED
- REQUIRES_NEW
- NEVER
- MANDATORY
7.
How does Spring @Transactional Really Work?
here mainly answer how to use features like isolation and propagation
JPA and Transaction Management :
JPA itself does not provide any kind of transaction management
when we are using jpa outside our dependency management container then transaction needed to be handled by developer programatically.
MyTransaction tx = entityManager.getTransaction();
try
{
tx.begin();
businessLogic();
tx.commit();
}
catch
(Exception ex) {
tx.rollback();
throw
ex;
}
This way of managing transactions makes the scope of the transaction very clear in the code, but it has several disadvantages:
- it’s repetitive and error prone
- any error can have a very high impact
- errors are hard to debug and reproduce
- this decreases the readability of the code base
- What if this method calls another transactional method?
Using Spring @Transactional
With Spring , the above code gets reduced to simply this:
@Transactional
public
void
businessLogic() {
... use entity manager inside a transaction ...
}
By using , many important aspects such as transaction propagation are handled automatically. In this case if another transactional method is called by , that method will have the option of joining the ongoing transaction.
what does @Transaction means
there are two separate concept to consider
- the persistence context
- the database transaction
The database transaction happens inside the scope of a persistence context.
persistence context is in JPA the , implemented internally using an Hibernate (when using Hibernate as the persistence provider).
The persistence context is just a synchronizer object that tracks the state of a limited set of Java objects and makes sure that changes on those objects are eventually persisted back into the database.
When does an EntityManager span multiple database transactions?
In such case the queries that run in the view layer are in separate database transactions than the one used for the business logic, but they are made via the same entity manager.
Another case is when the persistence context is marked by the developer as , which means that it can survive multiple requests.
What defines the EntityManager vs Transaction relation?
This is actually a choice of the application developer, but the most frequent way to use the JPA Entity Manager is with the “Entity Manager per application transaction” pattern. This is the most common way to inject an entity manager:
@PersistenceContext
private
EntityManager em;
How does @PersistenceContext work?
inject an entity manager only once at container startup time, given that entity managers are so short lived, and that there are usually multiple per request.
How does @Transactional work ?
Actually three separate components are needed:
- The EntityManager Proxy itself
- The Transactional Aspect
- The Transaction Manager
@Configuration
public
class
EntityManagerFactoriesConfiguration {
@Autowired
private
DataSource dataSource;
@Bean
(name =
"entityManagerFactory"
)
public
LocalContainerEntityManagerFactoryBean emf() {
LocalContainerEntityManagerFactoryBean emf = ...
}
}
configure the Transaction Manager and to apply the Transactional Aspect in annotated classes:
@Configuration
@EnableTransactionManagement
public
class
TransactionManagersConfig {
@Autowired
EntityManagerFactory emf;
@Autowired
private
DataSource dataSource;
@Bean
(name =
"transactionManager"
)
public
PlatformTransactionManager transactionManager() {
JpaTransactionManager tm =
new
JpaTransactionManager();
tm.setEntityManagerFactory(emf);
tm.setDataSource(dataSource);
return
tm;
}
}
8-
Transaction Management MCQ
1. Transactions can be described with key properties:-
a) Atomicity
b) Consistency
c) Isolation
d) All of the mentioned
2. To access a database running on the Derby server, you have to add:-
a) Derby client library
b) Tomcat client library
c) All of the mentioned
d) None of the mentioned
3. Spring’s transaction support offers a set of technology-independent facilities, including transaction managers.
a) org.springframework.transaction.PlatformTransactionManager
b) org.springframework.transaction.support.TransactionTemplate
c) all of the mentioned
d) none of the mentioned
Spring’s transaction support offers a set of technology-independent facilities, a transaction template (e.g., org.springframework.transaction.support.TransactionTemplate), and transaction declaration support to simplify your transaction management tasks.
4. Spring’s core transaction management abstraction is based on the interface:-
a) PlatformTransaction
b) PlatformTransactionManager
c) TransactionManager
d) PlatformManager
It encapsulates a set of technology-independent methods for transaction management.
5. The PlatformTransactionManager interface provides methods for working with transactions:
a) getTransaction(TransactionDefinition definition)
b) commit(TransactionStatus status)
c) rollback(TransactionStatus status)
d) all of the mentioned
TransactionStatus getTransaction(TransactionDefinition definition) throws
TransactionException
• void commit(TransactionStatus status) throws TransactionException;
• void rollback(TransactionStatus status) throws TransactionException;
6. Spring has several built-in implementations of PlatformTransactionManager interface for use with different transaction management APIs.
a) True
b) False
7. A transaction manager is declared in the Spring IoC container as a normal bean.
a) True
b) False
8. Method that allows you to start a new transaction (or obtain the currently active transaction).
a) getTransaction()
b) commit()
c) rollback()
d) all of the mentioned
9. PlatformTransactionManager is an abstract unit for transaction management.
a) True
b) False
10. Method to start a new transaction with that definition:-
a) getTransaction()
b) commit()
c) rollback()
d) none of the mentioned
11. To help you control the overall transaction management process and transaction exception handling.
a) SpringTransactionTemplate
b) TransactionTemplate
c) Transaction
d) None of the mentioned
12. You just have to encapsulate your code block in a callback class that implements the TransactionCallback interface and pass it to the TransactionTemplate execute method for execution. In this way, you don’t need to repeat the boilerplate transaction management code for this block.
a) True
b) False
13. A TransactionTemplate can accept a transaction callback object that implements:-
a) TransactionCallback
b) TransactionCallbackWithoutResult class
c) All of the mentioned
d) None of the mentioned
A TransactionTemplate can accept a transaction callback object that implements either the TransactionCallback or an instance of the one implementer of that interface provided by the framework, the TransactionCallbackWithoutResult class.
14. Spring (since version 2.0) offers a transaction advice that can be easily configured via the:-
a) rx:advice
b) bx:advice
c) tx:advice
d) none of the mentioned
This advice can be enabled with the AOP configuration facilities defined in the aop saop schema.
15. You can omit the transaction-manager attribute in the element if your transaction manager has the name transactionManager.
a) True
b) False
This element will automatically detect a transaction manager with this name. You have to specify a transaction manager only when it has a different name.
No comments:
Post a Comment
Note: only a member of this blog may post a comment.