읽은 책 정리/코드로 배우는 스프링 웹 프로젝트

[Spring] 스프링과 oracle Database연동

포포015 2020. 12. 13. 22:04

-오라클 설치  11g버전 (버전은 중요하지않음)

-sql Developer 설치(Sql plus보다 편한 gui방식 툴)

-계정생성 / 8080포트 변경(tomcat의 기본포트가 8080이기때문)

-프로젝트 jdbc 연결 (오라클은 pom.xml에 등록해도 저작권?문제때문에 자동으로 다운이 안되기때문에

 lib 폴더를 만들고 ojdbc8.jar를 넣어서 java balid path로 등록해주기!

 

- JDBC 테스트코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package org.zeorck.persistence;
 
import static org.junit.Assert.fail;
 
import java.sql.Connection;
import java.sql.DriverManager;
 
import org.junit.Test;
 
import lombok.extern.log4j.Log4j;
 
@Log4j
public class JDBCTests {
static {
    try {
        Class.forName("oracle.jdbc.driver.OracleDriver");
    }catch (Exception e) {
        e.printStackTrace();
        }
    }
 
@Test
public void testConnection() {
    try(Connection con = DriverManager.getConnection(
            "jdbc:oracle:thin:@localhost:1521:XE",
            "web02"
            "1234")){
        log.info(con);
    }catch(Exception e) {
        fail(e.getMessage());
        }
    }
}
 
cs

 커넥션 풀 설정

여러명의 사용자를 동시에 처리해야하는 웹 애플리케이션의 경우 DB연결을 이용할때는

'커넥션 풀'을 이용하므로, 아예 스프링에 커넥션풀을 등록해 사용하는게 좋음

java에서는 DataSource라는 인터페이스를 통해서 커넥션 풀을 사용.

DataSource를 통해 매번 데이터베이스와 연결하는 방식이 아닌,

미리 연결을 맺어주고 반환하는 구조를 이용해 성능향상을 꾀함

 

아래 예제는 HikariCp 사용 pom.xml에 추가

1
2
3
4
5
6
7
<!-- https://mvnrepository.com/artifact/com.zaxxer/HikariCP -->
<dependency>
    <groupId>com.zaxxer</groupId>
    <artifactId>HikariCP</artifactId>
    <version>2.7.4</version>
</dependency>
 
cs

root-context.xml 설정

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    
    <!-- Root Context: defines shared resources visible to all other web components -->
        
        
        <bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
        <property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:xe"></property>
        <property name="username" value="web02"></property>
        <property name="password" value="1234"></property>
        </bean>
        
        <!--HikariCP Configration-->
        <bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
        <constructor-arg ref="hikariConfig"/>
        </bean>
        
    <context:component-scan base-package="org.zeorck.sample"></context:component-scan>
        
</beans>
 
cs

DataSourceTests 테스트

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package org.zeorck.persistence;
 
import static org.junit.Assert.fail;
 
import java.sql.Connection;
 
import javax.sql.DataSource;
 
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 
import lombok.Setter;
import lombok.extern.log4j.Log4j;
 
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/root-context.xml")
@RunWith(SpringJUnit4ClassRunner.class)
@Log4j
public class DataSourceTests {
 
 
    @Setter(onMethod_ = {@Autowired})
    private DataSource dataSource;
    
    @Test
    public void testConnection() {
        try(Connection con = dataSource.getConnection()){
            log.info(con);
        }catch (Exception e) {
            fail(e.getMessage());
        }
    }
}
 
cs