세상에 나쁜 코드는 없다

오류 org.springframework.jdbc.CannotGetJdbcConnectionException 해결방법 ( 초보 웹개발자를 위한 스프링5 프로그래밍 입문) 본문

웹개발/백엔드

오류 org.springframework.jdbc.CannotGetJdbcConnectionException 해결방법 ( 초보 웹개발자를 위한 스프링5 프로그래밍 입문)

Beomseok Seo 2022. 3. 11. 10:41

오류 전문:

더보기

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet successfully received from the server was 130 milliseconds ago.  The last packet sent successfully to the server was 123 milliseconds ago.
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:990)
at com.mysql.jdbc.ExportControlled.transformSocketToSSLSocket(ExportControlled.java:203)
at com.mysql.jdbc.MysqlIO.negotiateSSLConnection(MysqlIO.java:4901)
at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1659)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1226)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2188)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2219)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2014)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:776)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:386)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:330)
at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:310)
at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:203)
at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:735)
at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:667)
at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:482)
at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:154)
at org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:118)
at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:107)
at org.apache.tomcat.jdbc.pool.DataSourceProxy.getConnection(DataSourceProxy.java:131)
at org.springframework.jdbc.datasource.DataSourceUtils.fetchConnection(DataSourceUtils.java:158)
at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:116)
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:79)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:371)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:452)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:462)
at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:473)
at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:480)
at spring.MemberDao.count(MemberDao.java:113)
at main.MainForMemberDao.selectAll(MainForMemberDao.java:30)
at main.MainForMemberDao.main(MainForMemberDao.java:21)
Caused by: javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)
at java.base/sun.security.ssl.HandshakeContext.<init>(HandshakeContext.java:172)
at java.base/sun.security.ssl.ClientHandshakeContext.<init>(ClientHandshakeContext.java:103)
at java.base/sun.security.ssl.TransportContext.kickstart(TransportContext.java:239)
at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:443)
at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:421)
at com.mysql.jdbc.ExportControlled.transformSocketToSSLSocket(ExportControlled.java:188)
... 36 more

Exception in thread "main" org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet successfully received from the server was 130 milliseconds ago.  The last packet sent successfully to the server was 123 milliseconds ago.
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:82)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:371)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:452)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:462)
at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:473)
at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:480)
at spring.MemberDao.count(MemberDao.java:113)
at main.MainForMemberDao.selectAll(MainForMemberDao.java:30)
at main.MainForMemberDao.main(MainForMemberDao.java:21)
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet successfully received from the server was 130 milliseconds ago.  The last packet sent successfully to the server was 123 milliseconds ago.
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:990)
at com.mysql.jdbc.ExportControlled.transformSocketToSSLSocket(ExportControlled.java:203)
at com.mysql.jdbc.MysqlIO.negotiateSSLConnection(MysqlIO.java:4901)
at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1659)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1226)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2188)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2219)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2014)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:776)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:386)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:330)
at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:310)
at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:203)
at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:735)
at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:667)
at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:482)
at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:154)
at org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:118)
at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:107)
at org.apache.tomcat.jdbc.pool.DataSourceProxy.getConnection(DataSourceProxy.java:131)
at org.springframework.jdbc.datasource.DataSourceUtils.fetchConnection(DataSourceUtils.java:158)
at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:116)
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:79)
... 8 more
Caused by: javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)
at java.base/sun.security.ssl.HandshakeContext.<init>(HandshakeContext.java:172)
at java.base/sun.security.ssl.ClientHandshakeContext.<init>(ClientHandshakeContext.java:103)
at java.base/sun.security.ssl.TransportContext.kickstart(TransportContext.java:239)
at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:443)
at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:421)
at com.mysql.jdbc.ExportControlled.transformSocketToSSLSocket(ExportControlled.java:188)
... 36 more

 

초보 웹 개발자를 위한 스프링5 프로그래밍 입문 책의 예제를 실습하다가 생긴 문제이다.

 

만약 MySQL 을 최신버전으로 설치하고 메이븐 프로젝트의 pom.xml 에 책의 예제를 그대로 입력했다면

mysql-connector-j 와 MySQL 이 호환이 되지 않아 생긴 문제일 것이다. 

pom.xml 에서 mysql-connector-java 의 버젼을 8.0.16으로 바꿔주고 다운로드가 다 되면 다시 빌드를 해보자. 

그럼 다음과 같은 오류가 뜰 것이다.

더보기

3 11, 2022 10:19:59 오전 org.apache.tomcat.jdbc.pool.ConnectionPool checkPoolConfiguration

경고: maxIdle is larger than maxActive, setting maxIdle to: 10

Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

3 11, 2022 10:20:00 오전 org.apache.tomcat.jdbc.pool.ConnectionPool init

심각: Unable to create initial connections of pool.

java.sql.SQLException: The server time zone value '���ѹα ǥؽ' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.

        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)

        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)

        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)

        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)

        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73)

        at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:76)

        at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:835)

        at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:455)

        at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)

        at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199)

        at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:310)

        at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:203)

        at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:735)

        at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:667)

        at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:482)

        at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:154)

        at org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:118)

        at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:107)

        at org.apache.tomcat.jdbc.pool.DataSourceProxy.getConnection(DataSourceProxy.java:131)

        at org.springframework.jdbc.datasource.DataSourceUtils.fetchConnection(DataSourceUtils.java:158)

        at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:116)

        at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:79)

        at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:371)

        at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:452)

        at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:462)

        at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:473)

        at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:480)

        at spring.MemberDao.count(MemberDao.java:113)

        at main.MainForMemberDao.selectAll(MainForMemberDao.java:30)

        at main.MainForMemberDao.main(MainForMemberDao.java:21)

Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value '���ѹα ǥؽ' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.

        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)

        at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)

        at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)

        at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)

        at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)

        at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:85)

        at com.mysql.cj.util.TimeUtil.getCanonicalTimezone(TimeUtil.java:132)

        at com.mysql.cj.protocol.a.NativeProtocol.configureTimezone(NativeProtocol.java:2243)

        at com.mysql.cj.protocol.a.NativeProtocol.initServerSession(NativeProtocol.java:2267)

        at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:1319)

        at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:966)

        at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:825)

        ... 23 more

 

Exception in thread "main" org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: The server time zone value '���ѹα ǥؽ' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.

        at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:82)

        at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:371)

        at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:452)

        at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:462)

        at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:473)

        at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:480)

        at spring.MemberDao.count(MemberDao.java:113)

        at main.MainForMemberDao.selectAll(MainForMemberDao.java:30)

        at main.MainForMemberDao.main(MainForMemberDao.java:21)

Caused by: java.sql.SQLException: The server time zone value '���ѹα ǥؽ' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.

        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)

        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)

        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)

        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)

        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73)

        at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:76)

        at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:835)

        at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:455)

        at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)

        at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199)

        at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:310)

        at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:203)

        at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:735)

        at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:667)

        at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:482)

        at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:154)

        at org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:118)

        at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:107)

        at org.apache.tomcat.jdbc.pool.DataSourceProxy.getConnection(DataSourceProxy.java:131)

        at org.springframework.jdbc.datasource.DataSourceUtils.fetchConnection(DataSourceUtils.java:158)

        at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:116)

        at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:79)

        ... 8 more

Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value '���ѹα ǥؽ' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.

        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)

        at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)

        at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)

        at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)

        at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)

        at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:85)

        at com.mysql.cj.util.TimeUtil.getCanonicalTimezone(TimeUtil.java:132)

        at com.mysql.cj.protocol.a.NativeProtocol.configureTimezone(NativeProtocol.java:2243)

        at com.mysql.cj.protocol.a.NativeProtocol.initServerSession(NativeProtocol.java:2267)

        at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:1319)

        at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:966)

        at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:825)

        ... 23 more

 

해당 오류는 spring 으로 jdbc 설정하는 파일에 있는 jdbc url에 serverTimezone 프로퍼티를 추가함으로 해결할 수 있다.

DataSource ds = new DataSource();
		ds.setDriverClassName("com.mysql.cj.jdbc.Driver");
		ds.setUrl("jdbc:mysql://localhost/spring5fs?characterEncoding=utf8&serverTimezone=UTC");