728x90
반응형
Transaction
- 원자성 : 모두 다 정상처리 되거나 모두 다 처리되지 않는..
package dbc6; import java.sql.Statement; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class TransactionTest { Connection con; //null ->&주소값 static { try { Class.forName("oracle.jdbc.driver.OracleDriver"); } catch (ClassNotFoundException cne) { cne.printStackTrace(); } } public void connect() { String url = "jdbc:oracle:thin:@localhost:1521:xe"; try { con = DriverManager.getConnection(url, "test", "1111"); // 자동커밋여부 제어 con.setAutoCommit(false); System.out.println("Connection Success"); } catch (SQLException se) { se.printStackTrace(); } } public void insert() { connect(); Statement stmt = null; boolean isOrderSuccess = false; try { stmt = con.createStatement(); String sql = "INSERT INTO PAY VALUES ('aaa', '001', 1000)"; stmt.executeUpdate(sql); sql = "INSERT INTO PAY VALUES ('bbb', '001', 1000)"; stmt.executeUpdate(sql); sql = "INSERT INTO orderList VALUES ('001', 'p2014001')"; stmt.executeUpdate(sql); isOrderSuccess = true; } catch (SQLException se) { System.out.println("추가 실패"); se.printStackTrace(); } finally { if (isOrderSuccess) { try { con.commit(); } catch (Exception e) { e.printStackTrace(); } } else { try { con.rollback(); } catch (Exception e) { e.printStackTrace(); } } try { stmt.close(); con.close(); } catch (Exception e) { e.printStackTrace(); } } } public static void main(String[] args) { TransactionTest tt = new TransactionTest (); tt.insert(); } }
728x90
반응형
'[JDBC]' 카테고리의 다른 글
[JDBC] PROCEDURE (0) | 2024.02.02 |
---|---|
[JDBC] FUNCTION (0) | 2024.02.02 |
[JDBC] ex) [회원정보테이블] (0) | 2024.01.31 |
[JDBC] DAO, DTO (0) | 2024.01.31 |
[JDBC] Ex (0) | 2024.01.31 |