35DAY / mysql / TRANSACTION
TRANSACTION 트랜잭션
OldidDao.java : Oldid DB에 id값을 저장하는 클래스
CustomerService.java : 회원을 삭제와 동시에 데이터베이스에 추가하는 클래스
둘 중 하나가 실패시 ROLLBACK을 하고
성공했을 때 COMMIT을 한다.
package Service;
import java.sql.Connection;
import java.sql.SQLException;
public class CustomerService {
private DBService dbservice;
private CustomerDao customerDao;
private OldidDao oldidDao;
//java실행시 바로실행
public CustomerService(){
dbservice = new DBService();
}
public void removeCustomer(int id){
customerDao = new CustomerDao();
oldidDao = new OldidDao();
Connection conn = null;
try{
conn=dbservice.getConnection();
customerDao.deleteCustomer(dbservice,conn, id);
oldidDao.insertId(dbservice, conn, id);
conn.commit();
}
catch (SQLException e){
e.printStackTrace();
try{
conn.rollback();
}catch (SQLException e1) {
e1.printStackTrace();
}
}
finally{
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
package Service;
import java.sql.*;
public class OldidDao {
public int insertId(DBService dbservice, Connection conn,int id) throws SQLException{
int returnRow = 0;
PreparedStatement stmt = null;
stmt = conn.prepareStatement("INSERT INTO oldid(id) VALUES (?)");
stmt.setInt(1, id);
returnRow=stmt.executeUpdate();
return returnRow;
}
}