IT_Programming/Dev Libs & Framework

[펌] mysql + ibatis 사용시, mysql connector 에러 해결 방법

JJun ™ 2013. 2. 27. 02:00


출처: http://cafe.daum.net/ITVillage/Mu3b/3?docid=1DpW4Mu3b320120604110130






참고 사이트: http://blog.daum.net/bada0217/97

                  http://mysqlpreacher.com/wordpress/2010/06/mysql-jdbc-connector-autoreconnecttrue/


[DEBUG][TP-Processor3][2010-04-26 13:45:48,825] login.LoginMultiAction (MultiActionController.java:530) - Binding request parameters onto MultiActionController command

[DEBUG][TP-Processor3][2010-04-26 13:45:48,825] login.LoginMultiAction (MultiActionController.java:530) - Binding request parameters onto MultiActionController command
[ERROR][TP-Processor3][2010-04-26 13:45:48,829] login.LoginMultiAction ( ?:?) - org.springframework.dao.DataAccessResourceFailureException: SqlMapClient operation; SQL [];
--- The error occurred in net/infobank/talkbox/login/da/maps/sqlMap-login.xml.
--- The error occurred while applying a parameter map.
--- Check the login.selectIdAndPassword-InlineParameterMap.
--- Check the statement (query failed).
--- Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was15223 milliseconds ago.The last packet sent successfully to the server was 15223 milliseconds ago, which is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred in net/infobank/talkbox/login/da/maps/sqlMap-login.xml.
--- The error occurred while applying a parameter map.
--- Check the login.selectIdAndPassword-InlineParameterMap.
--- Check the statement (query failed).
--- Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was15223 milliseconds ago.The last packet sent successfully to the server was 15223 milliseconds ago, which is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.
[ERROR][TP-Processor3][2010-04-26 13:45:48,829] login.LoginMultiAction ( ?:?) - org.springframework.dao.DataAccessResourceFailureException: SqlMapClient operation; SQL [];
--- The error occurred in net/infobank/talkbox/login/da/maps/sqlMap-login.xml.
--- The error occurred while applying a parameter map.
--- Check the login.selectIdAndPassword-InlineParameterMap.
--- Check the statement (query failed).
--- Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was15223 milliseconds ago.The last packet sent successfully to the server was 15223 milliseconds ago, which is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred in net/infobank/talkbox/login/da/maps/sqlMap-login.xml.
--- The error occurred while applying a parameter map.
--- Check the login.selectIdAndPassword-InlineParameterMap.
--- Check the statement (query failed).
--- Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was15223 milliseconds ago.The last packet sent successfully to the server was 15223 milliseconds ago, which is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.


iBatis, DBCP, MySQL 을 사용하는 웹어플리케이션 구동 중, 일정 시간 동안 컨넥션을 사용하지 않다가 사용을 하면 컨넥션이 이미 종료 되었다고 나타나는 문제가 발생했다.
Communications link failure due to underlying exception

뭐 사실 .. 어느 정도 사용을 하는 어플리케이션이라면 이런 문제는 발생하지 않을거다.
MySQL 은 기본적으로 컨넥션을 통해서 8시간 동안 request가 오지 않는다면, 강제적으로 컨넥션을 닫아버린다.
( 이는 물론 MySQL 의 설정을 변경함으로써 값을 바꿀 수는 있다. 하지만, 좋은 방법이라고 생각되지는 않는다. )

다음과 같은 iBatis 설정에서도 당연히 동일한 문제가 발생했다.
<dataSource type="DBCP">
<property name="JDBC.Driver" value="com.mysql.jdbc.Driver" />
<property name="JDBC.ConnectionURL" value="jdbc:mysql://데이터베이스접속url?autoReconnect=true" />
<property name="JDBC.Username" value="id" />
<property name="JDBC.Password" value="password" />
<property name="JDBC.DefaultAutoCommit" value="true" />
<property name="Pool.MaximumActiveConnections" value="5" />
<property name="Pool.MaximumIdleConnections" value="3" />
<property name="Pool.MaximumWait" value="60000" />
</dataSource>

다음과 같이 설정값을 변경함으로써 해당 문제를 해결 할 수 있다.
<dataSource type="DBCP">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://데이터베이스접속url?autoReconnect=true" />
<property name="username" value="id" />
<property name="password" value="password" />
<property name="defaultAutoCommit" value="true" />
<property name="maximumActiveConnections" value="5" />
<property name="maximumIdleConnections" value="3" />
<property name="maximumWait" value="60000" />
<!-- validationQuery:유효 검사용 쿼리( 1개 이상의 row를 반환하는 쿼리를 넣어주면 된다. ) -->
<property name="validationQuery" value="select 1"/>
<!-- testWhileIdle:컨넥션이 놀고 있을때 -_-; validationQuery 를 이용해서 유효성 검사를 할지 여부. -->
<property name="testWhileIdle" value="true"/>
<!-- timeBetweenEvictionRunsMillis:해당 밀리초마다 validationQuery 를 이용하여 유효성 검사 진행 -->
<property name="timeBetweenEvictionRunsMillis" value="7200000"/>
</dataSource>

이렇게 설정하면 컨넥션을 풀에서 가지고 올때도, validationQuery 를 통해서 유효성 검사를 진행하게 되는데,
뭐 퍼포먼스가 문제 될거라고 생각된다면 -_-aa .. testOnBorrow( default : true ) 값을 false 로 추가해주면 된다.

설정값의 테스트는 mysql 의 show processlist; 쿼리를 이용하여 정상적으로 구동되는지 알 수 있다.
show processlist 를 수행하면 현재 mysql 에서 수행되고 있는 프로세스의 리스트와 상태를 알 수 있는데,
웹어플리케이션을 구동하고, 컨넥션이 맺어지도록 한 후에 해당 프로세스가 command 필드가 sleep 상태에서 Time 필드 값이 timeBetweenEvictionRunsMillis/1000 보다 커지지 않는다면, 정상적으로 설정된 것이다. ( 뭐 물론 약간의 차이는 날 수 도 있겠으며, 해당 테스트 동안 컨넥션이 사용된다면 잘못 나타날 수 도 있다. ) 약 1,000 정도의 값으로 셋팅을 하고 테스트 후 적절한 값을 넣어서 사용하면 되겠다.

그 외에도 이상의 문제를 해결 할 수 있는 설정은 몇가지가 더 있다.
http://commons.apache.org/dbcp/configuration.html 를 참고 하면 된다.

property 3개 추가하여 디비가 끊기는 것을 방지할 수 있었다.

간단해결방법 //출처 : http://blog.daum.net/iammingoo/5

에러로그:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:
...., which is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.


wait_timeout 은 connection 증가로 추천하지 않고..autoReconnection 을 추가
설정은 jdbc 커넥션 설정 파일에서 url 에 "autoReconnection=true" 넣어줌.

* url=jdbc:mysql://ip:port/dbname?useUnicode=true&characterEncoding=utf8&autoReconnect=true
* validationQuery="select 1"

그리고 이 외에도 validationQuery를 넣어주는 데 이유는 한번 커넥션이 끊어진다음 재시도를 하는 경우 문제가 있는 처음 한번의 시도는 에러가 나게 된다. 이때 넣어주는게 validationQuery로 모든 사용자쿼리를 실행 전에 한번 이 쿼리를 실행하게 된다.(그러므로 DB로서는 추가적인 부하가 됨) 쿼리가 한번 더 실행되는 것인 만큼 가장 간단한 쿼리여야 한다