走近SSH之Hibernate--com.mysql.jdbc.CommunicationsException: Communications link failure due to underlyi

    进行java的学习已有几个月的时间了,可还是感觉使用vs+sqlserver开发比较舒服,最近学习java框架,接触了很多新概念,遇到各式各样的问题真有些不习惯,提示都是英文字母+阿拉伯数字,不过通过一个一个词翻译也能理解出现的错误是什么意思。


   今天由于mysql的编码问题,我不小心重装了mysql,装完后在建立数据库表的时候,出现下面这样一个问题:

14:22:02,783 ERROR SchemaExport:202 - schema export unsuccessful
com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception: 


** BEGIN NESTED EXCEPTION ** 


java.net.SocketException
MESSAGE: java.net.ConnectException: Connection refused: connect


STACKTRACE:


java.net.SocketException: java.net.ConnectException: Connection refused: connect
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:156)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:284)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2565)
at com.mysql.jdbc.Connection.<init>(Connection.java:1485)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:154)
at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:110)
at org.hibernate.tool.hbm2ddl.ManagedProviderConnectionHelper.prepare(ManagedProviderConnectionHelper.java:28)
at org.hibernate.tool.hbm2ddl.SchemaExport.execute(SchemaExport.java:180)
at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:133)
at com.tgbnode.hibernate.ExportDB.main(ExportDB.java:21)

** END NESTED EXCEPTION **


Last packet sent to the server was 20 ms ago.
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2631)
at com.mysql.jdbc.Connection.<init>(Connection.java:1485)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:154)
at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:110)
at org.hibernate.tool.hbm2ddl.ManagedProviderConnectionHelper.prepare(ManagedProviderConnectionHelper.java:28)
at org.hibernate.tool.hbm2ddl.SchemaExport.execute(SchemaExport.java:180)
at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:133)
at com.tgbnode.hibernate.ExportDB.main(ExportDB.java:21)


    通过错误提示,可以大概看出这是数据库无法连接的问题。通过查找问题的解决方法,发现异常“com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception”,出现的原因是:Mysql服务器默认的“wait_timeout”是8小时,也就是说一个connection空闲超过8个小时,Mysql将自动断开该 connection。这就是问题的所在,在C3P0 pools中的connections如果空闲超过8小时,Mysql将其断开,而C3P0并不知道该connection已经失效,如果这时有 Client请求connection,C3P0将该失效的Connection提供给Client,将会造成上面的异常。

最后在网上找到一个办法。为hibernate配置连接池,推荐用c3p0,然后配置c3p0的反空闲设置idle_test_period,只要小于MySQL的wait timeout即可。
在hibernate.cfg.xml中增加下面几项:

<!-- configuration pool via c3p0--> 
<property name="hibernate.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.time_out">1800</property> <!-- seconds --><!-- default: 0 --> 
<property name="c3p0.max_statement">50</property> <!-- default: 0 --> 
<property name="c3p0.acquire_increment">1</property> <!-- default: 1 --> 
<property name="c3p0.idle_test_period">120</property> <!-- seconds --><!-- default: 0 --> 
<property name="c3p0.validate">true</property> 

按照提示设置好后,又遇到这样一个问题:

14:44:44,298  WARN BasicResourcePool:1222 - com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@6c69d02b -- 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 (30).
14:44:44,311 ERROR SchemaExport:202 - schema export unsuccessful
java.sql.SQLException: Connections could not be acquired from the underlying database!
at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:104)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:236)
at com.mchange.v2.c3p0.PoolBackedDataSource.getConnection(PoolBackedDataSource.java:94)
at org.hibernate.connection.C3P0ConnectionProvider.getConnection(C3P0ConnectionProvider.java:35)
at org.hibernate.tool.hbm2ddl.ManagedProviderConnectionHelper.prepare(ManagedProviderConnectionHelper.java:28)
at org.hibernate.tool.hbm2ddl.SchemaExport.execute(SchemaExport.java:180)
at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:133)
at com.tgbnode.hibernate.ExportDB.main(ExportDB.java:21)
Caused by: com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source.
at com.mchange.v2.resourcepool.BasicResourcePool.awaitAcquire(BasicResourcePool.java:970)
at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:208)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:232)
... 6 more

这里提示的更清晰了,就是连接数据库不成功,这时候突然想起自己重装mysql时,端口号用的是3309,而我之前使用的端口号是3306,敲代码时使用的也是3306,将代码中的端口3306改为3309问题解决了。


Hibernate.cfg.xml配置文件连接数据库设置如下:

<hibernate-configuration>
	<session-factory>
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<!--Connect DB-->
		<property name="hibernate.connection.url">jdbc:mysql://localhost:3309/hibernate_hql</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">123456</property>
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		<property name="hibernate.show_sql">true</property>
		<!-- 
		<property name="hibernate.format_sql">true</property>
		 -->
		 
		<mapping resource="com/tgbnode/hibernate/Student.hbm.xml"/>
		<mapping resource="com/tgbnode/hibernate/Classes.hbm.xml"/>
	</session-factory>
</hibernate-configuration>

Hibernate连接MySql,Hibernate代码中设置的端口应该与MySql数据库中的对应才能正常连接,这看似不起眼的问题,其实恰恰是全局观的展现,也是知识网的一种展现。

  • 7
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 28
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 28
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值