用数据库连接池解决org.hibernate.exception.JDBCConnectionException:could not execute query
2017-04-07 17:29
573 查看
调试hibernate, 过一段时间网站就报错,后台提示 Connection is closed
等错误。
百思不得其解,
运行一段时间就会有连接保存,session closed掉。
有人说是数据库的问题,于是查sqlserver库的session连接,以及timeout时间等,都不是根本的解决方式,于是重新改造cp30连接池。
总结了一下,hibernate支持的连接池有这几个:
1.DriverManagerConnectionProvider:代表由Hibernate提供的默认的数据库连接池
2.C3P0ConnectionProvider:代表C3P0连接池
3.ProxoolConnectionProvider:代表Proxool连接池
4.DBCPConnectionProvider:代表DBCP连接池
转载地址:http://blog.csdn.net/isnotsuitable/article/details/6990505
在hibernate.cfg.xml中添加C3P0的配置:
在配置中增加了一项连接前检查连接的项:
testConnectionOnCheckout
如果设置为true,每次从池中取一个连接,将做一下测试,使用automaticTestTable 或者 preferredTestQuery,做一条查询语句.看看连接好不好用,不好用,就关闭它,重新从池中拿一个.
整体配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 配置事务实现方式 -->
<property name="transaction.factory_class">
org.hibernate.transaction.JDBCTransactionFactory
</property>
<!-- 配置JDBC里batch的大小 -->
<property name="jdbc.batch_size">50</property>
<property name="cache.use_second_level_cache">false</property>
<!-- 配置线程安全的session -->
<property name="current_session_context_class">thread</property>
<!-- 显示SQL -->
<property name="show_sql">false</property>
<property name="format_sql">false</property>
<!-- 配置数据库方言 -->
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<!-- 配置数据库连接 -->
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.username">root</property>
<property name="connection.password">111111</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/alumnus
</property>
<!-- 配置连接池 -->
<property name="hibernate.connection.provider_class">
org.hibernate.connection.C3P0ConnectionProvider
</property>
<property name="c3p0.acquire_increment">1</property>
<property name="c3p0.idle_test_period">300</property>
<property name="c3p0.max_size">20</property>
<property name="c3p0.max_statements">100</property>
<property name="c3p0.min_size">5</property>
<property name="c3p0.timeout">90</property>
<property name="c3p0.preferredTestQuery ">select 1 from user where id=1</property>
<property name="c3p0.idleConnectionTestPeriod ">18000</property>
<property name="c3p0.maxIdleTime">25000</property>
<property name="c3p0.testConnectionOnCheckout">true</property>
<!-- 指定hibernate管理的映射文件 -->
<mapping resource="com/alumnus/data/Users.hbm.xml" />
<mapping resource="com/alumnus/data/Alumnus.hbm.xml" />
<mapping resource="com/alumnus/data/Register.hbm.xml" />
</session-factory>
</hibernate-configuration> 转载自用数据库连接池解决org.hibernate.exception.JDBCConnectionException:could
not execute query
等错误。
百思不得其解,
运行一段时间就会有连接保存,session closed掉。
有人说是数据库的问题,于是查sqlserver库的session连接,以及timeout时间等,都不是根本的解决方式,于是重新改造cp30连接池。
总结了一下,hibernate支持的连接池有这几个:
1.DriverManagerConnectionProvider:代表由Hibernate提供的默认的数据库连接池
2.C3P0ConnectionProvider:代表C3P0连接池
3.ProxoolConnectionProvider:代表Proxool连接池
4.DBCPConnectionProvider:代表DBCP连接池
转载地址:http://blog.csdn.net/isnotsuitable/article/details/6990505
在hibernate.cfg.xml中添加C3P0的配置:
在配置中增加了一项连接前检查连接的项:
testConnectionOnCheckout
如果设置为true,每次从池中取一个连接,将做一下测试,使用automaticTestTable 或者 preferredTestQuery,做一条查询语句.看看连接好不好用,不好用,就关闭它,重新从池中拿一个.
<!-- 配置连接池 --> <property name="c3p0.acquire_increment">1</property> <property name="c3p0.idle_test_period">300</property> <property name="c3p0.max_size">20</property> <property name="c3p0.max_statements">100</property> <property name="c3p0.min_size">5</property> <property name="c3p0.timeout">90</property> <property name="c3p0.preferredTestQuery ">select 1 from user where id=1</property> <property name="c3p0.idleConnectionTestPeriod ">18000</property> <property name="c3p0.maxIdleTime">25000</property> <property name="c3p0.testConnectionOnCheckout">true</property>
整体配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 配置事务实现方式 -->
<property name="transaction.factory_class">
org.hibernate.transaction.JDBCTransactionFactory
</property>
<!-- 配置JDBC里batch的大小 -->
<property name="jdbc.batch_size">50</property>
<property name="cache.use_second_level_cache">false</property>
<!-- 配置线程安全的session -->
<property name="current_session_context_class">thread</property>
<!-- 显示SQL -->
<property name="show_sql">false</property>
<property name="format_sql">false</property>
<!-- 配置数据库方言 -->
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<!-- 配置数据库连接 -->
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.username">root</property>
<property name="connection.password">111111</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/alumnus
</property>
<!-- 配置连接池 -->
<property name="hibernate.connection.provider_class">
org.hibernate.connection.C3P0ConnectionProvider
</property>
<property name="c3p0.acquire_increment">1</property>
<property name="c3p0.idle_test_period">300</property>
<property name="c3p0.max_size">20</property>
<property name="c3p0.max_statements">100</property>
<property name="c3p0.min_size">5</property>
<property name="c3p0.timeout">90</property>
<property name="c3p0.preferredTestQuery ">select 1 from user where id=1</property>
<property name="c3p0.idleConnectionTestPeriod ">18000</property>
<property name="c3p0.maxIdleTime">25000</property>
<property name="c3p0.testConnectionOnCheckout">true</property>
<!-- 指定hibernate管理的映射文件 -->
<mapping resource="com/alumnus/data/Users.hbm.xml" />
<mapping resource="com/alumnus/data/Alumnus.hbm.xml" />
<mapping resource="com/alumnus/data/Register.hbm.xml" />
</session-factory>
</hibernate-configuration> 转载自用数据库连接池解决org.hibernate.exception.JDBCConnectionException:could
not execute query
相关文章推荐
- 用数据库连接池解决org.hibernate.exception.JDBCConnectionException:could not execute query
- 用数据库连接池解决org.hibernate.exception.JDBCConnectionException:could not execute query
- 用数据库连接池解决org.hibernate.exception.JDBCConnectionException:could not execute query
- 用数据库连接池解决org.hibernate.exception.JDBCConnectionException:could not execute query
- 从数据库方面解决org.hibernate.exception.JDBCConnectionException:could not execute query
- org.hibernate.exception.JDBCConnectionException: could not execute query
- org.hibernate.exception.JDBCConnectionException: could not execute query
- org.hibernate.exception.JDBCConnectionException: could not execute query
- Oracle:org.hibernate.exception.JDBCConnectionException: could not execute query
- org.hibernate.exception.JDBCConnectionException: could not execute query
- org.hibernate.exception.JDBCConnectionException: could not execute query
- org.hibernate.exception.JDBCConnectionException: could not execute query
- org.hibernate.exception.JDBCConnectionException: could not execute query
- org.hibernate.exception.JDBCConnectionException: could not execute query
- 数据库长时间未操作自己断开org.hibernate.exception.JDBCConnectionException: could not execute query
- org.hibernate.exception.SQLGrammarException:Could not execute JDBC batch update问题的解决
- JDBCConnectionException: could not execute query,数据库连接池问题
- cloudera-scm-server启动时出现Caused by: javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: Could not open connection问题解决方法(图文详解)
- org.hibernate.exception.SQLGrammarException:Could not execute JDBC batch update问题的解决
- org.hibernate.exception.GenericJDBCException: Could not open connection解决思路 SSH框架