今天项目报错: No operations allowed after connection closed
错误原因:MySQL服务器默认的“wait_timeout”是28800秒即8小时,意味着如果一个连接的空闲时间超过8个小时,MySQL将自动断开该连接,而连接池却认为该连接还是有效的(因为并未校验连接的有效性),当应用申请使用该连接时,就会导致上面的报错。
解决方案:
1.修改项目配置文件,首开看下公司,原有的配置:online.applicationContext.xml(我的配置文件)
<bean id="db" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://"/>
<property name="username" value=""/>
<property name="password" value=""/>
<property name="defaultCatalog" value="toy"/>
<property name="connectionProperties" value="[useUnicode=false;character_set_client=utf8mb4]"/>
<property name="initialSize" value="5"/>
<property name="maxActive" value="50"/>
<property name="maxIdle" value="50"/>
<property name="minIdle" value="10"/>
<property name="maxWait" value="5000"/>
<property name="removeAbandoned" value="true"/>
<property name="removeAbandonedTimeout" value="10"/>
<property name="logAbandoned" value="true"/>
bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="db" />
bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="db" />
bean>
<aop:aspectj-autoproxy />
公司项目配置的连接池用的是dbcp,看到配置信息还是挺全的,但是少了点什么?
少了两个参数,如下:
1、testOnReturn,testOnBorrow这两个参数为true,表示会在每次请求之前和之后进行连接池测试,如果连接失效,就会将这条连接对象销毁,创建一个新的连接对象,但是本次数据库操作也就失败了。对于读操作可能没什么问题,再请求一次就行了,但对于充值、定时任务等重要写操作来说还不够完美。(PS:使用这两个参数,一定要配置validationQuery参数,这样才会生效。)
2、为了解决以上问题,保证每次操作都有正常的连接池使用,我们来了解一下testWhileIdle参数。这个参数为true时候,表示空闲时是进行验证,检查对象是否有效。然后minEvictableIdleTimeMillis配合timeBetweenEvictionRunsMillis,每过timeBetweenEvictionRunsMillis秒对连接池进行一次检测,将对象闲置时间超过minEvictableIdleTimeMillis秒的对象进行销毁,创建新的对象来取代。这样就能保证时刻都有正常的连接池对象存在。
修改之后:
<bean id="db" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://"/>
<property name="username" value=""/>
<property name="password" value=""/>
<property name="defaultCatalog" value=""/>
<property name="connectionProperties" value="[useUnicode=false;character_set_client=utf8mb4]"/>
<property name="initialSize" value="5"/>
<property name="maxActive" value="50"/>
<property name="maxIdle" value="50"/>
<property name="minIdle" value="10"/>
<property name="maxWait" value="5000"/>
<property name="removeAbandoned" value="true"/>
<property name="removeAbandonedTimeout" value="10"/>
<property name="logAbandoned" value="true"/>
<property name= "timeBetweenEvictionRunsMillis" value="30000" />
<property name= "numTestsPerEvictionRun" value="30" />
<property name="minEvictableIdleTimeMillis" value="1800000" />
方案二:
首先执行sql语句:
SHOW VARIABLES LIKE
"%timeout%"
结果:
我们看到wait_timeout=28800 也就是默认是八小时
修改步骤:
增加 MySQL 的 wait_timeout 属性的值
修改mysql安装目录下的配置文件 my.ini文件(如果没有此文件,复制“my-default.ini”文件,生成“复件 my-default.ini”文件。将“复件 my-default.ini”文件重命名成“my.ini” ),在文件中设置:
wait_timeout=31536000
interactive_timeout=31536000
就可以了
最后看下C3P0的配置吧!
<default-config>
<property name="acquireIncrement">3property>
<property name="acquireRetryAttempts">30property>
<property name="acquireRetryDelay">1000property>
<property name="autoCommitOnClose">falseproperty>
<property name="automaticTestTable">Testproperty>
<property name="breakAfterAcquireFailure">falseproperty>
<property name="checkoutTimeout">100property>
<property name="connectionTesterClassName">property>
<property name="factoryClassLocation">nullproperty>
<property name="forceIgnoreUnresolvedTransactions">falseproperty>
<property name="idleConnectionTestPeriod">60property>
<property name="initialPoolSize">3property>
<property name="maxIdleTime">60property>
<property name="maxPoolSize">15property>
<property name="maxStatements">100property>
<property name="maxStatementsPerConnection">property>
<property name="numHelperThreads">3property>
<property name="overrideDefaultUser">rootproperty>
<property name="overrideDefaultPassword">passwordproperty>
<property name="password">property>
<property name="preferredTestQuery">select id from test where id=1property>
<property name="propertyCycle">300property>
<property name="testConnectionOnCheckout">falseproperty>
<property name="testConnectionOnCheckin">trueproperty>
<property name="user">rootproperty>
在Hibernate(spring管理)中的配置:
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass"><value>oracle.jdbc.driver.OracleDrivervalue>property>
<property name="jdbcUrl"><value>jdbc:oracle:thin:@localhost:1521:Testvalue>property>
<property name="user"><value>Kayvalue>property>
<property name="password"><value>rootvalue>property>
<property name="minPoolSize" value="10" />
<property name="maxPoolSize" value="100" />
<property name="maxIdleTime" value="1800" />
<property name="acquireIncrement" value="3" />
<property name="maxStatements" value="1000" />
<property name="initialPoolSize" value="10" />
<property name="idleConnectionTestPeriod" value="60" />
<property name="acquireRetryAttempts" value="30" />
<property name="breakAfterAcquireFailure" value="true" />
<property name="testConnectionOnCheckout" value="false" />
bean>
###########################
### C3P0 Connection Pool###
###########################
#hibernate.c3p0.max_size 2
#hibernate.c3p0.min_size 2
#hibernate.c3p0.timeout 5000
#hibernate.c3p0.max_statements 100
#hibernate.c3p0.idle_test_period 3000
#hibernate.c3p0.acquire_increment 2
#hibernate.c3p0.validate false
在hibernate.cfg.xml文件里面加入如下的配置:
<property name="hibernate.c3p0.max_size">20property>
<property name="hibernate.c3p0.min_size">5property>
<property name="hibernate.c3p0.timeout">120property>
<property name="hibernate.c3p0.max_statements">100property>
<property name="hibernate.c3p0.idle_test_period">120property>
<property name="hibernate.c3p0.acquire_increment">2property>
<property name="hibernate.c3p0.validate">trueproperty>
新手如有不对,请多多指导。