Last time, I wrote JNDI with Spring Framework.
This time, I add another DB on same Spring Framework with JNDI environment.
#Tomcat setting in Server
- server.xml
1. Add new resource.
<GlobalNamingResources>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
-->
<Resource auth="Container"
defaultAutoCommit="false"
driverClassName="com.mysql.jdbc.Driver"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
initialSize="10" maxActive="100" maxWait="10000" minIdle="10"
name="jdbc/wolfspringDS"
username="spring"
password="test"
type="javax.sql.DataSource" url="jdbc:mysql://xxx.xxx.xxx.xxx:11000/springbook"
validationQuery="SELECT 1 FROM DUAL"/>
<Resource auth="Container"
defaultAutoCommit="false"
driverClassName="com.mysql.jdbc.Driver"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
initialSize="10" maxActive="100" maxWait="10000" minIdle="10"
name="jdbc/afterrainDS"
username="afterrain"
password="test"
type="javax.sql.DataSource" url="jdbc:mysql://xxx.xxx.xxx.xxx:3306/afterrain"
validationQuery="SELECT 1 FROM DUAL"/>
<Resource auth="Container" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" name="UserDatabase" pathname="conf/tomcat-users.xml" type="org.apache.catalina.UserDatabase"/>
</GlobalNamingResources>
2. Add new Context
<Context docBase="AfterRainJ" path="/openapp" reloadable="true" source="org.eclipse.jst.jee.server:AfterRainJ">
<ResourceLink global="jdbc/afterrainDS" name="jdbc/afterrainDS" type="javax.sql.DataSource" />
<ResourceLink global="jdbc/wolfspringDS" name="jdbc/wolfspringDS" type="javax.sql.DataSource" />
</Context>
#root-context.xml
1. add new bean
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:/comp/env/jdbc/afterrainDS" />
</bean>
<bean id="dataSourceWolf" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:/comp/env/jdbc/wolfspringDS" />
</bean>
#Test JSP
<sql:query var="rs" dataSource="jdbc/afterrainDS">
select code, num, title, contents, gen_usr_id, use_flag from mboard
</sql:query>
<sql:query var="rswolf" dataSource="jdbc/wolfspringDS">
select code, num, title, contents, gen_usr_id, use_flag from mboard
</sql:query>
<h2>AWS Query Results</h2>
<c:forEach var="row" items="${rs.rows}">
Code ${row.code}
num ${row.num}
title ${row.title}
contents ${row.contents}
Gen_usr_no ${row.gen_usr_id}
Use_flag ${row.use_flag}
<br/>
</c:forEach>
<h2>WolfSpring Query Results</h2>
<c:forEach var="row" items="${rswolf.rows}">
Code ${row.code}
num ${row.num}
title ${row.title}
contents ${row.contents}
Gen_usr_no ${row.gen_usr_id}
Use_flag ${row.use_flag}
<br/>
</c:forEach>