jdbc 트랜잭션 mybatis 트랜잭션 spring tx

이전영·2022년 3월 14일
0

jdbc 트랜잭션

public int tx() {
		int num = 0;
		Connection con = null;
		try {
			con = DriverManager.getConnection(url, userid, passwd);
			DeptDAO dao = new DeptDAO();
			
			////////트랜잭션 처리//////////////////////////////
			con.setAutoCommit(false);
			// insert
			num = dao.insert2(con, new DeptDTO(9, "aa", "aa"));
			// delete
			num = dao.delete2(con, 97);
			con.commit();
			//////////////////////////////////////////////
		} catch (SQLException e) {
			try {
				con.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			System.out.println("tx 작업 실패로 인해 모든 작업이 rollback 됨.");
//			e.printStackTrace();
		} finally {
			try {
				if (con != null)
					con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	
		return 0;
	}//end tx
    
    

mybatis 트랜잭션 처리

@Override
	public int orderDone(OrderDTO dto, String cartNum) throws Exception {
		int num = 0;
		SqlSession session = MySqlSessionFactory.getSession();
		try {
			//트랜잭션 처리
			//1. OrderInfo테이블 insert
			CartDAO dao = new CartDAO();
			num = dao.orderDone(session, dto);
			
			//2. Cart테이블 delete
			num = dao.cartDel(session, Integer.parseInt(cartNum));
			
			session.commit();
		}catch(Exception e) {
			session.rollback();
		}finally {
			session.close();
		}
		return num;
	}

profile
개발자 3년차

0개의 댓글