java 开启事务管理,保证数据一致性

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
@Service
public class ICStockBillServiceImpl extends ServiceImpl<ICStockBillDao, ICStockBillEntity> implements ICStockBillService {
@Resource
private DataSource dataSource;
public void TransactionExample () {
Connection connection = null;
PreparedStatement statement = null;
try {
connection = dataSource.getConnection();
System.out.println("connection"+connection);
// 设置自动提交为false,禁用事务自动提交
connection.setAutoCommit(false);
// 创建Statement
statement = connection.createStatement();
// 执行一系列操作
statement.executeUpdate("INSERT INTO YourTable (column1) VALUES ('value1')");
statement.executeUpdate("UPDATE YourTable SET column1 = 'newValue' WHERE column1 = 'value1'");
// 提交事务
connection.commit();
System.out.println("事务成功提交!");
} catch (SQLException e) {
try {
if (connection != null) {
java 开启事务管理,保证数据一致性
声明:除非特别标注,否则均为本站原创文章,转载时请以链接形式注明文章出处。如若本站内容侵犯了原著者的合法权益,可联系本站删除。



