微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用Spring Hibernate使Transaction无法成功启动异常

我有一个我需要保存的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事务管理器

我的hibernate配置是:

factorybean">
    sql">trueMysqLDialect

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

最佳答案
我认为你已成为双重交易管理的受害者.如果您在同一个项目中一起使用Spring Transaction Management和Hibernate 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.春季交易管理

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐