프로젝트를 정리하던 중에 발견한 오류를 발견했다. 

커넥션 풀 관련 오류와 함께 아래의 오류 발생 

org.apache.commons.dbcp.SQLNestedException

1. 첫번째 해결책으로 아래의 사이트에서 connectionpool에 대해 알아보았지만, 배포되지 않은 개별 프로젝트이기때문에  동시접속자가 많지는 않다. 원인이 다른 곳에 있음이 분명했다. 

출처: JDBC 사용 - 커넥션 풀 :: 개발자의 공부 이야기 (tistory.com)

 

JDBC 사용 - 커넥션 풀

DriverManagerDataSource 사용시 문제점 설정 // jdbc 설정하는 xml 인위적인 오류 실행 - jdbc 연결을 인위적으로 많이 발생시킴 ### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could..

rlawls1991.tistory.com

2. oracle 에서 DB드라이버를 다시 연결 시켰다. 그럼에도 해결되지 않았다.

3. 마지막으로 알게된 원인은 root.xml파일을 검토하라는 조언이었다. 

출처: Clean Code that Works. :: org.apache.commons.dbcp.SQLNestedException (tistory.com)

 

org.apache.commons.dbcp.SQLNestedException

DBCP Pool 테스트중 아래와 같은 에러가 발생~!!!! org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class ' ' for connect URL 'null' 이것은 Tomcat의 server.xml 파일이 잘못되었기..

starplatina.tistory.com

SpringFramework를 사용하고 있었기 때문에 DB연결의 경우 아래와 같은 식으로 연동하고 있었다. 

연결되어있는 info파일로 들어가보니,

 dburl설정이 잘못되어있었다. 발표시 테스트한다고 호스트 번호를 잠시 바꾸어주었던것이 원인이었다 ㅠㅠ 

해결! 

첫번째!  태그와 속성 지정 

1. 먼저, 어떤 태그와 속성을 사용할지를 선언한다. 

<?xml version="1.0" encoding="UTF-8"?>
<!-- 
xml
 h4322   h1, h2
 paaak   p align=""
 1. DTD : 상단에 어떤 태그(태그명과 속성)를 사용할지를 선언
 2. 위에 선언 후, DTD에 맞는 태그를 활용 수 있다.
 -->
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:c="http://www.springframework.org/schema/c"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:websocket="http://www.springframework.org/schema/websocket"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
  	http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd     
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util.xsd
    http://www.springframework.org/schema/websocket
	http://www.springframework.org/schema/websocket/spring-websocket.xsd" >

두번째! <context:component-scan> 등록 

2. 컨테이너에 사용할 객체를 등록한다. 컨테이너에서 Bean형식으로 읽기 위해서 설정해주는 

<context:component-scan>태그와 

<context:include-filter> 태그가 있으며, <context:include-filter>는 component-scan의 하위태그이다.

	<context:component-scan base-package="springweb">
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Dao"/>
	</context:component-scan>

여기서 잠깐! 

그렇다면!
<Context:component-scan>이란?
Bean이 될 수 있는 모든 Component를 자동으로 찾아 Bean container에 등록해준다. 
패키지명 이후의 하위 패키지를 검색해 
@Component 어노테이션을 포함하는 모든 클래스를 
Bean으로 자동 등록해준다. 
그렇다면 , annotaion이란?
프로그램 코드의 일부가 아닌 프로그램에 관한 데이터를 제공하는 것이다.  
코드에 정보를 추가하는 정형화된 방법이다.
즉, 
프로그램에 추가적인 정보를 제공해주는 메타데이터(data를 위한 data) 라고 볼 수 있다. 
@Component 어노테이션이 달린 클래스를 Bean으로 자동 등록/생성해준다는의미다.


그렇다면!annotaion에는 어떤 종류가있을까?



@controller : 스프링MVC의 컨트롤러 객체임을 명시하는 어노테이션
@RequestMapping : 특정 URI에 매칭되는 클래스나 메소드임을 명시하는 어노테이션
@ModelAttribute: view에서 전달해주는 파라미터를 클래스의 멤버변수로 binding 해주는 어노테이션

 

세번째! DB 연동

1. connection pool 처리
		1) 드라이버, 주소, 계정, 비번
	 -->	
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
		destroy-method="close">
		<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>	
		<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"/>	
		<property name="username" value="scott"/>	
		<property name="password" value="tiger"/>	
	</bean>

Spring의 좋은 점 중 하나는 connection full을 일일이 지정해주지 않아도된다는 것에 있다.

property 속성값으로 드라이버, 주소,계정,비번을 입력하고 

bean의 아이디와 class값을 설정해주면, 이후부터는 connection 을 따로 써주지 않아도 자동연동처리된다. 

참 간단하죵? 

 

네번째! mybatis연동 

2. Mybatis와 연동
		1) dbcp연결
		2) java/resource/mybatis.Spring.xml
			로 공통 연결 처리..
	 --> 
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"/>
		 
		<property name="configLocation"
			value="classpath:resource/mybatis.Spring.xml" />
		
	</bean>

세번째에 선언해놓은, dataSource라는 DB 연동 소스를 참고하여 mybatis.srping.xml에 값을 매핑시켜준다. 

여기서 Mybatis란?

Mybatis는 자바object 와 SQL사이의 자동 매핑 기능을 지원하는 ORM(Object relational Mapping)프레임워크이다.

JDBC의 모든 기능은 mybatis가 대부분 제공하기때문에  JDBC코드를 작성하지 않아도되며,

복잡한 JDBC코드를 걷어낼 수 있기 때문에 깔끔한 소스코드를 유지할 수 있다.

수동적인 파라미터 설정과 쿼리 결과에 대한 맵핑 구문을 제거할 수 있다. 

다섯번째! DAO의 인터페이스 위치를 설정 

 

  1. 	3. dao 인터페이스 페이지 위치를 설정..
    		인터페이스 ==> 인터페이스의 상속받은
    			 실제객체생성(mybatis 프레임웍 XXXmapper.xml)
    		name="basePackage" value="dao경로1, dao경로2,..."
    	 -->
    	<bean 
    		class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    		<property name="basePackage" value="springweb.a05_mvc.a03_dao"/>
    	</bean>
    인터페이스의 상속받은 실제 객체를 생성하는 것은 mybatisframework에서 진행한다. xxxmapper.xml

      name= "basePackage"로 지정한다면 vlaue="dao경로1" "dao경로2" 의 식으로 작성한다. 

+ Recent posts