1. 程式人生 > >mysql使用資料庫連線池c3p0進行連線時出現的問題

mysql使用資料庫連線池c3p0進行連線時出現的問題

版本

c3p0jar包版本:c3p0-0.9.1.2.jar

jdbc連線包:mysql-connector-java-5.1.7-bin.jar

資料庫mysql版本:8.0.12


問題1:

十二月 17, 2018 11:46:05 上午 com.mchange.v2.c3p0.DriverManagerDataSource ensureDriverLoaded
警告: Could not load driverClass com.mysql.cj.jdbc.Driver
java.lang.ClassNotFoundException: com.mysql.cj.
jdbc.Driver at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) at java.lang.Class.forName0(Native Method) at java.
lang.Class.forName(Class.java:264) at com.mchange.v2.c3p0.DriverManagerDataSource.ensureDriverLoaded(DriverManagerDataSource.java:100) at ...
十二月 17, 2018 11:46:05 上午 com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask run
警告: com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@36977f83 --
Acquisition Attempt Failed!!! Clearing pending acquires. While trying to acquire a needed new resource, we failed to succeed more than the maximum number of allowed acquisition attempts (30). Last acquisition attempt exception: java.sql.SQLException: Unknown system variable 'tx_isolation'

解決方法:jdbc連線包:更新為mysql-connector-java-8.0.11.jar

上述問題解決,又出現了新的問題:

問題2:

WARN: 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.

解決方法:在資料庫url之後加上useSSL=false;

使用程式碼形式:

dataSource.setJdbcUrl("jdbc:mysql://localhost/bank?useSSL=false");

使用配置檔案的形式:

url=jdbc:mysql://localhost:3306/bank?useSSL=false

上述問題解決,又出現了新的問題:

問題3:

Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
	at sun.reflect.GeneratedConstructorAccessor6.newInstance(Unknown Source)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:59)
	at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:83)
	at com.mysql.cj.util.TimeUtil.getCanonicalTimezone(TimeUtil.java:128)
	at com.mysql.cj.protocol.a.NativeProtocol.configureTimezone(NativeProtocol.java:2201)
	at com.mysql.cj.protocol.a.NativeProtocol.initServerSession(NativeProtocol.java:2225)
	at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:1391)
	at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:993)
	at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:852)
	... 11 more

解決方法:在資料庫url之後再加上serverTimezone=UTC

使用程式碼形式:

dataSource.setJdbcUrl("jdbc:mysql://localhost/bank?useSSL=false&serverTimezone=UTC");

使用配置檔案:

url=jdbc:mysql://localhost:3306/bank?useSSL=false&serverTimezone=UTC

至此,全部解決。

總結

其實是資料庫版本太高,而資料庫連線的jar包版本太低導致的,更換更高版本的資料庫連線jar包即可,但是高版本的資料庫強制SSL的使用,即使不使用,也需要顯式的指出:useSSL=false ,而且需要指定時區:serverTimezone=UTC 。總之滿足它的要求即可。