hibernate - 使用 Spring Hibernate 获取事务未成功启动异常

标签 hibernate spring spring-mvc

我有一个需要保存的 UserProfile 实体。将实体保存在数据库中后,出现以下异常:

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

此外,当我看到表时,实体被持久化而不是回滚!
@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;
    }
}

我正在使用 hibernate 事务管理器
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>

我的 hibernate 配置是:
<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>

任何人都可以。告诉我这里发生了什么?

最佳答案

我认为您已经成为双重事务管理的受害者。如果您正在使用 Spring Transaction ManagementHibernate Transaction Management在同一个项目中,您更有可能遇到此问题。

您的代码应该是:

选项 1。 hibernate 事务管理

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

选项 2。 Spring事务管理
@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;
    }
}

关于hibernate - 使用 Spring Hibernate 获取事务未成功启动异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7484890/

相关文章:

java - Hibernate (MySQL) 上区分大小写的查询

java - 如何从 hibernate 中的存储过程中获取输出值(SQL SERVER)

spring - 将 JSON 反序列化器用于批处理作业执行上下文

java - 如何在junit中设置mockservletcontext的真实路径

spring - 将 Null 传递给 Spring Security UserDetailsS​​ervice

java - org.hibernate.hql.internal.ast.QuerySyntaxException : Data is not mapped [from Data]

java - Scala 案例类到 Spring POJO

java - 无法在 Controller 类中使用组件/服务/存储库

spring - <context :component-scan. ../> 和 <context :property-placeholder. ../> 在分层上下文中的范围

hibernate - 如何使用 Mockito 在 Hibernate 中模拟entityManager.getTransaction().begin()?