缘起

昨晚导入一个ssm框架的项目,访问请求的时候总跟我提无法创建数据库连接的问题,这台新电脑刚刚装系统不久,赶了个新潮,装了个mysql8

错误信息

Type Exception Report

Message Request processing failed; nested exception is org.springframework.transaction.CannotCreateTransactionException: Could not open JDBC Connection for transaction; nested exception is java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.transaction.CannotCreateTransactionException: Could not open JDBC Connection for transaction; nested exception is java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:981)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:860)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:845)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:121)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
.......

问题定位

image.png

那就是连不上数据库咯
网上找了好多解决方案,不怎么奏效
后来终于知道是MySQL8的一些特性的问题
首先更改下面这玩意


再去更改连接驱动器的版本

        
            mysql
            mysql-connector-java
            8.0.11
            runtime
        

改动连接url的配置为

useSSL=false

再跑!

image.png

回去看问题,划拉划拉到最后发现了bug

image.png

时区设置的不对
改动如下:

serverTimezone=UTC

再跑

image.png

果然,成功运行
附上完整的源配置

    
    
        
        
        
        
        
    

另外就是在pom里面改动数据库驱动的版本,这个上面有

image.png

文章来源于互联网:Java+SSM框架+mysql8连接数据库的一些坑

发表评论