Oracle&DB
Oracle 트랜잭션(Transaction) - 다건의 쿼리문, 대량데이터 처리
Code GGOON
2020. 10. 9. 15:17
반응형
비지니스 로직이 적요된 프로그램에서 Oracle 데이터베이스로 여러건의 쿼리, 또는 대량의 데이터에 대해 삽입과 수정을 일괄로 처리해야하는 경우가 있다, 이때 데이터베이스 쿼리문을 처리중 중간에 에러가 발생하거나, 여러가지 변수로 문제가 발생할 수 있는데, 이때 롤백을 통해서 처리 이전으로 상태를 되돌려야 한다.
이런 경우 트랜잭션(Transaction)을 이용하여 손쉽게 관리코드를 구현 할 수 있다.
Example
OracleConnection con ;
OracleCommand comm ;
OracleTransaction oraTrans=null; //오라클 트랜젝션
con = new OracleConnection();
con.ConnectionString = " Data Source=DAUL;USER ID=scott; Password=tiger; ..." ;
con.Open();
oraTrans =con.BeginTransaction(); //Oracle Transaction Init.
comm = new OracleCommand();
try
{
comm.Connection = con;
comm.Transaction = oraTrans; //커맨드에 트랜젝션 명시
comm.CommandText = "INSERT INTO.......";
comm.ExecuteNonQuery() ;
comm.CommandText = "INSERT INTO......";
comm.ExecuteNonQuery() ;
..... 계속
comm.Transaction.Commit(); //커밋
}
catch(Exception ex)
{
comm.Transaction.Rollback(); //롤백
}
finally
{
comm.Dispose();
con.Close();
}
반응형