tomcat 7 JNDI configuration 및 spring 연동 - c3p0, dbcp
매번 헷갈려서 문서 뒤적거리니 아예 매뉴얼화!
tomcat 의 JNDI 로 사용
server.xml 의 <GlobalNamingResources> 사이에 다음 내용 추가
Oracle 일 경우 driverClass와 url은 변경이 필요함
driverClassName
=
"oracle.jdbc.OracleDriver"
url
=
"jdbc:oracle:thin:@192.168.10.100:1521:SID"
c3p0
<GlobalNamingResources> <Resource name="jdbc/DatabaseName" auth="Container" description="DB Connection" driverClass="com.mysql.jdbc.Driver" maxPoolSize="50" minPoolSize="10" acquireIncrement="10" user="springdemo" password="demo1234" factory="org.apache.naming.factory.BeanFactory" type="com.mchange.v2.c3p0.ComboPooledDataSource" jdbcUrl="jdbc:mysql://localhost:3306/springdemo?autoReconnect=true&useUnicode=true&characterEncoding=utf8" /> </GlobalNamingResources>
dbcp
<GlobalNamingResources> <Resource name="jdbc/DatabaseName" auth="Container" description="DB Connection" driverClassName="com.mysql.jdbc.Driver" maxActive="50" initialSize="10" username="springdemo" password="demo1234" factory="org.apache.naming.factory.BeanFactory" type="com.mchange.v2.c3p0.ComboPooledDataSource" url="jdbc:mysql://localhost:3306/springdemo?autoReconnect=true&useUnicode=true&characterEncoding=utf8" /> </GlobalNamingResources>
context.xml 의 <Context> 사이에 내용 추가
<Context> <ResourceLink name="jdbc/DatabaseName" global="jdbc/DatabaseName" type="javax.sql.DataSource"/> </Context>
- tomcat 설정 끝
spring JNDI 로 사용
spring 의 context 설정에 다음 내용 추가
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <jee:jndi-lookup id="dbDataSource" jndi-name="jdbc/DatabaseName" expected-type="javax.sql.DataSource" /> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" p:dataSource-ref="dbDataSource" p:mapperLocations="classpath:/com/mycom/springdemo/mapper/*Mapper.xml" p:configLocation="classpath:/com/mycom/springdemo/mybatis-config.xml" /> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dbDataSource" /> </beans>
xml 상단에 다음 namespace 와 schemaLocation 이 추가되어야 jee tag를 인식함
<xmlns:jee="http://www.springframework.org/schema/jee" xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd">
- tomcat 재구동
See Also
Ref
- http://stackoverflow.com/questions/9183321/how-to-use-jndi-datasource-provided-by-tomcat-in-spring
- http://stackoverflow.com/questions/15064260/tomcat-jndi-configuration-best-practice
- http://commons.apache.org/proper/commons-dbcp/configuration.html