mysql 8小connection悬空问题及解决办法
今早起来无意间登陆了一下昨晚发布的项目,出乎意料的报了一个错误。不过学习的机会又来了,问题如下。 上网大致搜了一下,发现这是一个普遍存在的问题,还有个很霸气的名字叫做mysql 8小connection悬空问题。其实是由于Mysql连接超时所导致的。具体原因是My
今早起来无意间登陆了一下昨晚发布的项目,出乎意料的报了一个错误。不过学习的机会又来了,问题如下。
上网大致搜了一下,发现这是一个普遍存在的问题,还有个很霸气的名字叫做mysql 8小connection悬空问题。其实是由于Mysql连接超时所导致的。具体原因是Mysql服务器默认的“wait_timeout”是8小时【也就是默认的值默认是28800秒】,也就是说一个connection空闲超过8个小时,Mysql将自动断开该connection,通俗的讲就是一个连接在8小时内没有活动,就会自动断开该连接。wait timeout的值可以设定,但最多只能是2147483,不能再大了。也就是约24.85天
解决的办法那么两三种,最为有效地办法就是建立连接池,会定期维护连接,并处理自动关闭的这种情况。其他几种方法不理想就不做解释和记录了。
在我的项目中采用了hibernate,然而hibernate自带的连接池实在太渣(听说,没有考证,但出现了这个问题可以从一方面看出不给力)。所以只能采用主流的连接池,有这么几种。
1. DBCP.
Apche的DBCP在Hibernate2中受支持,但在Hibernate3中已经不再推荐使用,官方的解释是这个连接池存在缺陷。如果你因为某种原因需要在Hibernate3中使用DBCP,建议采用JNDI方式。
在我的项目一种我选择了c3p0,现将配置过程罗列如下:2.C3P0.
Hibernate2和Hibernate3的命名空间有所变化。例如,配置C3P0时的provider_class有Hibernate2环境下使用net.sf.hibernate.connection.C3P0ConnectionProvider,在Hibernate3环境下使用org.hibernate.connection.C3P0ConnectionProvider。
3.Proxool
至于三者的比较,网上有很多大家可以去查找,我发现一个比较有趣的小实验,可以自己编码来测试性能,大家可以试试。
有趣的实验还添加了与druid的比较(http://286.iteye.com/blog/1920417)
hibernate.cfg.xml
<hibernate-configuration> <session-factory> <property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property> <!--连接池的最小连接数--> <property name="c3p0.min_size">5</property> <!--最大连接数--> <property name="c3p0.max_size">30</property> <!--连接超时时间--> <property name="c3p0.timeout">1800</property> <!--statemnets缓存大小--> <property name="c3p0.max_statements">100</property> <!--每隔多少秒检测连接是否可正常使用 --> <property name="c3p0.idle_test_period">121</property> <!--当池中的连接耗尽的时候,一次性增加的连接数量,默认为3--> <property name="c3p0.acquire_increment">3</property> <property name="c3p0.validate">true</property> <!-- 连接池每隔60秒自动检测数据库连接情况,如果断开则自动重连 --> <property name="idleConnectionTestPeriod">60</property> <property name="testConnectionOnCheckout">true</property> <property name="dialect"> org.hibernate.dialect.MySQLDialect </property> <property name="connection.url"> jdbc:mysql://xxxx:3306/xxx?useUnicode=true&characterEncoding=UTF8 </property> <property name="connection.username">username</property> <property name="connection.password">password</property> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <mapping resource="com/demontf/bean/Project.hbm.xml"></mapping> <mapping resource="com/demontf/bean/User.hbm.xml"></mapping> </session-factory> </hibernate-configuration>Copy after login
一个不错的hibernate配置c3p0的教程:http://www.cnblogs.com/best/archive/2013/05/09/3069839.html网上很多一部分教程是说下面这个语句一定要加,其实在这里我倒没出什么错误,主要是在问题出在引包中。
<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>Copy after login开始导入的是c3p0-0.9.2.1.jar ,但总是出现问题,Could not initialize class com.dao.HibernateSessionFactory。最终,导入以下两个jar包解决该问题c3p0配置成功
c3p0-0.9.1.jar和hibernate-c3p0-4.1.11.Final.jar(我已经将资源上传)

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

When developing an e-commerce website using Thelia, I encountered a tricky problem: MySQL mode is not set properly, causing some features to not function properly. After some exploration, I found a module called TheliaMySQLModesChecker, which is able to automatically fix the MySQL pattern required by Thelia, completely solving my troubles.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.
