본문 바로가기

프로그래밍

[spring]jdbc연결/커넥션 풀 설정

728x90
반응형

-jdbc연결

https://mvnrepository.com/artifact/com.oracle.database.jdbc/ojdbc8/12.2.0.1

ojdbc8 maven 의존성 추가하기

테스트코드 작성

@Log4j
public class JDBCTests {
   static {
      try {
    	  //forName() : 물리적인 클래스 파일명을 인자로 넣어주면 이에 해당하는 클래스를 반환
         Class.forName("oracle.jdbc.driver.OracleDriver");
      } catch (ClassNotFoundException e) {
         e.printStackTrace();
      }
   }
   
   @Test
   public void testConnectioin() {
      try(Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@도메인:1521:xe", "JSPUSER", "JSPUSER")) {
         log.info(conn);
         
      }
      catch(Exception e) {
         fail(e.getMessage());
      }
   }
}

 

-커넥션풀

필요한 maven : hikariCP,spring-test

spring-test의 버전은  <version>${org.springframework-version}</version> 로 바꿔주도록 한다.

https://mvnrepository.com/artifact/org.springframework/spring-test/5.3.9

https://mvnrepository.com/artifact/com.zaxxer/HikariCP/2.7.8

<bean class="com.zaxxer.hikari.HikariDataSource" id="dataSource">
		<constructor-arg>
			<bean class="com.zaxxer.hikari.HikariConfig">
				<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
				<property name="jdbcUrl" value="jdbc:oracle:thin:@도메인:1521:xe"/>
				<property name="username" value="JSPUSER" />
                <property name="password" value="JSPUSER" /> 
			</bean>
		</constructor-arg>
		</bean>

-마이바티스

필요한 maven : mybatis,mybatis-spring

https://mvnrepository.com/artifact/org.mybatis/mybatis/3.4.5

https://mvnrepository.com/artifact/org.mybatis/mybatis-spring/1.3.2

테스트코드 작성

package com.kimjaeeun.persistence;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
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;

//빈 테스트하는 파일
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/root-context.xml")
@Log4j
public class DataSourceTests {
   @Autowired @Setter
   private DataSource dataSource;
   @Autowired @Setter
   private SqlSessionFactory sqlSessionFactory;
   
   @Test
   public void testExist() {
      assertNotNull(dataSource);
      log.info(dataSource);
   }
   //스프링에 빈으로 등록된 DateSource를 이용해서 Connection을 제대로 처리할 수 있는지 확인
   @Test
   public void testConnection() {
      try(Connection conn = dataSource.getConnection()) {
         log.info(conn);
      }catch (SQLException e) {
         fail(e.getMessage());
      }
   }
   
   @Test
   public void testMybatis(){
	   SqlSession session = sqlSessionFactory.openSession();
	   Connection conn= session.getConnection();
	   log.info(conn);
   }
}

 

728x90
반응형