为什么 myBatis 插入/更新函数现在需要在将 FK 添加到数据库后提交?

发布于 2024-10-07 00:29:10 字数 951 浏览 2 评论 0原文

我有一个使用 myBatis 进行持久化的项目。下面的方法“A”工作得很好,直到我添加了一些外键并将表从 myISAM 转换为 innoDB。转换后,方法“A”将默默失败,甚至不会在日志中发出警告。转换后,只有方法“B”才能成功插入。两种方法都会将正确的 sql 写入日志,但只有“B”有效。

谁能告诉我为什么我现在需要进行提交,但之前不需要进行提交?

//doesnt work, but worked previously
public void A(Role role) {
    SqlSession session = sqlSessionFactory.openSession();
    try {
        RoleMapper mapper = session.getMapper(RoleMapper.class);
        mapper.updateByPrimaryKeySelective(role);
    }catch(Exception e){
        logger.error(e);
    } finally {
        session.close();
    }
    return;
}

//works correctly, but why?
public void B(Role role) {
    SqlSession session = sqlSessionFactory.openSession();
    try {
        RoleMapper mapper = session.getMapper(RoleMapper.class);
        mapper.updateByPrimaryKeySelective(role);
        session.commit();
    }catch(Exception e){
        logger.error(e);
    } finally {
        session.close();
    }
    return;
}

i have a project that uses myBatis for persistence. method "A" below was working just fine until I added some foreign keys and converted my table from myISAM to innoDB. After the conversion, method "A" would fail silently, not even a warning in the logs. After the conversion, only method "B" does a successful insert. Both methods write the correct sql to the logs, but only "B" works.

Can anyone fill me in on why i need to do a commit now, but did not have to do a commit previously?

//doesnt work, but worked previously
public void A(Role role) {
    SqlSession session = sqlSessionFactory.openSession();
    try {
        RoleMapper mapper = session.getMapper(RoleMapper.class);
        mapper.updateByPrimaryKeySelective(role);
    }catch(Exception e){
        logger.error(e);
    } finally {
        session.close();
    }
    return;
}

//works correctly, but why?
public void B(Role role) {
    SqlSession session = sqlSessionFactory.openSession();
    try {
        RoleMapper mapper = session.getMapper(RoleMapper.class);
        mapper.updateByPrimaryKeySelective(role);
        session.commit();
    }catch(Exception e){
        logger.error(e);
    } finally {
        session.close();
    }
    return;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉城メ夜风 2024-10-14 00:29:10

myISAM 不是事务性的。默认情况下,自动提交处于启用状态(实际上 JDBC 驱动程序会忽略它,因为每个语句都会提交)。 innoDB 是事务性的,自动提交默认也是关闭的。这意味着您必须调用 session.commit() ,否则数据库永远不会真正执行更新。

请参阅 此博客条目了解更多信息。

请注意,您应该调用提交而不是将事情留给自动提交。关闭自动提交会导致连接池出现问题,因为当重用连接时,它会使语句处于未知状态。

myISAM is not transactional. Autocommit is on by default (actually it's ignored by the JDBC driver since every statement commits). innoDB is transactional and autocommit is also off by default. This means you have to call session.commit() or the DB never actually does the update.

See this blog entry for more information.

Note that you should call commit rather than leave things up to autocommit. Leaving autocommit off will cause problems with connection pooling since it can leave statements in an unknown state when the connection is reused.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文