加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 01:40:34 所属栏目:大数据 来源:网络整理
导读:我有一个我需要保存的UserProfile实体.在数据库中保存实体后,我得到以下异常: Could not commit Hibernate transaction; nested exception is org.hibernate.TransactionException: Transaction not successfully started 此外,当我看到表时,实体是持久的而

我有一个我需要保存的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配置是:

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读