- 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.
- 30天自制操作系統(tǒng)
- Linux運(yùn)維之道(第3版)
- 樂學(xué)Windows操作系統(tǒng)
- Linux從零開始學(xué)(視頻教學(xué)版)
- Persistence in PHP with the Doctrine ORM
- Kali Linux滲透測試全流程詳解
- Docker+Kubernetes應(yīng)用開發(fā)與快速上云
- Windows 7中文版從入門到精通(修訂版)
- VMware NSX Cookbook
- 云原生落地:產(chǎn)品、架構(gòu)與商業(yè)模式
- Learning IBM Watson Analytics
- 操作系統(tǒng)之哲學(xué)原理第2版
- 15分鐘!畫出我的漫畫角色:賣萌篇
- Administering ArcGIS for Server
- 鴻蒙應(yīng)用程序開發(fā)