【问题标题】:Why Hibernate is not rolling back the insert query from the table?为什么 Hibernate 不回滚表中的插入查询?
【发布时间】:2020-06-23 07:56:40
【问题描述】:

我有一个这样的伪代码:

Session session = sessionFactory.currentSession();
Connection connection = ((SessionImpl) session).connection();
connection.setAutoCommit(false);
Statement statement = connection.createStatement();
String sql = "INSERT into table...";
statement.execute(query);
connection.rollback(); // just for testing

当我执行此代码时,它应该回滚插入查询,但它没有执行回滚。

我可以看到表格中的数据。那么,根本原因是什么?为什么不回滚?

【问题讨论】:

    标签: java hibernate jdbc


    【解决方案1】:

    注意:

    此代码用于解释行为以用于学习目的。不建议这样执行sql。

    我不确定,因为我们不知道您的代码中还有什么。这是我的代码,它按预期回滚和提交。

    @Repository
    public class DealRepositoryImpl implements DealRepositoryCustom {
    
        @PersistenceContext
        EntityManager entityManager;
    
        //This one gets rolled back
        @Override
        @Transactional 
        public void saveDealWithTransactionAnnotation() {
            try {
                Session session = entityManager.unwrap(Session.class);
                Connection connection = ((SessionImpl) session).connection();
                connection.setAutoCommit(false);
                Statement statement = connection.createStatement();
                String sql = "insert into deal (name, id)  values ('WithTransactionAnnotationAndRollback', 100)";
                statement.execute(sql);
                connection.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    
        //This one also gets rolled back
        @Override
        public void saveDealWithoutTransactionAnnotation() {
            try {
                Session session = entityManager.unwrap(Session.class);
                Connection connection = ((SessionImpl) session).connection();
                connection.setAutoCommit(false);
                Statement statement = connection.createStatement();
                String sql = "insert into deal (name, id)  values ('WithoutTransactionAnnotationAndRollback', 200)";
                statement.execute(sql);
                connection.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    
        //this one gets committed
        @Override
        public void saveDealWithCommit() {
            try {
                Session session = entityManager.unwrap(Session.class);
                Connection connection = ((SessionImpl) session).connection();
                connection.setAutoCommit(false);
                Statement statement = connection.createStatement();
                String sql = "insert into deal (name, id)  values ('withCommit', 400)";
                statement.execute(sql);
                connection.commit();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    

    【讨论】:

    • 是的!我有这个
    • 我应该如何改变?我应该删除它吗?
    • 代码没有问题。我认为查询是问题所在。这是一种可重入插入查询。
    • 它不会恢复。
    猜你喜欢
    • 2021-07-19
    • 2017-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-13
    相关资源
    最近更新 更多