섹션 5. 연결
25강 커넥션풀
25-1 c3p0 모듈의 CompoPooledDataSource
connection pool의 datasource는 예외처리 해줘야함
@Repository
public class MemberDao implements IMemberDao {
private String driver = "oracle.jdbc.driver.OracleDriver";
private String url = "jdbc:oracle:thin:@localhost:1521:xe";
private String userid = "scott";
private String userpw = "tiger";
private ComboPooledDataSource dataSource;
private JdbcTemplate template;
public MemberDao() {
dataSource = new ComboPooledDataSource(); //예외처리 필수
try {
dataSource.setDriverClass(driver);
dataSource.setJdbcUrl(url);
dataSource.setUser(userid);
dataSource.setPassword(userpw);
} catch (PropertyVetoException e) {
e.printStackTrace();
}
template = new JdbcTemplate();
template.setDataSource(dataSource);
}
...
}
25-2 스프링 설정파일을 이용한 DataSource 설정
스프링 설정파일
<beans:bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<beans:property name="driverClass" value="oracle.jdbc.driver.OracleDriver" />
<beans:property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:xe" />
<beans:property name="user" value="scott" />
<beans:property name="password" value="tiger" />
<beans:property name="maxPoolSize" value="200" />
<beans:property name="checkoutTimeout" value="60000" />
<beans:property name="maxIdleTime" value="1800" />
<beans:property name="idleConnectionTestPeriod" value="600" />
</beans:bean>
config 자바 파일 이용
@Configuration
public class DBConfig {
@Bean
public ComboPooledDataSource dataSource() throws PropertyVetoException {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass("oracle.jdbc.driver.OracleDriver");
dataSource.setJdbcUrl("jdbc:oracle:thin:@localhost:1521:xe");
dataSource.setUser("scott");
dataSource.setPassword("tiger");
dataSource.setMaxPoolSize(200);
dataSource.setCheckoutTimeout(60000);
dataSource.setMaxIdleTime(1800);
dataSource.setIdleConnectionTestPeriod(600);
return dataSource;
}
}
@Autowired
public MemberDao(CompoPooledDataSource dataSource){
this.template = new JdbcTemplate(dataSource);
}
--> 실무에서 많이 쓰는 방법