2012-10-31

Java] Ibatis 다중링크 사용



출처 : http://mimul.com/pebble/default/2008/02/24/1203779580000.html



웹 어플리케이션에서 하나 이상의 데이터 베이스에 접근하여 어플리케이션이 실행되는 환경들이 많을 것입니다.
iBatis에서 어떻게 활용되는 지 사용방법을 공유합니다.
사용환경은 iBatis와 DBCP입니다.
  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. SqlConfig.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;
        }
    }

댓글 없음:

댓글 쓰기