Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.GenericJDBCException: Could not open connection ;Hibernate 连接mysql-8.0数据库时无法打开连接
在使用mysql 8.0 时 ,Hibernate 连接出现Could not open connection 错误,查了很长时间无法解决,原因是因为新版mysql连接时url需要设置好时差。
异常:
org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.GenericJDBCException: Could not open connection
解决方案:
<property name="url" value="jdbc:mysql://localhost:3306/数据库名称?serverTimezone=GMT%2B8&autoReconnect=true&useUnicode=true&characterEncoding=UTF-8"></property>
设置时区
//北京时间东八区
serverTimezone=GMT%2B8
同时数据库驱动类用mysql-connector-java-8.0.11.jar
驱动类名是 com.mysql.cj.jdbc.Driver