25/03/10 (월)
주말은 왜 이렇게 빨리지나가는걸까요.
그래도 다시 월요일입니다. 드디어 주짓수를 갈수있습니다..!
꿈에서 승격하는 꿈을 꾼거같은데 잘 모르겠습니다.
아무튼 이번주도 힘내보겠습니다.
아니 오후 열심히 필기한거 복습하려했는데 다 날아갔네..
1. 강의
CH08 | 트랜잭션, 동시성 제어, 회복
8.1) 트랜잭션
1. 트랜잭션의 개념
- 작업의 단위
2. 트랜잭션의 성질
ACID : 원자성, 일관성, 고립성, 지속성
- 원자성 : 전부 수행하거나, 아예 실행하지 않아야 함
- 일관성 : 테이블이 생성될 때마다 무결성 제약조건을 통해 명시되어 일관성을 유지
- 고립성 : 각 트랜잭션은 다른 트랜잭션의 방해를 받지 않고 독립적으로 수행
- 지속성 : 정상적으로 완료되거나 부분 완료된 데이터는 반드시 데이터베이스에 기록되어야 함
다 날라감ㅠㅠ
2. 실습
2.1) 강사님 코드
오전 수업
더보기
select * from test2.customer;
insert into customer values (1, '홍길동');
insert into customer values (2, '이길동');
insert into customer values (3, '삼길동');
-- 위 3개의 insert 는 모두 종료 후 자동 commit 됨. (현재 설정)
select @@autocommit; -- 1 : on, 0 : off
-- autocommit 을 off
insert into customer values (4, '사길동');
commit;
insert into customer values (5, '오길동');
commit;
update customer set name = '육길동' where id = 5;
commit;
delete from customer where id = 5;
commit;
set autocommit = 1;
delete from customer where id = 4;
=================================================================================
set autocommit = 0;
select @@autocommit;
-- 여러 insert, update, delete 같은 DB에 변화를 주는 query를 여러 개 수행
-- 단 query들 전체가 하나의 작업 단위(transaction)로 처리
-- customer truncate; (truncate는 rollback할 수 없음)
select * from test2.customer;
start transaction;
insert into customer values (1, '홍길동');
insert into customer values (2, '이길동');
insert into customer values (3, '삼길동');
commit; -- transaction 완료
rollback; -- transcation 취소
-- 복잡하고 긴 transaction 작업 수행 과정 (5-6시간 걸리는 작업)
-- 개발계 서버에서 코드 작성
-- A, B, C 작업완료 시간이 3시간 걸린다는 가정
-- A,B,C는 완성 D 개발 중... A,B,C는 이미 완성되었으므로
-- D가 문제 있을 경우, A,B,C는 완성된 상태로 rollback 하고 싶다. <= savepoint
start transaction;
-- A 테이블 변화
-- B 테이블 조회, 결과값에 따라 다르게 처리 (PL-SQL)
-- C 테이블 변화(insert)
-- D 테이블 변화(update)
-- E 테이블 조회
-- ....
-- 홍길동, 이길동 insert를 위 A,B,C로 가정, 삼길동 insert를 D로 가정
select * from test2.customer;
insert into customer values (1, '홍길동');
insert into customer values (2, '이길동');
savepoint s1;
insert into customer values (3, '삼길동');
commit; -- transaction 완료
rollback to s1;
오후 수업
더보기
-- 고립수준
-- 한 트랜잭션은 읽기, 다른 트랜잭션을 쓰기를 진행
-- 읽는 트랜잭션이 쓰는 트랜잭션의 변화를 어떻게 대응할 것인가 하는 정책에 따라 다른 결과를 보여준다.
-- set transaction isolation level ___ ;
-- ___ 에 올 수 있는 경우
-- read uncommited : 쓰기 트랜잭션의 변화가 commit 되지 않아도 읽는다.
-- <= 읽기 트랜잭션에서 commit 되지 않은 데이터를 읽은 후 쓰기 트랜잭션에서 rollback 하면
-- 잘못된 데이터를 읽게 된다. ( dirty read )
-- read commited : 쓰기 트랜잭션의 변화가 commit 되어야만 읽는다.
-- <= 읽기 트랜잭션에서 이전에 commit 된 데이터를 읽은 후 쓰기 트랜잭션에서 변경 commit 하면
-- 이전에 읽은 데이터와 달라진다. ( non - repeatable read )
-- <= 읽기 트랜잭션에서 이전에 commit 된 데이터들을 읽은 후 ( 복수개가 될 수 있는) 쓰기 트랜잭션에서 등록 commit 하면
-- 이전에 읽은 데이터들과 달라진다. ( phantom read )
-- repeatable read
-- <= 읽기 트랜잭션에서 이전에 commit 된 데이터를 읽은 후 쓰기 트랜잭션에서 변경 commit 해도
-- 이전에 읽은 데이터는 동일하게 읽는다 ( X )
create table `users` (
`id` int not null,
`name` varchar(20) default null,
`age` int default null,
primary key (`id`)
)
insert into users values (1, 'hong gildong', 30);
select * from users;
-- #1 read uncommited
set transaction isolation level read uncommitted;
start transaction;
select * from users where id = 1; -- 최초 30
-- 쓰기 트랜잭션
start transaction;
update users set age = 21 where id = 1; -- uncommitted 상태
select * from users where id = 1; -- 쓰기 트랜잭션 uncommitted 된 21 ( dirty read )
-- 쓰기 트랜잭션
rollback;
select * from users where id = 1; -- 최초 30
commit;
-- 데이터 초기화
update users set age = 30 where id = 1; commit;
-- #2 read committed
set transaction isolation level read committed;
start transaction;
select * from users where id = 1; -- 최초 30
-- 쓰기 트랜잭션
start transaction;
update users set age = 21 where id = 1;
commit; -- committed 상태
select * from users where id = 1; -- 쓰기 트랜잭션 committed 된 21 ( non-repeatable read )( dirty-read 는 X)
commit;
-- 데이터 초기화
update users set age = 30 where id = 1; commit;
-- #3 read committed
set transaction isolation level read committed;
start transaction;
select * from users where age between 10 and 30; -- 최초 30
-- 쓰기 트랜잭션
start transaction;
insert into users values (2, 'LEE GILDONG', 21);
commit;
select * from users where age between 10 and 30; -- 최초 30 과 쓰기 트랜잭션에서 추가된 21 이 함께 보인다. (phamtom read)
commit;
-- 데이터 초기화
delete from users where id = 2; commit;
-- #4 repeatable read
set transaction isolation level repeatable read;
start transaction;
select * from users where age between 10 and 30; -- 최초 30
-- 쓰기 트랜잭션
start transaction;
insert into users values (2, 'LEE GILDONG', 21);
commit;
select * from users where age between 10 and 30; -- 최초 30 만 보인다. ( phamtom read X )
commit;
3. 마무리
3.1) WorkShop
- 고립수준
3.2) 더 공부할 것
- 오늘꺼 복습하기
'LG 유플러스 유레카 > 데이터베이스 심화' 카테고리의 다른 글
[33일 차] 데이터베이스 심화 (JPA) (0) | 2025.03.13 |
---|---|
[32일 차] 데이터베이스 심화 (MyBatis) (1) | 2025.03.12 |
[31일 차] 데이터베이스 심화 (공통 코드) (0) | 2025.03.11 |