1、c3p0、HikariCP等连接池配置。
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 
 | //c3p0<bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
 <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
 <property name="driverClass" value="${jdbc.driverClass}"></property>
 <property name="user" value="${jdbc.user}"></property>
 <property name="password" value="${jdbc.password}"></property>
 
 <property name="maxIdleTime" value="1800"/>
 <property name="preferredTestQuery" value="SELECT 1"/>
 <property name="idleConnectionTestPeriod" value="18000"/>
 <property name="testConnectionOnCheckout" value="true"/>
 </bean>
 
 | 
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 
 | //hikariCP<bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
 <property name="driverClassName" value="${jdbc.driverClass}"/>
 <property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>
 <property name="username" value="${jdbc.user}"/>
 <property name="password" value="${jdbc.password}"/>
 <property name="maximumPoolSize" value="5"/>
 
 <property name="maxLifetime" value="700000"/>
 <property name="idleTimeout" value="600000"/>
 <property name="connectionTimeout" value="10000"/>
 <property name="dataSourceProperties">
 <props>
 <prop key="dataSourceClassName">com.mysql.jdbc.jdbc2.optional.MysqlDataSource</prop>
 <prop key="cachePrepStmts">true</prop>
 <prop key="prepStmtCacheSize">250</prop>
 <prop key="prepStmtCacheSqlLimit">2048</prop>
 </props>
 </property>
 </bean>
 
 <bean id="pooledDataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
 <constructor-arg ref="hikariConfig"/>
 </bean>
 
 | 
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 
 | //mybatis
 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
 
 <property name="configLocation" value="classpath:mybatis_config.xml"></property>
 <property name="dataSource" ref="pooledDataSource"></property>
 
 <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
 </bean>
 
 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
 
 <property name="basePackage" value="com.duoqux.sc.dao"></property>
 </bean>
 
 
 <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
 <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
 <constructor-arg name="executorType" value="BATCH"></constructor-arg>
 </bean>
 
 
 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
 
 <property name="dataSource" ref="pooledDataSource"></property>
 </bean>
 
 <aop:config>
 
 <aop:pointcut expression="execution(* com.duoqux.sc.service..*(..))" id="txPoint"/>
 
 <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
 </aop:config>
 
 <tx:advice id="txAdvice" transaction-manager="transactionManager">
 <tx:attributes>
 
 <tx:method name="*"/>
 
 <tx:method name="get*" read-only="true"/>
 </tx:attributes>
 </tx:advice>
 
 | 
2、在自己的程序中插入定时访问数据库的方法,比如使用Timer,Quartz或者spring中简易Quartz。
3、在mysql中有相关参数设定,当数据库连接空闲一定时间后,服务器就会断开等待超时的连接:
相关参数
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 
 | mysql> show variables like '%timeout%';+-----------------------------+----------+
 | Variable_name               | Value    |
 +-----------------------------+----------+
 | connect_timeout             | 10       |
 | delayed_insert_timeout      | 300      |
 | innodb_flush_log_at_timeout | 1        |
 | innodb_lock_wait_timeout    | 50       |
 | innodb_rollback_on_timeout  | OFF      |
 | interactive_timeout         | 28800    |
 | lock_wait_timeout           | 31536000 |
 | net_read_timeout            | 30       |
 | net_write_timeout           | 60       |
 | rpl_stop_slave_timeout      | 31536000 |
 | slave_net_timeout           | 3600     |
 | wait_timeout                | 28800    |
 +-----------------------------+----------+
 12 rows in set
 
 | 
同一时间,interactive_timeout,wait_timeout 这两个参数只有一个起作用。
到底是哪个参数起作用,和用户连接时指定的连接参数相关,缺省情况下是使用wait_timeout。
我在配置文件中将wait_timeout修改后在mysql中查寻到还是不起作用,于是将这两个参数都修改了,再次查询wait_timeout的值后才显示修改后的。
4、修改参数
这两个参数的默认值是8小时(60608=28800)。测试过将这两个参数改为0,系统自动将这个值设置为1。也就是说,不能将该值设置为永久。
将这2个参数设置为24小时(606024=86400)。
set interactive_timeout=86400;
set wait_timeout=86400;
也可以修改my.cnf,修改后重起mysql
打开/etc/my.cnf,在属性组mysqld下面添加参数如下:
| 12
 
 | interactive_timeout=28800000wait_timeout=28800000
 
 | 
如果一段时间内没有数据库访问则mysql自身将切断连接,之后访问java访问连接池时对数据库的数据通道早就关闭了。