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