Spring JPA使用EntityManager保存时的性能问题

huangapple 未分类评论52阅读模式
英文:

Spring JPA performance issue when saving with EntityManager

问题

我对Spring JPA还不太熟悉,如果我做错了什么,请您谅解。

我有一个用于执行一些本地查询的Repository DAO:

@Repository
public class TestingDAO {
    @Autowired
    private EntityManager entityManager;

    public void createNewFoos(Long fooId, Long barId) {
        if (ebuId == null) return;
        String insertQuery = "INSERT INTO FOO_BAR(foo_id, bar_id) values (" + fooId + "," + barId + ")";
        Query query = entityManager.createNativeQuery(insertQuery);
        query.executeUpdate();
    }
}

FOO_BAR是一个有两个外键的关系表。
我注意到在同一个事务中多次调用createNewFoos方法时,其执行时间会不断增加(在第10,000次时,甚至需要几秒钟)。当我使用JPA Repository保存实体对象(数据库中的结果相同)时,并没有出现这样的性能问题。

您能解释一下为什么会发生这种情况吗?是我做错了什么吗?

提前感谢您的帮助!

英文:

I'm new to Spring JPA, bear with me if I did something wrong.

I have a Repository DAO for executing some native query:

@Repository
public class TestingDAO {
    @Autowired
    private EntityManager entityManager;

    public void createNewFoos(Long fooId, Long barId) {
        if (ebuId == null) return;
        String insertQuery = "INSERT INTO FOO_BAR(foo_id, bar_id) values (" + fooId + "," + barId + ")";
        Query query = entityManager.createNativeQuery(insertQuery);
        query.executeUpdate();
    }
}

FOO_BAR is a relation table with 2 FK.
I realized that the execution time of createNewFoos method keeps increasing when I call it multiple times in one transaction (for 10,000 th, it even takes some seconds). When I use JPA Repository to save the entity object (result in db is the same), there is no performance issue like that.

Could you please explain why does it happen? Am I did something wrong?

Thanks in advance for you helps!

huangapple
  • 本文由 发表于 2020年4月8日 01:51:43
  • 转载请务必保留本文链接:https://java.coder-hub.com/61086284.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定