Spring Data JPA消耗了所有的连接对象。

huangapple go评论45阅读模式
英文:

Spring data JPA consumes all the connection objects

问题

I have a scenario where in data is uploaded from excel sheet to mysql db. I am using spring data jpa. And the service calls the entities recursively after stuffing them with data taken from excel sheet to save in db. This creates "unable to acquire jdbc connections" after a certain load.

我有一个情景,从Excel表格上传数据到MySQL数据库。我正在使用Spring Data JPA。服务在从Excel表格中获取数据后,递归调用实体以保存在数据库中。这在一定负载之后会导致"无法获取JDBC连接"的问题。

I tried with @Transactional to know advantage. Then I am thinking of using EntityManager manually in code and control transaction boundary so that all recursive save calls of entities happen within one transaction and thereby one connection object. I just wanted to check would it be a nice idea or is there any other approach I should take which is more performant. Needless to say anyhow I have to do it through entities.

我尝试使用@Transactional来了解优势。然后,我在代码中考虑手动使用EntityManager,并控制事务边界,以便所有实体的递归保存调用都在一个事务内进行,从而只使用一个连接对象。我只是想检查这是否是一个好主意,或者是否有其他更高性能的方法。毫无疑问,我必须通过实体来完成。

英文:

I have a scenario where in data is uploaded from excel sheet to mysql db. I am using spring data jpa. And the service calls the entities recursively after stuffing them with data taken from excel sheet to save in db. This creates "unable to acquire jdbc connections" after a certain load.

I tried with @Transactional to know advantage. Then I am thinking of using EntityManager manually in code and control transaction boundary so that all recursive save calls of entities happen within one transaction and thereby one connection object. I just wanted to check would it be a nice idea or is there any other approach I should take which is more performant. Needless to say anyhow I have to do it through entities.

答案1

得分: 2

我的回答完全基于一种假设,即实现要求的方式存在问题,因为在问题中没有分享任何代码。

按照你的方法,是的,你会耗尽连接,因为实体的创建速度肯定比将实体持久化到数据库中快得多,而且由于你是递归执行的,如果数据量很大,你的应用程序将在某个时候耗尽连接,数字肯定是一个因素。

我更喜欢的另一种方法是,你可以准备好你的实体(假设所有数据都属于一个通用的实体类),并将其存储在一个集合中,一旦准备好,你可以使用saveAll()方法一次性将所有实体持久化。

如果数据不是针对通用实体的,你可以创建多个不同实体的列表,并在处理Excel表格后启动数据库操作。

英文:

My answer is completely based on the assumption that the way of implementing the requirement is faulty as there isn't any code shared in the question.

By your approach, yes you will run out of the connection as the entity population would surely be much faster than persisting that entity in the database and since you are doing it recursively your application will run out connections at one point of time if the amount of the data is very high, numbers are certainly a factor here.

The other approach I would prefer is that you can prepare your entities(Assuming all the data is for a common entity class) and store in a collection, once it is ready you can persist all of it in one transaction using saveAll() method.
If the data is not for common entities you can create multiple lists of different entities and initiate the DB operations after processing the excel sheet.

huangapple
  • 本文由 发表于 2020年8月3日 13:04:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63224002.html
匿名

发表评论

匿名网友

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

确定