- Java EE 8 Design Patterns and Best Practices
- Rhuan Rocha Jo?o Purifica??o
- 240字
- 2021-07-23 16:55:01
Implementing the Transaction class
The Transaction class is responsible for controlling the transaction lifecycle and defining the transaction delimit:
package com.packt.javaee8.domainstore;
import javax.annotation.PostConstruct;
public class Transaction {
private boolean opened;
@PostConstruct
public void init(){
this.opened = false;
}
public void commit() throws Exception {
if( !opened ) throw new Exception("Transaction is not opened");
opened = false;
}
public void rollback() throws Exception {
if( !opened ) throw new Exception("Transaction is not opened");
opened = false;
}
public void begin() throws Exception {
if( opened ) throw new Exception("Transaction already is opened");
opened = true;
}
public boolean isOpened(){
return opened;
}
}
In the preceding code block, we have the Transaction class, which is responsible for controlling the transaction lifecycle and defining the transaction delimit. This class has the init() method, annotated with @PostConstruct, which configures this method to be called after the constructor is executed. Furthermore, this class has the commit method, which is used when the user needs to confirm the transaction; the rollback method, used to undo all transactions; the begin method, used to open a transaction; and the isOpened method, which is used to verify whether a transaction is open or not.
The transaction is closed if the begin method was not called or if the commit or rollback methods were called and a new call to the begin method was not made.
- Implementing Cisco UCS Solutions
- Linux系統文件安全實戰全攻略
- 構建高可用Linux服務器(第4版)
- Linux Mint Essentials
- FreeRTOS實時內核應用指南
- vSphere Virtual Machine Management
- 構建可擴展分布式系統:方法與實踐
- 高性能Linux服務器構建實戰:系統安全、故障排查、自動化運維與集群架構
- 深入Linux內核架構與底層原理(第2版)
- Linux網絡內核分析與開發
- Docker+Kubernetes應用開發與快速上云
- 無蘋果不生活 The New iPad隨身寶典
- 注冊表應用完全DIY
- Cassandra 3.x High Availability(Second Edition)
- Learning Continuous Integration with Jenkins(Second Edition)