[Docker/MySQL] ํŠธ๋žœ์žญ์…˜(Transaction)

2021. 1. 3. 23:43ใ†๐Ÿ™‹๐Ÿป‍โ™€๏ธ Study/โœ”๏ธ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ( Database )

๋ฐ˜์‘ํ˜•

ํŠธ๋žœ ์žญ์…˜์ด๋ž€(Transaction) ?!

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ์ƒํƒœ๋ฅผ ๋ณ€ํ™˜์‹œํ‚ค๋Š” ํ•˜๋‚˜์˜ ๋…ผ๋ฆฌ์  ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•œ ์ž‘์—… / ํ•œ๊บผ๋ฒˆ์— ์ˆ˜ํ–‰๋˜์–ด์•ผํ•  ์ผ๋ จ์˜ ์ž‘์—… ์—ฐ์‚ฐ์„ ์˜๋ฏธํ•œ๋‹ค.

์—ฌ๋Ÿฌ ์ค„์˜ SQL ์ฟผ๋ฆฌ๋ฅผ ์‹คํ–‰ํ•  ๋•Œ ์žฅ์• ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๊ฒฝ์šฐ , ์ฟผ๋ฆฌ ์ „์ฒด๋ฅผ ์ทจ์†Œ(rollback) ํ•˜๊ฑฐ๋‚˜ ํ™•์ •(commit)ํ•˜๋Š” ์—ฐ์‚ฐ(์›์ž์„ฑ์— ํ•ด๋‹น)์„ ํ•˜๋ฉฐ

๋ณดํ†ต RDBMS์—์„œ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์ง€์›ํ•˜๋Š” RDBMS์˜ ๊ฐ„ํŒ ๊ธฐ๋Šฅ์ด๋ผ๊ณ  ๋งํ•  ์ˆ˜ ์ž‡๋‹ค.

 

ํŠธ๋žœ์žญ์…˜์˜ ACID ํŠน์„ฑ

  • ์›์ž์„ฑ(Atomicity)
  • ์ผ๊ด€์„ฑ(Consistency)
  • ๊ฒฉ๋ฆฌ์„ฑ(Isolation)
  • ๋‚ด๊ตฌ์„ฑ(Durability)

MySQL(InnoDB)์˜ ํŠธ๋žœ์žญ์…˜

MySQL์˜ ๊ธฐ๋ณธ ์Šคํ† ๋ฆฌ์ง€ ์—”์ง„์€ ' InnoDB'์ด๋‹ค.

MySQL์˜ InnoDB ์—”์ง„์€ ๊ธฐ๋ณธ ์„ค์ •์ด ํŠธ๋žœ์žญ์…˜์ด ๋ฏธ์ง€์› ์ƒํƒœ์ด๋‹ค.

์™œ๋ƒํ•˜๋ฉด autocommit๋ชจ๋“œ (์ค„ ๋‹จ์œ„ ์ปค๋ฐ‹)์ด ์ผœ์ ธ์žˆ๋Š” ์ƒํƒœ์ด๊ธฐ ๋•Œ๋ฌธ์— ํŠธ๋žœ์žญ์…˜์ด ๋ฏธ์ง€์› ์ƒํƒœ์ด๋‹ค.

ํŠธ๋žœ์žญ์…˜์€ ์—ฌ๋Ÿฌ์ค„์ด ์‹คํ–‰๋˜๋‹ค๊ฐ€ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธฐ๋ฉด ๋‹ค ์ทจ์†Œ๊ฐ€ ๋˜๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— autocommit ๋ชจ๋“œ์—์„œ๋Š” ํŠธ๋žœ์žญ์…˜์ด ์ˆ˜ํ–‰ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค!

ํŠธ๋žœ์žญ์…˜์ด ์ง€์›๋˜๊ฒŒ ํ•˜๋ ค๋ฉด ๊ธฐ๋ณธ ์„ค์ •์„ autocommit ๋ชจ๋“œ๋ฅผ 'False'๋กœ ๋ณ€๊ฒฝํ•ด์•ผํ•œ๋‹ค!

 

ํŠธ๋žœ์žญ์…˜ ์‹ค์Šต

โœ”๏ธauto commit - false๋กœ ๋ณ€๊ฒฝํ•˜๊ธฐ

show variables like '%commit%'; // commit ๋ชจ๋“œ ํ™•์ธํ•˜๊ธฐ 
set autocommit=0; // autocommit๋ชจ๋“œ false๋กœ ๋ณ€๊ฒฝ
show variables like '%commit%';

 

MySQL InnoDB์˜ ๊ธฐ๋ณธ์€ autocommit ON ์ƒํƒœ์ด๋‹ค. ํŠธ๋žœ์žญ์…˜ ์‹ค์Šต์„ ํ•˜๋ ค๋ฉด autocommit ๋ชจ๋“œ๋ฅผ False๋กœ ๋ณ€๊ฒฝํ•ด์•ผํ•œ๋‹ค.

 

โœ”๏ธCommit / Rollback ์‹ค์Šต

Commit - SQL๋ฌธ ์‹คํ–‰์„ ํ™•์ • ์ง“๋Š” ๊ฒƒ ( SQL๋ฌธ ์‹คํ–‰ ํ›„ ๋ณ„ ๋ฌธ์ œ ์—†์œผ๋ฉด commit ์ˆ˜ํ–‰ํ•œ๋‹ค.)

Rollback - ๋งˆ์ง€๋ง‰ Commit ์ด์ „ ์ƒํƒœ๋กœ ๋Œ์•„๊ฐ€๋Š” ๊ฒƒ (๋งˆ์ง€๋ง‰ Commit ์ดํ›„์— ์‹คํ–‰๋˜์—ˆ๋˜ ๊ฒƒ๋“ค์€ ๋ชจ๋‘ ์ทจ์†Œ๋œ๋‹ค.)

use employees;

insert into employees values (1000, '2021-01-03', 'yunakim1', 'kim1', 'F', '2021-01-01');
select * from employees where emp_no=1000;

rollback; 

select * from employees where emp_no=1000;

 

Commit์„ ํ•˜์ง€์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— SQL๋ฌธ์ด rollbackํ›„ select๋ฌธ ๊ฒฐ๊ณผ์— ์‚ฌ๋ผ์ง„ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค!

insert into employees values (2000, '2021-01-01', 'yunakim2', 'kim2', 'F', '2021-01-01');
select * from employees where emp_no = 2000;

commit; 

rollback;

select * from employees where emp_no=2000;

SQL๋ฌธ์ด commit ๋˜์–ด rollback ํ›„ select ๋ฌธ ๊ฒฐ๊ณผ๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค!

โœ”๏ธSavePoint ์‹ค์Šต

SavePoint๋ž€?

  • rollback์„ ํŠธ๋žœ์žญ์…˜์˜ ์‹œ์ž‘์ด ์•„๋‹Œ savepoint๊นŒ์ง€ rollbackํ•œ๋‹ค.
  • ์—ฌ๋Ÿฌ ๊ฐœ์˜ savepoint๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์Šคํ† ๋ฆฌ์ง€ ์—”์ง„์— ๋”ฐ๋ผ ์ž‘๋™์—ฌ๋ถ€๊ฐ€ ๋‹ค๋ฅด๋‹ค
insert into employees values (3000, '2021-01-04', 'yunakim3', 'kim3', 'F', '2021-01-04');
savepoint A;
insert into employees values (4000, '2021-01-05', 'yunakim4', 'kim4', 'F', '2021-01-05');
rollback to A;

select * from employees where emp_no = 3000;
select * from employees where emp_no = 4000;

 

๋งˆ์ง€๋ง‰ Commit ์ง€์ ์„ SavePoint A๋กœ ์ง€์ •ํ•˜์—ฌ Rollback์‹œ์— savepoint๊นŒ์ง€ commit๋œ ์ƒํƒœ๋กœ ๋˜๋Œ๋ฆด ์ˆ˜ ์žˆ๋‹ค.

 

 

JDBC ํŠธ๋žœ์žญ์…˜ ์‹ค์Šต

JDBC ์‹ค์Šต์— ๊ด€ํ•œ ์„ค์น˜๋ฒ• ๋ฐ ์‚ฌ์šฉ๋ฒ•์— ๋Œ€ํ•ด์„œ๋Š” ์•„๋ž˜ ๊ธ€์„ ๋ณด๊ณ  ์˜ค์‹œ๋Š” ๊ฒƒ์„ ์ถ”์ฒœํ•ฉ๋‹ˆ๋‹ค!

 

[Docker/MySQL] JDBC - Statement ์‹ค์Šต

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” Docker์— Ubuntu๋ฅผ ๋งŒ๋“ค์–ด, Tomcat8์„ ์ด์šฉํ•˜์—ฌ JDBC ์˜ Statement ์‹ค์Šต์„ ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. JDBC ์‹ค์Šต์„ ์›ํ• ํ•˜๊ฒŒ ํ•˜์‹œ๋ ค๋ฉด MySQL๊ณผ Launchpad(test-db)๊ฐ€ ํ•„์š”ํ•˜๋ฏ€๋กœ ์•„๋ž˜์˜ ๊ธ€์„ ์ฐธ๊ณ ํ•˜..

yunaaaas.tistory.com

 

[Docker/MySQL] JDBC - PreparedStatement ์‹ค์Šต

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” ์ €๋ฒˆ ๊ธ€์— ์ด์–ด PreparedStatement ์‹ค์Šต์„ ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. Docker์™€ MySQL์„ ์ด์šฉํ•œ JDBC ๊ธฐ๋ณธ ์‹ค์Šต ๊ณผ์ •์€ ์•„๋ž˜ ๊ธ€์—์„œ ์„ค๋ช…ํ•˜์—ฌ ์ƒ๋žตํ•˜์˜€์Šต๋‹ˆ๋‹ค. JDBC ๊ธฐ๋ณธ ์‚ฌ์šฉ๋ฒ•๊ณผ Statement ์‹ค

yunaaaas.tistory.com

โœ”๏ธubuntu ์‹คํ–‰ 

docker exec -it ubuntu_new1 bash

โœ”๏ธtomcat8 ์„œ๋ฒ„ ์—ด๊ธฐ

cd /usr/share/java
service tomcat8 start

โœ”๏ธ tomcat8 ํ™ˆ์œผ๋กœ์ด๋™ํ•˜๊ธฐ

cd /var/lib/tomcat8/webapps/ROOT

 

โœ”๏ธ ํŠธ๋žœ์žญ์…˜ ์‹ค์Šต ๊ด€๋ จ JDBC jsp ์ž‘์„ฑํ•˜๊ธฐ

nano sample.jsp
<%@ page import = "java.sql.*" %>
<% Class.forName("com.mysql.jdbc.Driver");
String dbUrl = "jdbc:mysql://172.17.0.2:3306/employees";
Connection con = DriverManager.getConnection(dbUrl, "root","yunas");
try {
        con.setAutoCommit(false);
        Statement stmt = con.createStatement();
        Savepoint savepoint1 = con.setSavepoint("Savepoint1");
        String SQL = "insert into employees values (1000,'2020-01-03','yuna1','kim1','F','2020-01-03');";
        stmt.executeUpdate(SQL);
        con.commit();
        String SQL2 = "inserted into employees values (2000,'2020-01-04','yuna2','kim2','F','2020-01-04');";
        stmt.executeUpdate(SQL2);
        con.commit();

        out.println("record adding Success!");
} catch (SQLException e) { out.println("ERROR!");
        out.println(e);
        con.rollback();
} finally {
        con.setAutoCommit(true);

}

โœ”๏ธ์‹คํ–‰๊ฒฐ๊ณผ

localhost:8080/sample.jsp

SQL2 ๋ฌธ์žฅ์—์„œ SQL syntax์— ์˜ํ•ด Error ๊ฐ€ ๋ฐœ์ƒํ•œ ๊ฒƒ์„ ์•Œ์ˆ˜์žˆ๋‹ค!

 

์ €๋ฒˆ JDBC ์‹ค์Šต์—์„œ ๋งŒ๋“ค์—ˆ๋˜ statement.jsp ๋ฅผ ํ†ตํ•ด ์ฒซ๋ฒˆ์งธ SQL๋ฌธ์€ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ insert๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค!

๋ฐ˜์‘ํ˜•