Shakeeb Manjeri
Shakeeb Manjeri

Reputation: 130

hibernate exception: transaction roll back failed

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.

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

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

Upvotes: 2

Views: 40992

Answers (3)

c.s.
c.s.

Reputation: 4816

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.

So first please make sure that the database host: queenpharma.db.11583306.hostedresource.com is spelled correctly.

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

Upvotes: 4

sorencito
sorencito

Reputation: 2625

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.

It would be helpful to see how you access the DB, please post some code.

Upvotes: 0

Siva
Siva

Reputation: 1940

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

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.

So you no need to specify the hibernate connection pool size property when you are using c3p0 connection pooling

Upvotes: 1

Related Questions