xml 파일에서 네임스페이스 tx 추가
xml파일에 코드 추가
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<context:component-scan
base-package="com.springbook.biz" />
<!-- AOP 어노테이션 자동 인식 설정 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<context:property-placeholder location="classpath:config/database.properties"/>
<!-- DataSource 설정 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- Transaction 설정 / tx매니저가 감시 -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- Transaction Advice 설정 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<!-- get으로 시작되는 메서드 read only는 감시x -->
<tx:method name="get*" read-only="true"/>
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<aop:config>
<!-- aop:pointcut - 어떤것을 감시대상으로 삼을 것인지 -->
<aop:pointcut id="txPointcut" expression="execution(* com.springbook.biz..*(..))" />
<aop:advisor pointcut-ref="txPointcut" advice-ref="txAdvice" />
</aop:config>
</beans>
BoardServiceImpl
insert메서드 한번 더 실행하도록 코드 추가
BoardDAOSpring
쿼리문 수정
물음표와 대응하도록 getSeq도 추가
BoardServiceClient
seq는 primary key인데 36을 넣으면 insertBoard(vo)에서 첫번째는 성공하지만 두번째에선 트랜잭션 오류일 것이다.
그런데 메서드 자체에 transaction이 걸려있으므로 둘다 모두 취소해버릴 것이다. (전체 rollback 처리)
console 결과