log4j
给出的异常信息有下面几句:
Caused by: org.hibernate.exception.GenericJDBCException: Unable to acquire JDBC Connection
at ...
Caused by: java.sql.SQLException: Connections could not be acquired from the underlying database!
at ...
Caused by: com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source.
at ...
多半是关于数据库的配置有问题
刚开始网上查了很多资料还是百思不得其解,从 pom 中把log4j
,slf4j-api
,slf4j-log4j12
等依赖去掉(从log4j
切换成了默认日志),才看到下面这个异常信息,这才明白是url
里需要加上时区参数。
四月 16, 2020 8:36:09 下午 com.mchange.v2.log.MLog <clinit>
信息: MLog clients using java 1.4+ standard logging.
...
java.sql.SQLException: The server time zone value '�й���ʱ��' is unrecognized or represents more than one time zone.
关于数据库配置的几个建议:
- 用
mysql-connector-java-8.x.x-bin.jar
的驱动com.mysql.cj.jdbc.Driver
连接MySQL 8
; - URL 最好带上
characterEncoding
和serverTimezone
,还可以指明是否进行SSL连接useSSL=false
:(XML文件 记得用&
来转义&
)
/// xml 文件
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test?useSSL=false&characterEncoding=utf8&serverTimezone=UTC"/>
/// properties 文件
jdbc.url=jdbc:mysql://localhost:3306/test?useSSL=false&characterEncoding=utf8&serverTimezone=UTC