Mybatis 다중 Datasource 설정
Mybatis 다중 Datasource 설정
통계치를 다른 서버 DB에 적재 할 일이 생겨 추가적으로 작업.
(소스상에 나와있는 수치는 예제이므로 절대치는 아닙니다.)
database.properties
## 이전접속 DB
jdbc.main.driver=xxxx
jdbc.main.url=xxxx
jdbc.main.user=xxx
jdbc.main.password=xxxx
## 신규접속 DB
jdbc.main.driver.sts=yyyyy
jdbc.main.url.sts=yyyyyyy
jdbc.main.user.sts=yyyyy
jdbc.main.password.sts=yyyyyy
context-datasource.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:property-placeholder location="classpath:config/properties/database.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.main.driver}"/>
<property name="url" value="${jdbc.main.url}" />
<property name="username" value="${jdbc.main.user}"/>
<property name="password" value="${jdbc.main.password}"/>
<property name="validationQuery" value="select 1"/>
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<property name="testWhileIdle" value="true" />
<property name="maxWait" value="1000" />
<property name="maxActive" value="200" />
<property name="maxIdle" value="10" />
<property name="minIdle" value="1" />
</bean>
<bean id="dataSourceSts" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.main.driver.sts}"/>
<property name="url" value="${jdbc.main.url.sts}" />
<property name="username" value="${jdbc.main.user.sts}"/>
<property name="password" value="${jdbc.main.password.sts}"/>
<property name="validationQuery" value="select 1"/>
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<property name="testWhileIdle" value="true" />
<property name="maxWait" value="1000" />
<property name="maxActive" value="200" />
<property name="maxIdle" value="10" />
<property name="minIdle" value="1" />
</bean>
</beans>
context-mybatis.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 이전DB -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:config/mybatis/mybatis-config.xml" />
<property name="mapperLocations">
<list>
<value>classpath:sqlmap/**/*.xml</value>
</list>
</property>
</bean>
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="sqlSessionFactory"/>
</bean>
<!-- 신규DB -->
<bean id="sqlSessionFactorySts" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSourceSts" />
<property name="configLocation" value="classpath:config/mybatis/mybatis-config.xml" />
<property name="mapperLocations">
<list>
<value>classpath:sqlmap/sts/*.xml</value>
</list>
</property>
</bean>
<bean id="sqlSessionTemplateSts" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="sqlSessionFactorySts"/>
</bean>
</beans>
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="cacheEnabled" value="false" />
<setting name="useGeneratedKeys" value="true" />
<setting name="callSettersOnNulls" value="true"/>
<setting name="mapUnderscoreToCamelCase" value="true" />
<setting name="defaultExecutorType" value="REUSE" />
<setting name="localCacheScope" value="STATEMENT" />
</settings>
</configuration>
context-transaction.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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
<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="select*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="Exception" read-only="true"/>
<tx:method name="insert*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="Exception" read-only="false"/>
<tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="Exception" read-only="false"/>
<tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="Exception" read-only="false"/>
<tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="Exception" read-only="false"/>
<tx:method name="*" rollback-for="Exception"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="requiredTx" expression="execution(* platform.stownbus.business..impl.*Impl.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="requiredTx" />
</aop:config>
<bean id="txManagerSts" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSourceSts"/>
</bean>
<tx:advice id="txAdviceSts" transaction-manager="txManagerSts">
<tx:attributes>
<tx:method name="select*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="Exception" read-only="true"/>
<tx:method name="insert*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="Exception" read-only="false"/>
<tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="Exception" read-only="false"/>
<tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="Exception" read-only="false"/>
<tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="Exception" read-only="false"/>
<tx:method name="*" rollback-for="Exception"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="requiredTxSts" expression="execution(* platform.stownbus.api..impl.*Impl.*(..))"/>
<aop:advisor advice-ref="txAdviceSts" pointcut-ref="requiredTxSts" />
</aop:config>
</beans>
Dao
// 기존 Dao
@Repository("oldDao")
public class OldDao extends AbstractDao {
..
..
}
// 신규Dao
@Repository("newDao")
public class NewDao extends AbstractStsDao {
..
..
}
// 이전 AbstractDao
public class AbstractDao {
@Autowired
@Resource(name="sqlSessionTemplate")
private SqlSessionTemplate sqlSessionTemplate;
..
..
}
// 신규 AbstractStsDao
public class AbstractStsDao {
@Autowired
@Resource(name="sqlSessionTemplateSts")
private SqlSessionTemplate sqlSessionTemplate;
..
..
}
## 적용방법은 다양하나 현 시스템 기준으로 작성 하였습니다.