Mybatis 다중 Datasource 설정

Mybatis 다중 Datasource 설정

통계치를 다른 서버 DB에 적재 할 일이 생겨 추가적으로 작업.

(소스상에 나와있는 수치는 예제이므로 절대치는 아닙니다.)


database.properties

  1. ## 이전접속 DB
  2. jdbc.main.driver=xxxx
  3. jdbc.main.url=xxxx
  4. jdbc.main.user=xxx
  5. jdbc.main.password=xxxx
  6. ## 신규접속 DB
  7. jdbc.main.driver.sts=yyyyy
  8. jdbc.main.url.sts=yyyyyyy
  9. jdbc.main.user.sts=yyyyy
  10. jdbc.main.password.sts=yyyyyy


context-datasource.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:context="http://www.springframework.org/schema/context"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  5. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
  6. <context:property-placeholder location="classpath:config/properties/database.properties"/>
  7. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  8. <property name="driverClassName" value="${jdbc.main.driver}"/>
  9. <property name="url" value="${jdbc.main.url}" />
  10. <property name="username" value="${jdbc.main.user}"/>
  11. <property name="password" value="${jdbc.main.password}"/>
  12. <property name="validationQuery" value="select 1"/>
  13. <property name="timeBetweenEvictionRunsMillis" value="60000" />
  14. <property name="testWhileIdle" value="true" />
  15. <property name="maxWait" value="1000" />
  16. <property name="maxActive" value="200" />
  17. <property name="maxIdle" value="10" />
  18. <property name="minIdle" value="1" />
  19. </bean>
  20. <bean id="dataSourceSts" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  21. <property name="driverClassName" value="${jdbc.main.driver.sts}"/>
  22. <property name="url" value="${jdbc.main.url.sts}" />
  23. <property name="username" value="${jdbc.main.user.sts}"/>
  24. <property name="password" value="${jdbc.main.password.sts}"/>
  25. <property name="validationQuery" value="select 1"/>
  26. <property name="timeBetweenEvictionRunsMillis" value="60000" />
  27. <property name="testWhileIdle" value="true" />
  28. <property name="maxWait" value="1000" />
  29. <property name="maxActive" value="200" />
  30. <property name="maxIdle" value="10" />
  31. <property name="minIdle" value="1" />
  32. </bean>
  33. </beans>


context-mybatis.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5. <!-- 이전DB -->
  6. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  7. <property name="dataSource" ref="dataSource" />
  8. <property name="configLocation" value="classpath:config/mybatis/mybatis-config.xml" />
  9. <property name="mapperLocations">
  10. <list>
  11. <value>classpath:sqlmap/**/*.xml</value>
  12. </list>
  13. </property>
  14. </bean>
  15. <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
  16. <constructor-arg ref="sqlSessionFactory"/>
  17. </bean>
  18. <!-- 신규DB -->
  19. <bean id="sqlSessionFactorySts" class="org.mybatis.spring.SqlSessionFactoryBean">
  20. <property name="dataSource" ref="dataSourceSts" />
  21. <property name="configLocation" value="classpath:config/mybatis/mybatis-config.xml" />
  22. <property name="mapperLocations">
  23. <list>
  24. <value>classpath:sqlmap/sts/*.xml</value>
  25. </list>
  26. </property>
  27. </bean>
  28. <bean id="sqlSessionTemplateSts" class="org.mybatis.spring.SqlSessionTemplate">
  29. <constructor-arg ref="sqlSessionFactorySts"/>
  30. </bean>
  31. </beans>


mybatis-config.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
  3. <configuration>
  4. <settings>
  5. <setting name="cacheEnabled" value="false" />
  6. <setting name="useGeneratedKeys" value="true" />
  7. <setting name="callSettersOnNulls" value="true"/>
  8. <setting name="mapUnderscoreToCamelCase" value="true" />
  9. <setting name="defaultExecutorType" value="REUSE" />
  10. <setting name="localCacheScope" value="STATEMENT" />
  11. </settings>
  12. </configuration>


context-transaction.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:tx="http://www.springframework.org/schema/tx"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  6. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
  7. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
  8. <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  9. <property name="dataSource" ref="dataSource"/>
  10. </bean>
  11. <tx:advice id="txAdvice" transaction-manager="txManager">
  12. <tx:attributes>
  13. <tx:method name="select*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="Exception" read-only="true"/>
  14. <tx:method name="insert*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="Exception" read-only="false"/>
  15. <tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="Exception" read-only="false"/>
  16. <tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="Exception" read-only="false"/>
  17. <tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="Exception" read-only="false"/>
  18. <tx:method name="*" rollback-for="Exception"/>
  19. </tx:attributes>
  20. </tx:advice>
  21. <aop:config>
  22. <aop:pointcut id="requiredTx" expression="execution(* platform.stownbus.business..impl.*Impl.*(..))"/>
  23. <aop:advisor advice-ref="txAdvice" pointcut-ref="requiredTx" />
  24. </aop:config>
  25. <bean id="txManagerSts" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  26. <property name="dataSource" ref="dataSourceSts"/>
  27. </bean>
  28. <tx:advice id="txAdviceSts" transaction-manager="txManagerSts">
  29. <tx:attributes>
  30. <tx:method name="select*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="Exception" read-only="true"/>
  31. <tx:method name="insert*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="Exception" read-only="false"/>
  32. <tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="Exception" read-only="false"/>
  33. <tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="Exception" read-only="false"/>
  34. <tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="Exception" read-only="false"/>
  35. <tx:method name="*" rollback-for="Exception"/>
  36. </tx:attributes>
  37. </tx:advice>
  38. <aop:config>
  39. <aop:pointcut id="requiredTxSts" expression="execution(* platform.stownbus.api..impl.*Impl.*(..))"/>
  40. <aop:advisor advice-ref="txAdviceSts" pointcut-ref="requiredTxSts" />
  41. </aop:config>
  42. </beans>


Dao

  1. // 기존 Dao
  2. @Repository("oldDao")
  3. public class OldDao extends AbstractDao {
  4. ..
  5. ..
  6. }
  7. // 신규Dao
  8. @Repository("newDao")
  9. public class NewDao extends AbstractStsDao {
  10. ..
  11. ..
  12. }

  1. // 이전 AbstractDao
  2. public class AbstractDao {
  3. @Autowired
  4. @Resource(name="sqlSessionTemplate")
  5. private SqlSessionTemplate sqlSessionTemplate;
  6. ..
  7. ..
  8. }
  9. // 신규 AbstractStsDao
  10. public class AbstractStsDao {
  11. @Autowired
  12. @Resource(name="sqlSessionTemplateSts")
  13. private SqlSessionTemplate sqlSessionTemplate;
  14. ..
  15. ..
  16. }


## 적용방법은 다양하나 현 시스템 기준으로 작성 하였습니다.