Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Transaction not successfully started exception using Spring Hibernate

I have a UserProfile entity which I need to save. after saving the entity in the database, I get the following exception:

Could not commit Hibernate transaction; nested exception is org.hibernate.TransactionException: Transaction not successfully started

Also when I see the table the entity is persisted instead of doing a rollback!

@Transactional(isolation=Isolation.REPEATABLE_READ)
public class HibernateUserProfileDAO implements UserProfileDAO {
    private org.hibernate.SessionFactory sessionFactory;
    public UserProfile getUserProfile(int userId) {
        org.hibernate.classic.Session session = sessionFactory.getCurrentSession();
        session.beginTransaction();
        UserProfile userProfile = new UserProfile();
        userProfile.setUserName("sury1");
        session.save(userProfile);
        session.getTransaction().commit();
        session.close();
        return userProfile;
    }
}

I'm using hibernate transaction manager

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>

and my hibernate config is:

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="packagesToScan" value="com.springheatmvn.domain"/>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.connection.pool_size">10</prop>
            <prop key="hibernate.connection.show_sql">true</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>     
</bean>

Can anyone pl. tell me what's going on here?

like image 475
tintin Avatar asked Sep 20 '11 12:09

tintin


1 Answers

I think you've fall a victim of dual transaction managements. If you are using Spring Transaction Management and Hibernate Transaction Management together in the same project, you are more likely to have this issue.

Your code then should either be:

Option 1. Hibernate transaction management

public class HibernateUserProfileDAO implements UserProfileDAO {
    private org.hibernate.SessionFactory sessionFactory;
    public UserProfile getUserProfile(int userId) {
        org.hibernate.classic.Session session = sessionFactory.getCurrentSession();
        session.beginTransaction();
        UserProfile userProfile = new UserProfile();
        userProfile.setUserName("sury1");
        session.save(userProfile);
        session.getTransaction().commit();
        session.close();
        return userProfile;
    }
}

or Option 2. Spring transaction Management

@Transactional
public class HibernateUserProfileDAO implements UserProfileDAO {
    private org.hibernate.SessionFactory sessionFactory;
    public UserProfile getUserProfile(int userId) {
        org.hibernate.classic.Session session = sessionFactory.getCurrentSession();
        UserProfile userProfile = new UserProfile();
        userProfile.setUserName("sury1");
        session.save(userProfile);
        session.close();
        return userProfile;
    }
}
like image 129
Bitmap Avatar answered Nov 01 '22 17:11

Bitmap