2013년 12월 27일 금요일

Spring Framework에서 데이터소스(datasource) 얻는 방법 1. DriverManagerDataSource

Spring Framework에서 데이터소스(datasource) 얻는 방법

1. DriverManagerDataSource 클래스를 이용

- 소스에서 

     DriverManagerDataSource dataSource = new DriverManagerDataSource();
     dataSource.setDriverClassName(“oracle.jdbc.OracleDriver”);
     dataSource.setUrl(“jdbc:oracle:thin:@localhost:1521:onj”);
     dataSource.setUsername(“scott”);
     dataSource.setPassword(“tiger”);


- 설정파일에서 

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>

<context:property-placeholder location="jdbc.properties"/>


2. DBCP를 이용

    common-dbcp.jar, commons-pool.jar 파일이 클래스패스에 필요합니다.

   <bean id="dataSource" 
        class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="${jdbc.driverClassName}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>

<context:property-placeholder location="jdbc.properties"/>


3. JNDI 이용

   [context.xml]

   <?xml version=”1.0″ encoding=”UTF-8″?>

   <Context>

   <Resource name=”jdbc/onjdb” 
auth=”Container” 
type=”javax.sql.DataSource” 
driverClassName=”oracle.jdbc.OracleDriver” 
url=”jdbc:oracle:thin:@localhost:1521:onj” 
username=”scott” 
password=”tiger” 
maxActive=”20″ 
maxIdle=”10″ 
maxWait=”-1″
/>

   </Context>


   <bean id=”dataSource” class=”org.springframework.jndi.JndiObjectFactoryBean”>
  <property name=”jndiName” value=”java.comp/env/jdbc/onjdb”/>
   </bran>


댓글 없음:

댓글 쓰기