druid連線池bug(轉)
MySQL Server 5.7.21 + mysql-connector-java 5.1.38 + druid 1.0.8
一.配置步驟:
1.pom.xml 引入相關jar包:
<!-- mysql資料庫的驅動包 start -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<!-- druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.18</version>
</dependency>
2.spring.xml 資料來源配置:
<!-- 資料來源,阿里Druid連線池 -->
<bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource" init-method="init" destroy-method="close">
<!-- 資料庫資訊 -->
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/manangement?useUnicode=true&characterEncoding=utf8&useSSL=false" />
<property name="username" value="root" />
<property name="password" value="root" />
<!-- 配置初始化大小、最小、最大 -->
<property name="initialSize" value="5" />
<property name="minIdle" value="1" />
<property name="maxActive" value="300" />
<!-- 配置獲取連線等待超時的時間 -->
<property name="maxWait" value="60000" />
<!-- 配置間隔多久才進行一次檢測,檢測需要關閉的空閒連線,單位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一個連線在池中最小生存的時間,單位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="validationQuery" value="SELECT 1" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<!-- PSCache,並且指定每個連線上PSCache的大小(mysql資料庫建議關閉) -->
<property name="poolPreparedStatements" value="false" />
<property name="maxPoolPreparedStatementPerConnectionSize" value="-1" />
<!-- 配置監控統計攔截的filters,去掉後監控介面sql無法統計 -->
<property name="filters" value="stat" />
</bean>
3.Druid監控配置這裡沒有寫,資料庫相關資訊也可以寫在config.properties資原始檔裡面;
二.注意事項:
1.MySQL Server 和 mysql-connector-java 要對應 ,如果是使用了 MySQL Server 8+和 mysql-connector-java 8+ 版本;
DataSource配置需要修改為:
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/manangement?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=時區標誌" />
2. 報錯:
a. java.sql.SQLException: Unknown system variable 'tx_isolation'
原因是mysql-connector-java版本過低,升級一下版本;
b.com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
Caused by: java.lang.NullPointerException
at com.mysql.jdbc.ConnectionImpl.getServerCharset(ConnectionImpl.java:3299)
原因是MySQL Server 和 mysql-connector-java 版本不一致,使Druid驅動載入不能連線資料庫;改成相應版本就行;如MySQL Server 5.7.21 + mysql-connector-java 5.1.38 + druid 1.0.8
c.Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
原因是預設SSL連線,禁用時在url上加上useSSL=false,如:
jdbc:mysql://localhost:3306/manangement?useUnicode=true&characterEncoding=utf8&useSSL=false
---------------------
作者:黃粱一夢-努力努力
來源:CSDN
原文:https://blog.csdn.net/qq_26369317/article/details/80681221
版權宣告:本文為博主原創文章,轉載請附上博文連結!