JDBC和連接池03
8.事務
8.1事務介紹
- 基本介紹
- JDBC程式中當一個Connection物件創建時,默認情況下是自動提交事務:每次執行一個SQL陳述句時,如果執行成功,就會向資料庫自動提交,而不能回滾,
- JDBC程式中為了讓多個SQL陳述句作為一個整體執行,需要使用事務
- 呼叫Connection的setAutoCommit(false)可以取消自動提交事務
- 在所有的SQL陳述句都執行成功后,呼叫Connection的commit();方法提交事務
- 在其中某個操作失敗或者出現例外時,呼叫Connection的rollback();方法回滾事務


8.2事務處理
應用實體
模擬經典的轉賬業務
首先創建一張account表,插入兩條資料
CREATE TABLE ACCOUNT(
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(32) NOT NULL DEFAULT '',
balance DOUBLE NOT NULL DEFAULT 0
)CHARACTER SET utf8;
INSERT INTO ACCOUNT VALUES(NULL,'馬云',3000),(NULL,'馬化騰',10000);
SELECT * FROM ACCOUNT;
package li.jdbc.transaction_;
import li.jdbc.utils.JDBCUtils;
import org.junit.Test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
/**
* 演示JDBC中如何使用事務
*/
public class Transaction_ {
//沒有使用事務
@Test
public void noTransaction() {
//操作轉賬業務
//1.得到連接
Connection connection = null;
//2.組織sql陳述句
String sql = "update account set balance=baLance-100 where id=1";
String sql2 = "update account set balance=baLance+100 where id=2";
//3.創建PreparedStatement物件
PreparedStatement preparedStatement = null;
try {
connection = JDBCUtils.getConnection();//在默認情況下,connection默認自動提交
preparedStatement = connection.prepareStatement(sql);
preparedStatement.executeUpdate(); //執行第一條sql
int i = 1 / 0;//拋出例外--模擬例外可能--可以看到出現例外狀態之后的陳述句沒有執行
preparedStatement = connection.prepareStatement(sql2);
preparedStatement.executeUpdate();//執行第二條sql
} catch (SQLException e) {
e.printStackTrace();
} finally {
//關閉資源
JDBCUtils.close(null, preparedStatement, connection);
}
}
//使用事務來解決
@Test
public void useTransaction() {
//操作轉賬業務
//1.得到連接
Connection connection = null;
//2.組織sql陳述句
String sql = "update account set balance=baLance-100 where id=1";
String sql2 = "update account set balance=baLance+100 where id=2";
//3.創建PreparedStatement物件
PreparedStatement preparedStatement = null;
try {
connection = JDBCUtils.getConnection();//在默認情況下,connection默認自動提交
//將connection設定為不自動提交
connection.setAutoCommit(false);
preparedStatement = connection.prepareStatement(sql);
preparedStatement.executeUpdate(); //執行第一條sql
int i = 1 / 0;//拋出例外
preparedStatement = connection.prepareStatement(sql2);
preparedStatement.executeUpdate();//執行第二條sql
//在這里提交事務
connection.commit();
} catch (Exception e) {
//如果在try里面出現了例外,就會進入catch陳述句,
// 這意味著我們可以在catch陳述句里面進行回滾,即撤銷執行的SQL陳述句
System.out.println("執行發生了例外,撤銷已執行的SQL");
try {
connection.rollback();//沒有填寫保存點就默認回滾到事務開始的狀態
} catch (SQLException ex) {
ex.printStackTrace();
}
e.printStackTrace();
} finally {
//關閉資源
JDBCUtils.close(null, preparedStatement, connection);
}
}
}
- 沒有使用事務(noTransaction)的運行結果:可以看到因為默認為直接提交事務,在出現例外后沒有執行例外后面的陳述句就進入了catch陳述句,造成資料錯誤
- 使用了事務(useTransaction)之后:可以看到由于在catch陳述句中進行了回滾操作,在捕獲到例外之后直接進行回滾,保證資料的一致性
9.批處理
- 基本介紹
- 當需要成批插入或者更新記錄時,可以采用Java的批量更新機制,這一機制允許多條陳述句一次性提交給資料庫批量處理,通常情況下比單獨提交處理更有效率
- JDBC的批量處理陳述句包括下面方法:
- addBatch():添加需要批量處理的SQL陳述句或引數
- executeBatch():執行批量處理陳述句
- clearBatch():清空批處理包的陳述句
- JDBC連接MySQL時,如果要使用批處理功能,請在url中加引數?rewriteBatchedStatements=true
- 批處理往往和PreparedStatement一起搭配使用,可以既減少編譯次數,又減少運行次數,效率大大提高
9.1批處理應用
例子
- 演示向admin2表中添加5000條資料,看看使用批處理耗時多久
- 注意批處理需要修改組態檔的資料:url=jdbc:mysql://localhost:3306/資料庫?rewriteBatchedStatements=true
user=root
password=123456
url=jdbc:mysql://localhost:3306/hsp_db02?rewriteBatchedStatements=true
driver=com.mysql.jdbc.Driver
首先創建測驗表admin2
CREATE TABLE admin2(
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(32) NOT NULL,
PASSWORD VARCHAR(32) NOT NULL );
SELECT COUNT(*) FROM admin2;
測驗程式:
package li.jdbc.batch_;
import li.jdbc.utils.JDBCUtils;
import org.junit.Test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
/**
* 演示java的批處理
*/
public class Batch_ {
//傳統方法,添加5000條資料到admin2
@Test
public void noBatch() throws Exception {
//獲取連接
Connection connection = JDBCUtils.getConnection();
//sql
String sql = "insert into admin2 values (null,?,?)";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
System.out.println("開始執行");
long start = System.currentTimeMillis();
for (int i = 0; i < 5000; i++) {
preparedStatement.setString(1, "jack" + i);
preparedStatement.setString(2, "666");
preparedStatement.executeUpdate();
}
long end = System.currentTimeMillis();
System.out.println("傳統的方式耗時:" + (end - start));
//關閉連接
JDBCUtils.close(null, preparedStatement, connection);
}
//使用批量方式添加資料--注意在組態檔添加引數?rewriteBatchedStatements=true
@Test
public void batch() throws Exception {
//獲取連接
Connection connection = JDBCUtils.getConnection();
//sql
String sql = "insert into admin2 values (null,?,?)";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
System.out.println("開始執行");
long start = System.currentTimeMillis();
for (int i = 0; i < 5000; i++) {
preparedStatement.setString(1, "jack" + i);
preparedStatement.setString(2, "666");
//將SQL陳述句加入到批處理包中
preparedStatement.addBatch();
//當有1000條SQL時,再批量執行
if ((i + 1) % 1000 == 0) {//每滿1000條時,就批量執行
preparedStatement.executeBatch();
//執行完就清空批處理包
preparedStatement.clearBatch();
}
}
long end = System.currentTimeMillis();
System.out.println("批量方式耗時:" + (end - start));
//關閉連接
JDBCUtils.close(null, preparedStatement, connection);
}
}
9.2批處理原始碼分析
在上述代碼中,在preparedStatement.addBatch();陳述句旁打上斷點,點擊debug,點擊step into
可以看到游標跳轉到了如下方法:
public void addBatch() throws SQLException {
if (this.batchedArgs == null) {
this.batchedArgs = new ArrayList();
}
this.batchedArgs.add(new PreparedStatement.BatchParams(this.parameterValues, this.parameterStreams, this.isStream, this.streamLengths, this.isNull));
}
第一次執行該方法時,會創建Arraylist型別的物件集合elementDate=>Object[],elementDate=>Object[]用來存放我們預處理的SQL陳述句,當elementDate滿后,就按照1.5倍擴容
當添加到指定的值后,就會執行executeBatch();
批處理會減少我們發送SQL陳述句的網路開銷,并且減少編譯次數,因此效率提高了
1.5倍擴容:
9.3.事務和批處理的區別
-
事務:
事務底層是在資料庫方存盤SQL,沒有提交事務的資料放在資料庫的臨時表空間,
最后一次提交是把臨時表空間的資料提交到資料庫服務器執行
事務消耗的是資料庫服務器記憶體 -
批處理:
批處理底層是在客戶端存盤SQL
最后一次執行批處理是把客戶端存盤的資料發送到資料庫服務器執行,
批處理消耗的是客戶端的記憶體
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/514285.html
標籤:其他
上一篇:【python】18行代碼帶你采集國外網小姐姐絕美圖片
下一篇:序列化組件
