jOOQ - 事務語句
什麼是事務?
事務是一系列一個或多個操作,作為一個單一的工 作單元執行。如果任何任務失敗,則事務失敗。事務必須滿足 ACID 屬性,它們是:
原子性:事務是原子的,這意味著要麼所有操作都成功,要麼都不成功。
一致性:它確保資料庫在成功提交的事務後能夠正確地從一個狀態轉換到另一個狀態。
隔離性:事務彼此隔離。
永續性:一旦事務提交,即使系統崩潰,更改也會持久儲存。
事務操作
常見的事務操作包括:
BEGIN:啟動一個新事務。
COMMIT:使更改永久生效。
ROLLBACK:撤銷更改
jOOQ 中的事務語句
在 jOOQ 中,DSL 類的以下方法用於建立事務語句:
| 序號 | 方法和描述 |
|---|---|
| 1. | transaction(Configuration) 此方法將多個數據庫操作組合在一個事務塊中。 |
| 2. | setTransactionIsolation(int isolationLevel) 它用於設定當前事務的隔離級別。 |
| 3. | commit() 此方法用於提交當前事務。 |
| 4. | rollback() 它將撤消事務期間所做的任何更改。 |
jOOQ 中事務語句的示例
在此示例中,我們將把ALTER TABLE操作和INSERT操作組合在一起。我們將使用以下表進行此操作:
| 排名 | 姓名 | 職位 |
|---|---|---|
| 1 | Aman | 技術作家 |
| 2 | Shriansh | 軟體工程師 |
| 4 | Vivek | 開發者 |
首先在“src -> main -> java -> com.example.demo”內部建立一個包。“com.example.demo”資料夾的名稱將取決於你的專案名稱。在這個包內建立一個 Java 類。我們已將包命名為“service”,並將 Java 類命名為“EmployeeService.java”。你可以隨意命名。
在 jOOQ 中,我們使用alterTable()方法修改表結構,使用renameColumn()方法重新命名列。要將資料插入表中,我們使用insertInto()方法。將以下程式碼片段複製並貼上到“EmployeeService.java”檔案中。
package com.example.demo.service;
import org.jooq.*;
import org.jooq.Record;
import org.jooq.impl.DSL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.sql.DataSource;
@Service
public class EmployeeService {
private final DataSource dataSource;
@Autowired
public EmployeeService(DataSource dataSource) {
this.dataSource = dataSource;
}
public void run() {
// Create a DSLContext using the DataSource
DSLContext create = DSL.using(dataSource, SQLDialect.MYSQL);
// Wrap the operations inside a transaction
create.transaction(configuration -> {
// Create a DSLContext within the transaction context
DSLContext ctx = DSL.using(configuration);
// Manually define the table and columns
Table<?> employee = DSL.table("employee");
Field<Integer> id = DSL.field("id", Integer.class);
Field<String> name = DSL.field("name", String.class);
Field<String> jobTitle = DSL.field("job_title", String.class);
// Rename the column 'id' to 'rank'
ctx.alterTable("employee")
.renameColumn("rank").to("id")
.execute();
// Insert a new employee record into the table
ctx.insertInto(employee, id, name, jobTitle)
.values(3, "Ansh", "Software Engineer")
.execute();
// Fetch the values from the employee table
Result<Record3<Integer, String, String>> result = ctx.select(id, name, jobTitle)
.from(employee)
.fetch();
// Print the results
for (Record record : result) {
Integer employeeId = record.get(id);
String employeeName = record.get(name);
String employeeJobTitle = record.get(jobTitle);
System.out.println("ID: " + employeeId + ", Name: " + employeeName + ", Job Title: " + employeeJobTitle);
}
});
}
}
現在,導航到“com.example.demo”資料夾內的“DemoApplication.java”檔案。然後,編寫如下程式碼:
package com.example.demo;
import com.example.demo.service.EmployeeService;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication implements CommandLineRunner {
private final EmployeeService demoApplication;
public DemoApplication(EmployeeService demoApplication) {
this.demoApplication = demoApplication;
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
demoApplication.run();
}
}
執行此程式碼時,它將把“Rank”列更改為“id”,並將另一行插入到指定的表中:
ID: 1, Name: Aman, Job Title: Technical Writer ID: 2, Name: Shriansh, Job Title: Software Engineer ID: 3, Name: Ansh, Job Title: Software Engineer ID: 4, Name: Vivek, Job Title: Developer