事務的概念在我看來是指的是一組sql序列,這個序列是一塊執行的單位,要麼全部執行,要不全部執行,這樣可以很好的對數據庫進行並發控制。
因為數據庫是多個用戶都可以同時操作的,如果多個用戶同時操作一個數據,就容易造成數據的不一致,所以事務作為並發控制的一個基本單位很有必要。
1.原子性:事務是一個完整的整體,所有的操作和數據都是一個整體。
2.一致性:事務的操作是一致性的。
3.隔離性:事務之間的操作是相互隔離的。
4.持久性:事務的操作是持久的,即使出現了錯誤也會完成。
BEGIN TRANSACTION
COMMIT TRANSACTION
ROLLBACK TRANSACTION
public static Statement getStatement(){
Statement st = null;
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/jsp_dbb", "root", "");
st = (Statement) conn.createStatement();
} catch (Exception e) {
// TODO: handle exception
}
return st;
}
public static void insertUserData(){
try {
String sql = "INSERT INTO tbl_user(id,name,password,email)" +
"VALUES(10,'Tom','123456','[email protected]')";
Statement st = getStatement();
int count = st.executeUpdate(sql);
System.out.println("插入了"+count+"行用戶數據");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public static void insertAddressData(){
try {
String sql = "INSERT INTO tbl_address(id,city,country,user_id)" +
"VALUES(1,'shanghai','china,'10')";
Statement st = getStatement();
int count = st.executeUpdate(sql);
System.out.println("插入了"+count+"行地址數據");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public static void main(String[] args) {
insertUserData();
insertAddressData();
}
這段代碼會報錯,因為在tbl_address中已經有id = 1的數據了,具體如下圖:
插入了1行用戶數據
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '10')' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:377)
at com.mysql.jdbc.Util.getInstance(Util.java:360)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:978)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3887)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3823)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2435)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2582)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2526)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1618)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1549)
at liu.peng.jdbc.TransactionTest.insertAddressData(TransactionTest.java:37)
at liu.peng.jdbc.TransactionTest.main(TransactionTest.java:46)
檢查發現只是插入user表的數據,沒有插入address表數據,造成數據不完整。
事務處理的核心代碼就是
conn.setAutoCommit(false);
在執行完之後的提交。
conn.commit();
以及在捕獲異常時候的回滾。
具體代碼如下:
public static Connection getConnection(){
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/jsp_dbb", "root", "");
} catch (Exception e) {
// TODO: handle exception
}
return conn;
}
public static void insertUserData(Connection conn)throws SQLException{
String sql = "INSERT INTO tbl_user(id,name,password,email)" +
"VALUES(10,'Tom','123456','[email protected]')";
Statement st = (Statement) conn.createStatement();
int count = st.executeUpdate(sql);
System.out.println("插入了"+count+"行用戶數據");
}
public static void insertAddressData(Connection conn) throws SQLException{
String sql = "INSERT INTO tbl_address(id,city,country,user_id)" +
"VALUES(1,'shanghai','china,'10')";
Statement st = (Statement) conn.createStatement();
int count = st.executeUpdate(sql);
System.out.println("插入了"+count+"行地址數據");
}
public static void main(String[] args) {
Connection conn = null;
try {
conn = getConnection();
conn.setAutoCommit(false);
insertUserData(conn);
insertAddressData(conn);
conn.commit();
} catch (SQLException e) {
System.out.println("==========捕獲SQL異常=========");
e.printStackTrace();
try {
conn.rollback();
System.out.println("=======s事務回滾成功=======");
} catch (Exception e2) {
e2.printStackTrace();
}
}finally{
try {
if (conn != null) {
conn.close();
}
} catch (Exception e3) {
e3.printStackTrace();
}
}
}
Console界面的報錯信息為:
插入了1行用戶數據
==========捕獲SQL異常=========
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '10')' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:377)
at com.mysql.jdbc.Util.getInstance(Util.java:360)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:978)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3887)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3823)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2435)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2582)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2526)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1618)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1549)
at liu.peng.jdbc.TransactionTest.insertAddressData(TransactionTest.java:31)
at liu.peng.jdbc.TransactionTest.main(TransactionTest.java:40)
=======s事務回滾成功=======
證明兩條數據都沒有插入進去保證了數據的完整性,而第一次只是插入user表的數據,沒有插入address表數據,造成數據不完整。