Presentation is loading. Please wait.

Presentation is loading. Please wait.

Transaction ㅇ Transaction 을 직접 구현하려면, 까다로운 설계와 복잡한 코드를 필요로 하고 수정에 공수가 많이 들게 된다. ㅇ 스프링에서 제공하는 Transaction Manager 를 사용하여 쉽고 간편하게 어려운 트랜잭션을 해결할 수 있다. xml.

Similar presentations


Presentation on theme: "Transaction ㅇ Transaction 을 직접 구현하려면, 까다로운 설계와 복잡한 코드를 필요로 하고 수정에 공수가 많이 들게 된다. ㅇ 스프링에서 제공하는 Transaction Manager 를 사용하여 쉽고 간편하게 어려운 트랜잭션을 해결할 수 있다. xml."— Presentation transcript:

1 Transaction ㅇ Transaction 을 직접 구현하려면, 까다로운 설계와 복잡한 코드를 필요로 하고 수정에 공수가 많이 들게 된다. ㅇ 스프링에서 제공하는 Transaction Manager 를 사용하여 쉽고 간편하게 어려운 트랜잭션을 해결할 수 있다. xml code

2 Transaction ㅇ XML 트랜잭션 설정 <bean id=“transactionManager” class=“org.springframework.jdbc.datasource.DataSourceTransactionManager”> <aop:pointcut id="daoMethod" expression="execution(* com.spring.transaction.exam.model..*(..))" />

3 Transaction ㅇ Annotation 트랜잭션 설정 - XML - 자바 클래스 @Transactional 애노테이션 추가

4 Transaction ㅇ 트랜잭션 속성 - name : 트랜잭션이 적용될 메소드 이름, * 사용 가능 - propagation : 트랜잭션 전파 규칙 ㆍ REQUIRED( 기본값 ) : 현재 진행 중인 트랜잭션이 있다면 해당 트랜잭션 사용 없다면 새로운 트랜잭션을 생성 ㆍ MANDATORY : 진행 중인 트랜잭션이 없다면 예외 발생 ㆍ REQUIRES_NEW : 항상 새로운 트랜잭션 시작 기존 트랜잭션이 존재하면 기존 트랜잭션 중지 새로운 트랜잭션이 종료된 후 기존 트랜잭션 계속 진행 ㆍ SUPPORTS : 기존 트랜잭션이 존재할 경우 트랜잭션을 사용 진행 중인 트랜잭션이 존재하지 않더라도 메소드 정상 동작 ㆍ NOT_SUPPORTED : 메소드가 실행되는 동안 트랜잭션 일시 중지 메소드 실행이 종료된 후에 트랜잭션 계속 진행 ㆍ NEVER : 메소드가 트랜잭션을 필요로 하지 않음 진행 중인 트랜잭션이 존재하면 예외 발생 ㆍ NESTED : 기존 트랜잭션이 존재하면 중첩된 트랜잭션에서 메소드 실행 기존 트랜잭션이 존재하지 않으면 REQUIRED 와 동일하게 동작

5 Transaction ㅇ 트랜잭션 속성 - isolation : 트랜잭션 격리 수준 ㆍ DEFAULT : 기본 설정 사용 ㆍ READ_UNCOMMITTED : 다른 트랜잭션에서 커밋하지 않은 데이터 읽을 수 있음 ㆍ READ_COMMITTED : 다른 트랜잭션에 의해 커밋된 데이터 읽을 수 있음 ㆍ REPEATABLE_READ : 처음 읽어온 데이터와 두번째 읽어온 데이터가 동일한 값을 갖음 ㆍ SERIALIZABLE : 동일한 데이터에 대해서 동시에 두개 이상 트랜잭션이 수행 될 수 없음 - rollback-for : 트랜잭션 롤백을 적용할 예외 타입 지정 기본값은 RuntimeException - no-rollback-for : 롤백하지 않을 예외 타입 설정 - timeout : 트랜잭션의 타임아웃 시간을 초 단위로 설정

6 config.xml (1 / 3) Transaction – XML <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource" />

7 config.xml (2 / 3) Transaction – XML <aop:pointcut id="daoMethod" expression="execution(* edu.seowon.transaction.xml.controller..*(..))" />

8 config.xml (3 / 3) Transaction – XML

9 Main.java Transaction – XML public class Main { public static void main(String[] args) { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext( "edu/seowon/transaction/xml/config.xml"); Object obj = ctx.getBean("listController"); ((ListController)obj).select(); ((ListController)obj).insert("x", false); ((ListController)obj).insert("z", true); }

10 ListController.java Transaction – XML public class ListController { @Autowired private BoardDao boardDao; public void select() { List > list = boardDao.selectBoard(); for(int i = 0; i < list.size(); i++) { System.out.println(" 아이디 : " + list.get(i).get("ID")); System.out.println(" 비밀번호 : " + list.get(i).get("PW")); System.out.println(" 성명 : " + list.get(i).get("NAME")); } public void insert(String id, boolean flag) { boardDao.insertBoard(id); if(flag) throw new RuntimeException(); System.out.println("id : " + id + " 입력 "); }

11 BoardDao.java Transaction – XML public class BoardDao { @Autowired private JdbcTemplate jdbcTemplate; public List > selectBoard() { String sql = "SELECT * FROM MEMBER_M1"; List > list = jdbcTemplate.queryForList(sql); return list; } public void insertBoard(String id) { String sql = "INSERT INTO MEMBER_M1 (ID, PW, NAME, PHONE, EMAIL) " + " VALUES (?, ?, ?, ?, ?)"; jdbcTemplate.update(sql, id, "1", " 김재욱 ", "010", "seo@gmail.com"); }

12 config.xml (1 / 2) Transaction – Annotation <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource" />

13 config.xml (2 / 2) Transaction – Annotation

14 Main.java Transaction – Annotation public class Main { public static void main(String[] args) { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext( "edu/seowon/transaction/anno/config.xml"); Object obj = ctx.getBean("listController"); ((ListController)obj).select(); ((ListController)obj).insert("e", false); ((ListController)obj).insert("f", true); }

15 ListController.java Transaction – Annotation @Transactional public class ListController { @Autowired private BoardDao boardDao; public void select() { List > list = boardDao.selectBoard(); for(int i = 0; i < list.size(); i++) { System.out.println(" 아이디 : " + list.get(i).get("ID")); System.out.println(" 비밀번호 : " + list.get(i).get("PW")); System.out.println(" 성명 : " + list.get(i).get("NAME")); } public void insert(String id, boolean flag) { boardDao.insertBoard(id); if(flag) throw new RuntimeException(); System.out.println("id : " + id + " 입력 "); }


Download ppt "Transaction ㅇ Transaction 을 직접 구현하려면, 까다로운 설계와 복잡한 코드를 필요로 하고 수정에 공수가 많이 들게 된다. ㅇ 스프링에서 제공하는 Transaction Manager 를 사용하여 쉽고 간편하게 어려운 트랜잭션을 해결할 수 있다. xml."

Similar presentations


Ads by Google