Содержание
- 2. Database Transaction A database transaction is a sequence of actions that are treated as a single
- 3. ACID (1 of 2) Atomicity. A transaction should be treated as a single unit of operation
- 4. ACID (2 of 2) Isolation. There may be many transactions processing with the same data set
- 5. Spring Transaction Management Spring framework provides an abstract layer on top of different underlying transaction management
- 6. Local Transactions Local transaction management can be useful in a centralized computing environment where application components
- 7. Global Transactions Global transaction management is required in a distributed computing environment where all the resources
- 8. Programmatic vs. Declarative Spring supports two types of transaction management: Programmatic transaction management: you have manage
- 9. Programmatic vs. Declarative Declarative transaction management is preferable over programmatic transaction management * Victor Mozharsky
- 10. Spring Transaction Abstractions The key to the Spring transaction abstraction is defined by PlatformTransactionManager interface in
- 11. PlatformTransactionManager getTransaction - returns a currently active transaction or create a new one, according to the
- 12. TransactionDefinition Is the core interface of the transaction support in Spring and it is defined as
- 13. TransactionDefinition Methods getPropagationBehavior - returns the propagation behavior getIsolationLevel - returns the degree to which this
- 14. Isolation Level (1 of 2) TransactionDefinition.ISOLATION_DEFAULT - the default isolation level TransactionDefinition.ISOLATION_READ_COMMITTED - indicates that dirty
- 15. Isolation Level (2 of 2) TransactionDefinition.ISOLATION_REPEATABLE_READ - dirty reads and non-repeatable reads are prevented; phantom reads
- 16. Propagation Types (1 of 2) TransactionDefinition.PROPAGATION_MANDATORY - support a current transaction; throw an exception if no
- 17. Propagation Types (2 of 2) TransactionDefinition.PROPAGATION_REQUIRED - support a current transaction; create a new one if
- 18. TransactionStatus interface Provides a simple way for transactional code to control transaction execution and query transaction
- 19. TransactionStatus Methods hasSavepoint - returns whether this transaction internally carries a savepoint, that is, has been
- 20. Declarative Transaction Management This approach allows you to manage the transaction with the help of configuration
- 21. Configuring Transaction Management class="org.springframework.orm.jpa.JpaTransactionManager"> ref="entityManagerFactory"/> * Victor Mozharsky
- 22. Using @Transactional You can place the @Transactional annotation before a class definition, or a public method
- 23. @Transactional Attributes propagation (Propagation.REQUIRED by default) Isolation (Isolation.DEFAULT by default) timeout (TransactionDefinition.TIMEOUT_DEFAULT) readonly rollbackFor rollbackForClassName noRollbackFor
- 24. Exercise: Insert New Customer Insert new record to the CUSTOMER DB table – this problem was
- 25. New Save Interface Method package com.bionic.edu; public interface CustomerDao { public Customer findById(int id); public void
- 26. Save DAO Implementation @Repository public class CustomerDaoImpl implements CustomerDao{ @PersistenceContext private EntityManager em; public Customer findById(int
- 27. Save Service Implementation @Named public class CustomerServiceImpl implements CustomerService{ @Inject private CustomerDao customerDao; public Customer findById(int
- 28. Example: Payment of a New Customer The task is to add a payment of a new
- 29. PaymentDaoImpl Class package com.bionic.edu; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.springframework.stereotype.Repository; @Repository public class PaymentDaoImpl implements PaymentDao{
- 30. CustomerServiceImpl Class @Transactional public void add(Customer c, Payment p){ save(c); p.setCustomerId(c.getId()); paymentDao.save(p); } } * Victor
- 31. Output INSERT on table 'PAYMENT' caused a violation of foreign key constraint 'CUSTOMER_FK' for key (0).
- 32. CustomerServiceImpl Class package com.bionic.edu; import javax.inject.Inject; import javax.inject.Named; import org.springframework.transaction.annotation.Transactional; @Named public class CustomerServiceImpl implements CustomerService{
- 33. PaymentServiceImpl Class @Named public class PaymentServiceImpl implements PaymentService{ @Inject private PaymentDao paymentDao; @Inject private CustomerService customerService;
- 34. Output INSERT on table 'PAYMENT' caused a violation of foreign key constraint 'CUSTOMER_FK' for key (0).
- 35. CustomerServiceImpl Class package com.bionic.edu; import javax.inject.Inject; import javax.inject.Named; import org.springframework.transaction.annotation.Transactional; @Named public class CustomerServiceImpl implements CustomerService{
- 36. Output JpaDialect does not support savepoints - check your JPA provider's capabilities The reason is that
- 37. CustomerServiceImpl Class package com.bionic.edu; import javax.inject.Inject; import javax.inject.Named; import org.springframework.transaction.annotation.Transactional; @Named public class CustomerServiceImpl implements CustomerService{
- 38. Output Customer and Payment entities are successfully saved The problem is in the risk of data
- 39. Payment Entity @Entity public class Payment { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; private java.sql.Date dt; .
- 40. Customer Entity @Entity public class Customer { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; . . . .
- 41. Main Class Customer c = new Customer(); . . . . . . . . .
- 43. Скачать презентацию