상세 컨텐츠

본문 제목

aop xml transaction 설정

Framework/Spring

by H_Develop 2023. 9. 14. 20:41

본문

문제의 발단은 한 페이지에서 동영상 파일을 업로드 할 때,

솔루션에서 제공하는 퍼센트 숫자가 계속 바뀌어지기에 

setInterval() 함수로 컨트롤러를 계속 요청해서 문제가 되었다.

 

 

출처:https://granya.tistory.com/43

- web.xml 

<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>
		/WEB-INF/spring/root-context.xml
		, /WEB-INF/spring/context-datasource.xml
		, /WEB-INF/spring/context-transaction.xml
	</param-value>
</context-param>
위와 같은 설정파일 추가 및 수정예정

- servlet-context.xml

<context:component-scan base-package="com.x.y">
	<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
	<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
	<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository" />
</context:component-scan>
context:component-scan에 Controller만 스캔함

- root-context.xml

<context:component-scan base-package="com.x.y">
	<context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
	<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
	<context:include-filter type="annotation" expression="org.springframework.stereotype.Component" />
	<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
context:component-scan에 Controller를 제외한 Service등 스캔함

- context-datasource.xml

<bean id="dataSource"  class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
	p:driverClassName="oracle.jdbc.driver.OracleDriver"
	p:url="jdbc:oracle:thin:@192.168.0.111:1521:test"
	p:username="test"
	p:password="test#123" />
jdbc 설정

- context-transaction.xml

<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource" />

<tx:advice id="txAdvice" transaction-manager="txManager">
	<tx:attributes>
		<tx:method name="insert*" timeout="600" rollback-for="Exception" />
		<tx:method name="update*" timeout="600" rollback-for="Exception" />
		<tx:method name="delete*" timeout="600" rollback-for="Exception" />
		<tx:method name="*" read-only="true" />
	</tx:attributes>
</tx:advice>

<aop:config>
	<aop:pointcut id="txPointcut" expression="execution(* com.x.y.*.service..impl.*Impl.*(..))" />
	<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" />
</aop:config>

위 코드를 보고, transactionManager를 설정해준다

 

 

	<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"/>
	</bean>
	  
	<tx:advice id="txAdvice" transaction-manager="txManager">
	   <tx:attributes>
	       <tx:method name="*" rollback-for="Exception"/>
	   </tx:attributes>
	</tx:advice>
    
    <aop:config>
		<aop:pointcut id="txPointcut" expression="execution(* 경로.filename1..*Impl.*(..)) or
												  execution(* 경로.filename2..*Impl.*(..))"/>
		<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" />
	</aop:config>

이렇게 aop 설정을 마치면 모든 impl이 붙은 폴더 안의 파일들이 트렌젝션이 설정되고

Exception이 나면 롤백이 된다.

 

<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource" />

<tx:advice id="txAdvice" transaction-manager="txManager">
	<tx:attributes>
		<tx:method name="insert*" timeout="600" rollback-for="Exception" />
		<tx:method name="update*" timeout="600" rollback-for="Exception" />
		<tx:method name="delete*" timeout="600" rollback-for="Exception" />
		<tx:method name="*" read-only="true" />
	</tx:attributes>
</tx:advice>

<aop:config>
	<aop:pointcut id="txPointcut" expression="execution(* com.x.y.*.service..impl.*Impl.*(..))" />
	<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" />
</aop:config>

이렇게 하면, Service의 메서드명이 insert, update, delete로 시작하는 Service를 실행할 때 Exception이 나면 롤백하게 설정 그외 나머지는 read-only="true" 속성으로 select만 가능하게 설정된다.

 

 

 

 

 

 

 

 

 

 

 

관련글 더보기