简体   繁体   English

hibernate异常:事务回滚失败

[英]hibernate exception: transaction roll back failed

When im running my hibernate project in java swing, it works at first. 当我在java swing中运行我的hibernate项目时,它首先工作。 but when i wait for some time and i recieve error like org.hibernate.TransactionException: rollback failed.. tell me a solution for this. 但是,当我等待一段时间后,我收到像org.hibernate.TransactionException这样的错误:回滚失败..告诉我一个解决方案。

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 这是我的hibernate 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();

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的错误代码表明,根据将MySQL错误号映射到JDBC SQLState代码 ,您用于数据库的主机名是不正确的。

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();
}

It seems like the issue with Mysql connection time out, Guess there would be default time out for Mysql. 看来Mysql连接的问题超时了,猜猜Mysql会有默认的超时时间。 Refer this article might help you Hibernate Broken pipe 参考这篇文章可能会帮助你Hibernate Broken管道

UPDATE UPDATE
From the hibernate documents 来自hibernate文档

Hibernate's own connection pooling algorithm is, however, quite rudimentary. 然而,Hibernate自己的连接池算法非常简陋。 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. 只需用连接池特定设置替换hibernate.connection.pool_size属性即可。 This will turn off Hibernate's internal pool . 这将关闭Hibernate的内部池 For example, you might like to use c3p0. 例如,您可能想使用c3p0。

So you no need to specify the hibernate connection pool size property when you are using c3p0 connection pooling 因此,在使用c3p0连接池时,无需指定hibernate连接池大小属性

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. 看看你如何访问数据库会很有帮助,请发布一些代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM