그라냐's blog

[SpringFramework] Transaction 설정 방법 본문

Java & SpringFramework

[SpringFramework] Transaction 설정 방법

그라냐 2019. 6. 10. 17:19

- 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>

Service의 메서드명이 insert, update, delete로 시작하는 Service를 실행할 때 Exception이 나면 롤백하게 설정

그외 나머지는 read-only="true" 속성으로 select만 가능하게 설정

 

 

 

- pom.xml

	<dependency>
		<groupId>com.oracle</groupId>
		<artifactId>ojdbc6</artifactId>
		<version>11.2.0.3.0</version>
	</dependency>
	<dependency>
		<groupId>org.aspectj</groupId>
		<artifactId>aspectjrt</artifactId>
		<version>${org.aspectj-version}</version>
	</dependency>
	<dependency>
		<groupId>org.aspectj</groupId>
		<artifactId>aspectjweaver</artifactId>
		<version>${org.aspectj-version}</version>
	</dependency>
	<dependency>
		<groupId>cglib</groupId>
		<artifactId>cglib-nodep</artifactId>
		<version>3.1</version>
	</dependency>
	<dependency>
		<groupId>org.mybatis</groupId>
		<artifactId>mybatis</artifactId>
		<version>3.4.1</version>
	</dependency>
	<dependency>
		<groupId>org.mybatis</groupId>
		<version>1.3.0</version>
	</dependency>

jdbc, mybatis, aspectj, cglib 등 추가

 

 

 

0 Comments
댓글쓰기 폼