mysql使用数据库连接池c3p0进行连接时出现的问题

时间:2018-12-17
本文章向大家介绍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 。总之满足它的要求即可。