事务(mysql事务、jdbc事务)

一、MYSQL事务

1、事务

概念:事务是一个用户定义的数据库操作序列,这些操作要么全做要么全不做,是一个不可分割的工作单位。事务可以是一条sql语句,一组sqi语句或者整个程序。

特性(ACDI):

(1)原子性:一个事务中的操作要么全做,要么全不做。

(2)一致性:事务执行的结果必须使数据库从一个一致性状态转换为另一个一致性状态。

(3)隔离性:并发执行的事务之间互不干扰。

(4)持久性:一个事务一旦提交对数据库的操作就是永久性的。(已经存储到了磁盘)

2、MYSQL的事务:

(1)如果用户不对数据库进行操作,默认情况下事务直接开始并提交(将对数据库的更新写到磁盘)。

例如:在login数据表中执行以下插入操作后:

INSERT login VALUES('mysql','事务')

执行查询语句后,数据库中已经存在数据。

(2)手动操作事务的开始和提交:

先开启事务:

START TRANSACTION

执行插入语句:

INSERT login VALUES('星期一','Monday')

此时执行查询语句,依旧能够查询到插入的数据:

 但是,此时并未对事务进行提交操作,数据并未进入磁盘。

如果执行回滚语句,插入的数据将会消失(从事务开启到回滚之间的操作均被撤销):

ROLLBACK

但是,如果执行的是提交语句,数据将会被写入磁盘。

 

二、JDBC事务

1、自动提交方式,即不手动操作,只适用于对数据库的更新操作

创建JavaBean:

package pers.zhb.domain;
public class Student {
    private String studentno;
    private String sname;
    private String sex;
    private String birthday;
    private String classno;
    private String point;
    private String phone;
    private String email;
    public String getStudentno() {
        return studentno;
    }
    public void setStudentno(String studentno) {
        this.studentno = studentno;
    }
    public String getSname() {
        return sname;
    }
    public void setSname(String sname) {
        this.sname = sname;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getBirthday() {
        return birthday;
    }
    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }
    @Override
    public String toString() {
        return "Student [studentno=" + studentno + ", sname=" + sname + ", sex="
                + sex + ", birthday=" + birthday + ", classno=" + classno
                + ", point=" + point + ", phone=" + phone + ", email=" + email
                + "]";
    }
    public String getClassno() {
        return classno;
    }
    public void setClassno(String classno) {
        this.classno = classno;
    }
    public String getPoint() {
        return point;
    }
    public void setPoint(String point) {
        this.point = point;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
}

不设置对事务的提交方式:

package pers.zhb.test;
import org.apache.commons.dbutils.QueryRunner;
import pers.zhb.dbutils.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Test{
    public static void main(String[] args){
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            con = c3p0Utiils.getConnection();//从连接池中获取连接
            //con.setAutoCommit(false);//设置为手动提交事务的方式
            QueryRunner qr = new QueryRunner();
            String sql = "INSERT INTO student(studentno,sname,sex,birthday,classno,point,phone,email) VALUES(?,?,?,?,?,?,?,?)";
            Object[] params = { "20191813", "huab", "男", "1988-12-01", "201901", "239", "16623540978", "Tom.@3218n.com" };
            int num = qr.update(con, sql, params);
            //con.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            c3p0Utiils.close(con, null, rs);
        }
    }
}

2、手动提交方式:

package pers.zhb.test;
import org.apache.commons.dbutils.QueryRunner;
import pers.zhb.dbutils.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Test{
    public static void main(String[] args){
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            con = c3p0Utiils.getConnection();//从连接池中获取连接
            con.setAutoCommit(false);//设置为手动提交事务的方式
            QueryRunner qr = new QueryRunner();
            String sql = "INSERT INTO student(studentno,sname,sex,birthday,classno,point,phone,email) VALUES(?,?,?,?,?,?,?,?)";
            Object[] params = { "20191815", "huab", "男", "1988-12-01", "201901", "239", "16623540978", "Tom.@3218n.com" };
            int num = qr.update(con, sql, params);
            con.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            c3p0Utiils.close(con, null, rs);
        }
    }
}

不提交的话,数据库中是没有数据的。

原文地址:https://www.cnblogs.com/zhai1997/p/11631347.html