웹앱프로젝트/서버

[error] java.net.SocketTimeoutException: connect timed out 에러 DBCP validation query 로 특정시간마다 쿼리 실행으로 해결

Minah Park 2023. 4. 24. 14:41
반응형

에러내용:

31-Mar-2023 12:22:20.414 SEVERE [ajp-nio-0.0.0.0-8009-exec-255] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [appServlet] in context with path [] threw exception [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error updating database.  Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLNonTransientConnectionException: Could not connect to address=(host=118.67.129.190)(port=3306)(type=master) : Socket fail to connect to host:118.67.129.190, port:3306. connect timed out
### The error may exist in file [/usr/local/tomcat9/webapps/ROOT/WEB-INF/classes/mybatis/AccessHistoryMapper.xml]
### The error may involve com.app.hanbat.accessHistory.AccessHistoryDao.insertAccessHistory
### The error occurred while executing an update
### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLNonTransientConnectionException: Could not connect to address=(host=118.67.129.190)(port=3306)(type=master) : Socket fail to connect to host:118.67.129.190, port:3306. connect timed out] with root cause
        java.net.SocketTimeoutException: connect timed out

 

 

해결>>>>>

commons-dbcp2 라이브러리를 추가하고

mybatis-context 에 validationQuery 를 value="select 1" 로 추가해준다.

기존에는 bean 이

<bean id="dataSource" class="org.springframework.jdbc.datasource.DrivermanagerDataSource"> 로 설정되어있었다. 하지만 spring 에서 제공하는 것이 아닌 apache 에서 제공하는 dbcp2를 이용해서 추가하였다.

build.gradle

compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.13'
implementation group: 'org.apache.commons', name: 'commons-dbcp2', version: '2.9.0'

 

mybatis-context.xml

<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="net.sf.log4jdbc.sql.jdbcapi.DriverSpy" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />

    <!-- 특정 시간마다 validationQuery를 실행 셋팅 시작 -->
    <property name="validationQuery" value="select 1"/>
</bean>

 

 

 

반응형