Java 休眠异常:事务回滚失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18266507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
hibernate exception: transaction roll back failed
提问by Shakeeb Manjeri
When im running my hibernate project in java swing, it works at first. but when i wait for some time and i recieve error like org.hibernate.TransactionException: rollback failed.. tell me a solution for this.
当我在 java swing 中运行我的 hibernate 项目时,它首先工作。但是当我等待一段时间并收到类似 org.hibernate.TransactionException: rollback failed.. 之类的错误时,请告诉我一个解决方案。
Here is my error
这是我的错误
Aug 16, 2013 10:52:21 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 0, SQLState: 08S01
Aug 16, 2013 10:52:21 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Communications link failure
The last packet successfully received from the server was 89,371 milliseconds ago.
The last packet sent successfully to the server was 1 milliseconds ago.
Exception in thread "AWT-EventQueue-0" org.hibernate.TransactionException: rollback failed
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.rollback(AbstractTransactionImpl.java:215) at com.softroniics.queenpharma.services.PurchaseOrderService.showAllPurchase(PurchaseOrderService.java:131)
Here is my hibernate cfg file
这是我的休眠 cfg 文件
<?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="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://queenpharma.db.11583306.hostedresource.com/queenpharma</property>
<property name="connection.username">queenpharma</property>
<property name="connection.password">Queenpharma#1</property>
<property name="connection.pool_size">1</property>
<property name="hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<property name="connection.autocommit">false</property>
<property name="hibernate.c3p0.max_size">1</property>
<property name="hibernate.c3p0.min_size">0</property>
<property name="hibernate.c3p0.timeout">5000</property>
<property name="hibernate.c3p0.max_statements">1000</property>
<property name="hibernate.c3p0.idle_test_period">300</property>
<property name="hibernate.c3p0.acquire_increment">1</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<mapping class="com.softroniics.queenpharma.model.LoginModel" />
<mapping class="com.softroniics.queenpharma.model.PurchaseCompanyModel" />
---- and so on------
- - 等等 - - -
here is some my code
这是我的一些代码
session = sessionFactory.openSession();
StockModel stockModel = null;
try {
tx = session.beginTransaction();
Iterator<StockModel> iterator = session
.createQuery("FROM StockModel where productid='" + id + "'")
.list().iterator();
if(iterator.hasNext()){
stockModel = iterator.next();
}
} catch (HibernateException e) {
if (tx != null)
tx.rollback();
e.printStackTrace();
} finally {
session.close();
采纳答案by c.s.
The error code you get SQLState: 08S01
suggests that the host name that you use for the database is incorrect according to Mapping MySQL Error Numbers to JDBC SQLState Codes.
SQLState: 08S01
根据Mapping MySQL Error Numbers to JDBC SQLState Codes,您得到的错误代码表明您用于数据库的主机名不正确。
So first please make sure that the database host: queenpharma.db.11583306.hostedresource.com
is spelled correctly.
所以首先请确保数据库主机:queenpharma.db.11583306.hostedresource.com
拼写正确。
If the error persists please modify your exception handler to catch the exception caused by the rollback statement like below so you are able to understand what caused the rollback in the first place.
如果错误仍然存在,请修改您的异常处理程序以捕获由如下回滚语句引起的异常,以便您能够首先了解导致回滚的原因。
Note: you should do this only for troubleshooting this issue. You do not want to shallow any exceptions when in a production environment
注意:您应该仅在解决此问题时执行此操作。在生产环境中,您不想淡化任何异常
} catch (HibernateException e) {
if (tx != null) {
try {
tx.rollback();
} catch(Exception re) {
System.err.println("Error when trying to rollback transaction:"); // use logging framework here
re.printStackTrace();
}
}
System.err.println("Original error when executing query:"); // // use logging framework here
e.printStackTrace();
}
回答by Siva
It seems like the issue with Mysql connection time out, Guess there would be default time out for Mysql. Refer this article might help you Hibernate Broken pipe
似乎是 Mysql 连接超时的问题,猜猜 Mysql 会有默认超时。参考这篇文章可能会帮助你Hibernate Broken pipe
UPDATE
From the hibernate documents
更新
从休眠文档
Hibernate's own connection pooling algorithm is, however, quite rudimentary. It is intended to help you get started and is not intended for use in a production system, or even for performance testing. You should use a third party pool for best performance and stability. Just replace the hibernate.connection.pool_size property with connection pool specific settings. This will turn off Hibernate's internal pool. For example, you might like to use c3p0.
然而,Hibernate 自己的连接池算法非常初级。它旨在帮助您入门,不适用于生产系统,甚至不用于性能测试。您应该使用第三方池以获得最佳性能和稳定性。只需将 hibernate.connection.pool_size 属性替换为连接池特定设置。这将关闭 Hibernate 的内部池。例如,您可能喜欢使用 c3p0。
So you no need to specify the hibernate connection pool size property when you are using c3p0 connection pooling
所以你在使用 c3p0 连接池时不需要指定 hibernate 连接池大小属性
回答by sorencito
Remember committing and closing the session. It might will be that you do not commit and Hibernate tries a rollback after the connection has already timed out.
记住提交和关闭会话。可能是您没有提交并且 Hibernate 在连接超时后尝试回滚。
It would be helpful to see how you access the DB, please post some code.
查看您如何访问数据库会很有帮助,请发布一些代码。