Framework/iBATIS

ibatis에서 dataSource 여러개 사용하기 (자바 프레임워크)

FreeEnd 2010. 8. 18. 10:52
반응형

1. 님은 properties를 사용 안하셨는데 만약 사용한다면  아래와 같이 두가지 접속정보를 설정합니다.

driver=oracle.jdbc.driver.OracleDriver

jdbc.url.db1=jdbc:oracle:thin:@//<ip>:<port>/<instance>
username.db1=user1
password.db1=password1

jdbc.url.db2=jdbc:oracle:thin:@//<ip>:<port>/<instance>
username.db2=user2
password.db2=password2

 

2. sqlmapconfig.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”>

<!– Always ensure to use the correct XML header as above! –>
<sqlMapConfig>
<!– The properties (name=value) in the file specified here can be used placeholders in this config
file (e.g. ${driver}. The file is usually relative to the classpath and is optional. –>
<properties resource=”database.properties”/>

<!– These settings control SqlMap configuration details, primarily to do with transaction
management. They are all optional (see the Developer Guide for more). –>
<settings
cacheModelsEnabled=”true”
enhancementEnabled=”true”
lazyLoadingEnabled=”true”
maxRequests=”1024″
maxSessions=”128″
maxTransactions=”32″
useStatementNamespaces=”false”
/>

<!– Type aliases allow you to use a shorter name for long fully qualified class names. –>

<!– Configure a datasource to use with this SQL Map using SimpleDataSource.
Notice the use of the properties from the above resource –>
<transactionManager type=”JDBC”>
<dataSource type=”DBCP”>

<!-- 요기가 서로 다르겠죠? -->
<property name=”driverClassName” value=”${driver}”/>
<property name=”url” value=”${jdbc.url.db1}”/>
<property name=”username” value=”${username.db1}”/>
<property name=”password” value=”${password.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=”testWhileIdle” value=”true”/>–>
</dataSource>
</transactionManager>

<!– Identify all SQL Map XML files to be loaded by this SQL map. Notice the paths
are relative to the classpath. For now, we only have one –>
<sqlMap resource=”maps/table1.xml”/>

</sqlMapConfig>

 

두번째,

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

<!– Always ensure to use the correct XML header as above! –>
<sqlMapConfig>
<!– The properties (name=value) in the file specified here can be used placeholders in this config
file (e.g. ${driver}. The file is usually relative to the classpath and is optional. –>
<properties resource=”database.properties”/>

<!– These settings control SqlMap configuration details, primarily to do with transaction
management. They are all optional (see the Developer Guide for more). –>
<settings
cacheModelsEnabled=”true”
enhancementEnabled=”true”
lazyLoadingEnabled=”true”
maxRequests=”1024″
maxSessions=”128″
maxTransactions=”32″
useStatementNamespaces=”false”
/>

<!– Type aliases allow you to use a shorter name for long fully qualified class names. –>

<!– Configure a datasource to use with this SQL Map using SimpleDataSource.
Notice the use of the properties from the above resource –>
<transactionManager type=”JDBC”>
<dataSource type=”DBCP”>

<!-- 요기가 서로 다르겠죠? -->
<property name=”driverClassName” value=”${driver}”/>
<property name=”url” value=”${jdbc.url.db2}”/>
<property name=”username” value=”${username.db2}”/>
<property name=”password” value=”${password.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=”testWhileIdle” value=”true”/>–>
</dataSource>
</transactionManager>

<!– Identify all SQL Map XML files to be loaded by this SQL map. Notice the paths
are relative to the classpath. For now, we only have one –>
<sqlMap resource=”maps/table2.xml”/>

</sqlMapConfig>

 

 

 

3. 중요한것은 여기 입니다.SqlMapClient를 두 개 생성하여 필요한 DB를 선택하여 질의 합니다.

private static SqlMapClient db1SqlMap = null;
private static SqlMapClient db2SpSqlMap = null;

// Static initializer
static {
try {
String resource = “db1.xml”;
Reader reader = Resources.getResourceAsReader(resource);
db1SqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);

resource = “db2.xml”;
reader = Resources.getResourceAsReader(resource);
db2SpSqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);

.................

 

 

도움이 되셨나요?

 

반응형