缘起
昨晚导入一个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)
.......
问题定位
那就是连不上数据库咯
网上找了好多解决方案,不怎么奏效
后来终于知道是MySQL8的一些特性的问题
首先更改下面这玩意
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
再去更改连接驱动器的版本
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
<scope>runtime</scope>
</dependency>
改动连接url的配置为
useSSL=false
再跑!
回去看问题,划拉划拉到最后发现了bug
时区设置的不对
改动如下:
serverTimezone=UTC
再跑
果然,成功运行
附上完整的源配置
<!-- 配置数据源 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://localhost:3306/gitchat_ssm_demo_db?useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false&serverTimezone=UTC"/>
<!-- 改为你的地址即可 -->
<property name="username" value="root"/>
<property name="password" value="admin"/>
</bean>
另外就是在pom里面改动数据库驱动的版本,这个上面有