JDBC事务处理【代码实现】

本文介绍了如何在Java中使用C3P0连接池进行事务管理,包括开启事务、设置保存点以及在异常发生时进行回滚。通过示例代码展示了如何在事务中执行SQL更新操作,并在遇到错误时选择性地回滚到保存点,确保数据的一致性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

概述

在这里插入图片描述在这里插入图片描述

代码实现

在这里插入图片描述

package cn.tedu.transaction;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class TransDemo1 {
    public static void main(String[] args) {
//        double p=0x3p4;
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        //创建连接池对象
        ComboPooledDataSource source=new ComboPooledDataSource();
        try {
            //1.获取数据库连接
            conn = source.getConnection();
            //2.开启事务
            //默认布尔值为true,true代表会自动提交每一条sql语句(每条sql都是单独的事务)
            //false则不会自动提交每一条sql语句当前语句以及之后的sql语句都在一次事务中
            conn.setAutoCommit(false);
            //3.进行数据库操作
            ps = conn.prepareStatement("update user set money=money-100 where name=?");
            ps.setString(1,"a");

            //异常语句
//            int i=1/0;

            //执行sql语句
            ps.executeUpdate();
            ps = conn.prepareStatement("update user set money=money+100 where name=?");
            ps.setString(1,"b");
            //执行sql语句
            ps.executeUpdate();

            //4.提交事务
            conn.commit();

        } catch (Exception e) {
            e.printStackTrace();
            //回滚事务
            if(conn!=null)
            try {
                conn.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }finally {
            if(ps!=null)
                try {
                    ps.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }finally {
                    ps=null;
                }
            if(conn!=null)
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }finally {
                    conn=null;
                }
        }
    }
}

带保存点的事务

在这里插入图片描述

package cn.tedu.transaction;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import java.sql.*;

public class TransDemo2 {
    public static void main(String[] args) {
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        //声明保存点的对象
        Savepoint sp=null;
        //创建连接池对象
        ComboPooledDataSource source=new ComboPooledDataSource();
        try {
            conn = source.getConnection();
            //开启事务
            //默认布尔值为true,true代表会自动提交每一条sql语句(每条sql都是单独的事务)
            //false则不会自动提交每一条sql语句当前语句以及之后的sql语句都在一次事务中
            conn.setAutoCommit(false);

            ps = conn.prepareStatement("update user set money=money-100 where name=?");
            ps.setString(1,"a");
            //
            //int i=1/0;
            //执行sql语句
            ps.executeUpdate();
            ps = conn.prepareStatement("update user set money=money+100 where name=?");
            ps.setString(1,"b");
            //执行sql语句
            ps.executeUpdate();

            //添加保存点
            sp = conn.setSavepoint();
            //
            int i=1/0;


            ps = conn.prepareStatement("update user set money=money-100 where name=?");
            ps.setString(1,"a");
            //
            //int i=1/0;
            //执行sql语句
            ps.executeUpdate();
            ps = conn.prepareStatement("update user set money=money+100 where name=?");
            ps.setString(1,"b");
            //执行sql语句
            ps.executeUpdate();

            //提交事务
            conn.commit();

        } catch (Exception e) {
            e.printStackTrace();
            //回滚事务
            if(conn!=null)
                try {
                    //判断保存点的对象是否为null
                    if(sp!=null){
                        //回滚到保存点
                        conn.rollback(sp);
                        //继续提交回滚点之前的代码
                        conn.commit();
                    }else{
                        //没有保存点全部回滚
                        conn.rollback();
                    }
                    conn.rollback();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
        }finally {
            if(ps!=null)
                try {
                    ps.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }finally {
                    ps=null;
                }
            if(conn!=null)
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }finally {
                    conn=null;
                }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Sparky*

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值