Apache iBatis에서 다중 데이터 베이스 사용 방법

Apache iBatis에서 다중 데이터 베이스에 접속하는 방법이다.

1. database.properties

driver=oracle.jdbc.driver.OracleDriver
jdbc.url1=jdbc:oracle:thin:@mimuluserdb:1521:mimuluser
username1=mimuluser
password1=mimuluser
jdbc.url2=jdbc:oracle:thin:@pepsiuserdb:1521:pepsiuser
username2=pepsiuser
password2=pepsiuser

2. sqlmap1.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<properties resource="com/mimul/dwr/app/
resource/database.properties"/>
<settings
cacheModelsEnabled="true"
enhancementEnabled="true"
lazyLoadingEnabled="true"
maxRequests="40"
maxSessions="20"
maxTransactions="5"
useStatementNamespaces="false"
/>
<transactionManager type="JDBC">
<dataSource type="DBCP">
<property name="driverClassName" value="${driver}"/>
<property name="url" value="${jdbc.url1}"/>
<property name="username" value="${username1}"/>
<property name="password" value="${password1}"/>
<!-- OPTIONAL PROPERTIES BELOW -->
<property name="initialSize" value="5"/>
<property name="maxActive" value="30"/>
<property name="maxIdle" value="20"/>
<property name="maxWait" value="60000"/>
<property name="poolPreparedStatements" value="true"/>
<property name="validationQuery" value="select 0 from dual"/>
<property name="testOnBorrow" value="true"/>
<property name="maximumActiveConnections" value="10"/>
<property name="maximumIdleConnections" value="5"/>
<property name="maximumWait" value="60000"/>
<property name="logAbandoned" value="false"/>
<property name="removeAbandoned" value="false"/>
<property name="removeAbandonedTimeout" value="50000"/>
</dataSource>
</transactionManager>
<sqlMap resource="com/mimul/dwr/app/sql/Mimul.xml"/>
</sqlMapConfig>

3. sqlmap2.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<properties resource="com/mimul/dwr/app/resource/database.properties"/>
<settings
cacheModelsEnabled="true"
enhancementEnabled="true"
lazyLoadingEnabled="true"
maxRequests="40"
maxSessions="20"
maxTransactions="5"
useStatementNamespaces="false"
/>
<transactionManager type="JDBC">
<dataSource type="DBCP">
<property name="driverClassName" value="${driver}"/>
<property name="url" value="${jdbc.url2}"/>
<property name="username" value="${username2}"/>
<property name="password" value="${password2}"/>
<!-- OPTIONAL PROPERTIES BELOW -->
<property name="initialSize" value="5"/>
<property name="maxActive" value="30"/>
<property name="maxIdle" value="20"/>
<property name="maxWait" value="60000"/>
<property name="poolPreparedStatements" value="true"/>
<property name="validationQuery" value="select 0 from dual"/>
<property name="testOnBorrow" value="true"/>
<property name="maximumActiveConnections" value="10"/>
<property name="maximumIdleConnections" value="5"/>
<property name="maximumWait" value="60000"/>
<property name="logAbandoned" value="false"/>
<property name="removeAbandoned" value="false"/>
<property name="removeAbandonedTimeout" value="50000"/>
</dataSource>
</transactionManager>
<sqlMap resource="com/mimul/dwr/app/sql/Pepsi.xml"/>
</sqlMapConfig>

4. SqlCondig.java

import java.io.File;
import java.io.Reader;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.jaeminara.common.log.LogPool;
public class SqlConfig {
  private static SqlMapClient sqlMap1 = null;
  private static SqlMapClient sqlMap2 = null;
  private static SqlConfig instance_ = null;

  private SqlConfig() throws Exception {
    Reader reader = null;
    String resource = null;
    try {
      if (sqlMap == null) {
        resource = "sqlmap1.xml";
        reader = Resources.getResourceAsReader(resource);
        sqlMap1 = SqlMapClientBuilder.buildSqlMapClient(reader);
        resource = "sqlmap2.xml";
        reader = Resources.getResourceAsReader(resource);
        sqlMap2 = SqlMapClientBuilder.buildSqlMapClient(reader);
        reader.close();
      }
    } catch (Exception e) {
      System.out.println(e);
      throw e;
    } finally {
      if (reader != null)
      reader.close();
      reader = null;
      rsc = null;
    }
  }

  public static SqlConfig instance() {
    try {
      if (instance_ == null) {
        synchronized (SqlConfig.class) {
          if (instance_ == null)
            instance_ = new SqlConfig();
        }
      }
    } catch (Exception e) {
      System.out.println(e);
    }
    return instance_;
  }

  /**
  * Return SqlMapClient for SDP schema
  *
  * @return
  */
  public static SqlMapClient getSqlMap1Instance() {
    return sqlMap1;
  }

  /**
  * Return SqlMapClient for SDP schema
  *
  * @return
  */
  public static SqlMapClient getSqlMap2Instance() {
    return sqlMap2;
  }
}

* Reference
http://mimul.com/pebble/default/2008/02/24/1203779580000.html

답글 남기기

이메일은 공개되지 않습니다. 필수 입력창은 * 로 표시되어 있습니다.