스프링18_트랜잭션

charl hi·2022년 2월 4일
0

Spring

목록 보기
19/25

설정

pom.xml

		<!-- transaction -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>

servlet-context.xml

	<!-- trasaction -->
	<beans:bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
			<beans:property name="dataSource" ref="dbcpSource"/>
	</beans:bean>

->

->

	<!-- trasaction tx -->
	<!-- @Transactional 을 사용할 수 있게 함 -->
	<tx:annotation-driven/>
	<beans:bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
			<beans:property name="dataSource" ref="dbcpSource"/>
	</beans:bean>



spring23prj

MyController

@Controller
@RequestMapping("my")
public class MyController {
	
	@Autowired
	private MyService service;
	
	
	@GetMapping("test")
	@ResponseBody
	public String test() {
		
		service.test();
		return "test called~!";
	}
}


MyService

@Service
@Transactional
//@Transactional(isolation = )
//@Transactional(rollbackFor = Exception.class)
//@Transactional(noRollbackFor = NullPointerException.class)
//@Transactional(readOnly=true)
//오직 select쿼리만 날리겠다?
public class MyService {
	
	@Autowired
	private MyDao dao;

	public void test() {
		try {
			dao.testA();
			dao.testB();
		} catch (Exception e) {
			throw new RuntimeException("내가 생각못한 checked 예외 발생");
		}
	}

}


MyDao

@Repository
public class MyDao {

	@Autowired
	private SqlSession session;
	
	public void testA() {
		session.insert("test.a");
	}
	
	public void testB() {
		session.insert("test.b");
	}

}


member-mapper.xml

<mapper namespace="test">
 	
 	<insert id="a">
 		INSERT INTO TEST_TB
 		VALUES('AAA')
 	</insert>

 	<insert id="b">
 		INSERT INTO TEST_TB
 		VALUES('BBBbbbbbbbbbbbb')
 	</insert>
 	
</mapper>


옵션

rollbackFor

  • @Transactional(rollbackFor = Exception.class)

noRollbackFor

  • @Transactional(noRollbackFor = NullPointerException.class)

readOnly

  • @Transactional(readOnly=true)
  • 오직 select쿼리만 날리겠다?

isolation

  • @Transactional(isolation = )

0개의 댓글