MySQL数据库,使用hibernate配置连接时,报错:
No operations allowed after connection closed。
hibernate.cfg.xml的配置如下:
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<!-- Assume test is the database name -->
<property name="hibernate.connection.url">
jdbc:mysql://localhost/test?autoReconnect=true
</property>
<property name="hibernate.connection.username">
root
</property>
<property name="hibernate.connection.password">
root
</property>
<!-- List of XML mapping files -->
<mapping resource="Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>
但是使用dos控制台可以正常操作,根据网上查阅的资料,说是mysql设置有等待连接时间,当超过等待连接时间时,便会无法连接,但是我的mysql服务是刚启动的,查看了数据库的等待连接时间,远远没有达到超时,不存在超时一说。之后怀疑是我的hibernate配置有问题,又直接使用JDBC测试连接,还是报一样的错。
又看到stackflow上有人说需要配置成自动连接,于是修改配置文件:
<property name="hibernate.connection.url">
jdbc:mysql://localhost/test?autoReconnect=true
</property>
结果还是一样,继续报错:
No operations allowed after connection closed。
最后想起使用的driver是官网上最新的mysql-connector-java-8.0.7-dmr-bin.jar,但是我的数据库版本是5.0.67,可能是驱动版本不一致导致错误。于是下载了mysql-5.0.7.jar,重新测试,没问题了~
折腾了半天,原来是驱动版本不一致造成的,以前一直以为高版本的驱动可以兼容低版本的,现在接受教训了。