配置Spring数据源遇到的那些坑。。。

作为一名Sping初学者,今天第一次配置Spring数据源就遇到好几个坑人的地方,记录下来做个备忘。

1.

Mon Nov 27 21:42:01 CST 2017 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.
Mon Nov 27 21:42:02 CST 2017 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.
Mon Nov 27 21:42:02 CST 2017 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.
Mon Nov 27 21:42:02 CST 2017 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.

向数据库发送请求后一直都在报这个,其实这个问题早在学习jdbc时就遇到了,只需要在jdbc的URL后面加上  useUnicode=true&characterEncoding=utf-8&useSSL=false 即可;

2.

Loading class `com.mysql.jdbc.Driver‘. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver‘. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

原来的jdbc驱动是 com.mysql.jdbc.Driver , 改为  com.mysql.cj.jdbc.Driver 即可;

3.

 1 27-Nov-2017 21:58:49.667 警告 [C3P0PooledConnectionPoolManager[identityToken->1hgefi99r1yeux661are7zk|12876430]-HelperThread-#0] com.mchange.v2.resourcepool.BasicResourcePool. com[email protected]74bb9e1f -- 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 (10). Last acquisition attempt exception:
 2  java.sql.SQLException: 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.
 3     at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:121)
 4     at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
 5     at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:81)
 6     at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:55)
 7     at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:65)
 8     at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:70)
 9     at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:853)
10     at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:440)
11     at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:241)
12     at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:221)
13     at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:175)
14     at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:220)
15     at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:206)
16     at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:203)
17     at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1138)
18     at com.mchange.v2.resourcepool.BasicResourcePool.doAcquireAndDecrementPendingAcquiresWithinLockOnSuccess(BasicResourcePool.java:1125)
19     at com.mchange.v2.resourcepool.BasicResourcePool.access$700(BasicResourcePool.java:44)
20     at com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask.run(BasicResourcePool.java:1870)
21     at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:696)
22 Caused by: com.mysql.cj.core.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.
23     at sun.reflect.GeneratedConstructorAccessor15.newInstance(Unknown Source)
24     at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
25     at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
26     at com.mysql.cj.core.exceptions.ExceptionFactory.createException(ExceptionFactory.java:54)
27     at com.mysql.cj.core.exceptions.ExceptionFactory.createException(ExceptionFactory.java:73)
28     at com.mysql.cj.core.util.TimeUtil.getCanonicalTimezone(TimeUtil.java:118)
29     at com.mysql.cj.mysqla.MysqlaSession.configureTimezone(MysqlaSession.java:360)
30     at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:1434)
31     at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:986)
32     at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:843)
33     ... 12 more
34
35 27-Nov-2017 21:58:49.669 严重 [C3P0PooledConnectionPoolManager[identityToken->1hgefi99r1yeux661are7zk|12876430]-HelperThread-#0] com.mchange.v2.resourcepool.BasicResourcePool. A RESOURCE POOL IS PERMANENTLY BROKEN! [com[email protected]74bb9e1f] (because a series of 10 acquisition attempts failed.)
36 27-Nov-2017 21:58:49.671 严重 [C3P0PooledConnectionPoolManager[identityToken->1hgefi99r1yeux661are7zk|12876430]-HelperThread-#0] com.mchange.v2.resourcepool.BasicResourcePool. [email protected] -- Unexpectedly broken!!!
37  com.mchange.v2.resourcepool.ResourcePoolException: Unexpected Break Stack Trace!
38     at com.mchange.v2.resourcepool.BasicResourcePool.unexpectedBreak(BasicResourcePool.java:974)
39     at com.mchange.v2.resourcepool.BasicResourcePool.access$1000(BasicResourcePool.java:44)
40     at com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask.run(BasicResourcePool.java:1927)
41     at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:696)
42
43 27-Nov-2017 21:58:49.678 信息 [http-nio-8080-exec-4] com.mchange.v2.resourcepool.BasicResourcePool. [email protected] -- an attempt to checkout a resource was interrupted, because the pool is now closed. [Thread: http-nio-8080-exec-4]

关键在第二行,在jdbc的url后面在加上一个  serverTimezone=UTC 就行了。

最后上一下最终修改好的数据源配置代码

jdbc.properties

 1 ###### C3P0 MySQL CONFIG #######
 2 jdbc.driverClassName=com.mysql.cj.jdbc.Driver
 3 jdbc.url=jdbc:mysql://localhost/localstudent?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
 4 jdbc.username=root
 5 jdbc.password=123456
 6
 7 connection.initialPoolSize=1
 8 connection.minPoolSize=1
 9 connection.maxPoolSize=5
10 connection.acquireIncrement=2
11 connection.acquireRetryAttempts=10
12 connection.acquireRetryDelay=1000
13 connection.maxIdleTime=3600
14 connection.idleConnectionTestPeriod=20
15 connection.maxStatements=0
16 connection.maxStatementsPerConnection=0

springmvc-servlet.xml

 1 <!-- 获取mysql数据库连接池配置信息 -->
 2     <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
 3         <property name="locations" value="classpath:config/jdbc.properties"/>
 4     </bean>
 5     <!-- 配置c3p0数据源连接池 -->
 6     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
 7           destroy-method="close">
 8         <property name="driverClass" value="${jdbc.driverClassName}"/>
 9         <property name="jdbcUrl" value="${jdbc.url}"/>
10         <property name="user" value="${jdbc.username}"/>
11         <property name="password" value="${jdbc.password}"/>
12         <property name="initialPoolSize" value="${connection.initialPoolSize}"/>
13         <property name="minPoolSize" value="${connection.minPoolSize}"/>
14         <property name="maxPoolSize" value="${connection.maxPoolSize}"/>
15         <property name="acquireIncrement" value="${connection.acquireIncrement}"/>
16         <property name="acquireRetryAttempts" value="${connection.acquireRetryAttempts}"/>
17         <property name="acquireRetryDelay" value="${connection.acquireRetryDelay}"/>
18         <property name="maxIdleTime" value="${connection.maxIdleTime}"/>
19         <property name="idleConnectionTestPeriod" value="${connection.idleConnectionTestPeriod}"/>
20         <property name="maxStatements" value="${connection.maxStatements}"/>
21         <property name="maxStatementsPerConnection" value="${connection.maxStatementsPerConnection}"/>
22         <property name="preferredTestQuery" value="select 1"/>
23         <property name="breakAfterAcquireFailure" value="true"/>
24         <property name="testConnectionOnCheckout" value="false"/>
25     </bean>
时间: 2024-11-09 00:21:32

配置Spring数据源遇到的那些坑。。。的相关文章

Spring MVC 使用tomcat中配置的数据源

Spring MVC 使用tomcat中配置的数据源 配置tomcat数据源 打开tomcat目录下的conf目录,编辑sever.xml目录.在<GlobalNamingResources>标签中添加数据源配置: <Resource name="jdbc/dbsourse" scope="Shareable" type="javax.sql.DataSource" factory="org.apache.tomcat

SpringBoot2 配置多数据源,整合MybatisPlus增强插件

本文源码:GitHub·点这里 || GitEE·点这里 一.项目案例简介 1.多数据简介 实际的项目中,经常会用到不同的数据库以满足项目的实际需求.随着业务的并发量的不断增加,一个项目使用多个数据库:主从复制.读写分离.分布式数据库等方式,越来越常见. 2.MybatisPlus简介 MyBatis-Plus(简称 MP)是一个MyBatis的增强工具,在MyBatis的基础上只做增强不做改变,为简化开发.提高效率而生. 插件特点 无代码侵入:只做增强不做改变,引入它不会对现有工程产生影响.

Spring 数据源配置三:多数据源

在上一节中,我们讲述了多数据的情况: 1. 数据源不同(数据库厂商不同, 业务范围不同, 业务数据不同) 2. SQL mapper 文件不同, 3. mybatis + 数据方言不同 即最为简单的多数据, 将多个数据源叠加在一起,不同service--->dao--->sessionFactory; 如果上述的所有条件都相同,仅仅是数据的的多个拷贝情况,想做主备(读写分离),那我们当然可以用2套复制的代码和配置,但是显然不是最佳的实现方式. 这里,我们就讲解一下多数据源的读写分离,怎么实现.

Spring配置DataSource数据源

在Spring框架中有如下3种获得DataSource对象的方法: 1.从JNDI获得DataSource. 2.从第三方的连接池获得DataSource. 3.使用DriverManagerDataSource获得DataSource. 一.从JNDI获得DataSource SpringJNDI数据源配置信息: <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean&qu

Spring动态配置多数据源

Spring动态配置多数据源,即在大型应用中对数据进行切分,并且采用多个数据库实例进行管理,这样可以有效提高系统的水平伸缩性.而这样的方案就会不同于常见的单一数据实例的方案,这就要程序在运行时根据当时的请求及系统状态来动态的决定将数据存储在哪个数据库实例中,以及从哪个数据库提取数据. 基本信息 1.Spring配置多数据源的方式和具体使用过程. 2.Spring对于多数据源,以数据库表为参照,大体上可以分成两大类情况: 一是,表级上的跨数据库.即,对于不同的数据库却有相同的表(表名和表结构完全相

Spring配置c3p0数据源时出错报:java.lang.NoClassDefFoundError: com/mchange/v2/ser/Indirector

今天在使用Spring配置c3p0数据源时,使用的数据库是mysql,服务器是tomcat,运行时报了一个 java.lang.NoClassDefFoundError: com/mchange/v2/ser/Indirector 网络上找了很久都没有解决,最后发现是因为:         C3P0 少了个 jar 包,mchange-commons-java-0.2.3.1.jar 因为c3p0.jar依赖另一个jar包,就是mchange-commons-java-0.2.3.1.jar.

Spring配置动态数据源-读写分离和多数据源

在现在互联网系统中,随着用户量的增长,单数据源通常无法满足系统的负载要求.因此为了解决用户量增长带来的压力,在数据库层面会采用读写分离技术和数据库拆分等技术.读写分离就是就是一个Master数据库,多个Slave数据库,Master数据库负责数据的写操作,slave库负责数据读操作,通过slave库来降低Master库的负载.因为在实际的应用中,数据库都是读多写少(读取数据的频率高,更新数据的频率相对较少),而读取数据通常耗时比较长,占用数据库服务器的CPU较多,从而影响用户体验.我们通常的做法

Spring 数据源配置二:多数据源

通过上一节  Spring 数据源配置一: 单一数据源  我们了解单一数据源的配置, 这里我们继续多个数据源的配置 如下(applicationContent.xml 内容) 一:  Spring  配置: <!-- MYSQL 配置 --> <bean id="mysqlDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <p

spring 配置多数据源 (案例)

*.properties配置: <!--数据连接配置一--> jdbc.type=mysqljdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3066/qshop?useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false&allowMultiQueries=true&serverTimezone=GMT%2b8j