<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <title>Degraded performance of service call writing many items in document library</title>
  <link rel="self" href="https://liferay.dev/c/message_boards/find_thread?p_l_id=119785294&amp;threadId=120683281" />
  <subtitle>Degraded performance of service call writing many items in document library</subtitle>
  <id>https://liferay.dev/c/message_boards/find_thread?p_l_id=119785294&amp;threadId=120683281</id>
  <updated>2026-06-15T06:07:40Z</updated>
  <dc:date>2026-06-15T06:07:40Z</dc:date>
  <entry>
    <title>Degraded performance of service call writing many items in document library</title>
    <link rel="alternate" href="https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=120683280" />
    <author>
      <name>Jan Tošovský</name>
    </author>
    <id>https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=120683280</id>
    <updated>2021-03-11T17:07:31Z</updated>
    <published>2021-03-11T10:35:52Z</published>
    <summary type="html">&lt;p&gt;In my module I import ZIP packages containg HTML files and their
  resources. My portlet/scheduler code takes ZIP packages one by one and
  call the custom service method which stores HTML content in database
  and resources in Document Library.&lt;/p&gt;
&lt;p&gt;I found if this loop is moved directly to the service, the process is
  continuously slowed down with every iteration. I suppose data is
  written only after the service call succeeds, so when the loop is
  directly in the service method, all data are acummulated in some
  temporary storage what makes the process slower and slower.&lt;/p&gt;
&lt;p&gt;I found it is especially apparent for operation with lot of resources
  so I assume the problems is somewhere in core
  LR &lt;code&gt;PortletFileRepositoryUtil.addPortletFileEntry()&lt;/code&gt; method.&lt;/p&gt;
&lt;p&gt;Here are durations when importing 13 different entries, but in same
  order using both approaches:&lt;/p&gt;
&lt;p&gt;
  &lt;code&gt;fast vs. slow (minutes)&lt;br /&gt;=============&lt;br /&gt;0:17 vs.
    0:49&lt;br /&gt;1:10 vs. 2:31&lt;br /&gt;0:01 vs. 0:16&lt;br /&gt;0:00 vs.
    0:02&lt;br /&gt;1:31 vs. 5:41&lt;br /&gt;0:03 vs. 0:30&lt;br /&gt;1:16 vs.
    9:19&lt;br /&gt;0:01 vs. 0:07&lt;br /&gt;0:09 vs. 0:16&lt;br /&gt;0:07 vs.
    3:33&lt;br /&gt;0:11 vs. 0:33&lt;br /&gt;0:15 vs. 0:24&lt;br /&gt;0:03 vs. 0:07&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;The fast method is finished in 10 minutes, while the slow in 30
  minutes. It the number of entires is greater, the slowness is
  significant even more. &lt;/p&gt;
&lt;p&gt;Here are results when same package with 1000 resources is imported
  three times in a row (slow method):&lt;/p&gt;
&lt;p&gt;
  &lt;code&gt;0:39&lt;br /&gt;1:36 (+  57 s)&lt;br /&gt;2:29 (+ 110 s)&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;
  &lt;strong&gt;Fast variant:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Portlet/Scheduler/Rest App:&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-java"&gt;for (entry : entries) {
   myService.import(entry);
}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Service:&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-java"&gt;public void import(Entry entry) {
   addContentToDatabase(entry);
   addResourcesToDocumentLibrary(entry);
}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
  &lt;strong&gt;Slow variant:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Portlet/Scheduler/Rest App:&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-java"&gt;myService.importEntries(entries);&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Service:&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-java"&gt;public void importEntries(List&amp;lt;Entry&amp;gt; entries) {
   for (entry: entries) {
      import(entry);
   }
}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That delay comes from addPortletFileEntry method:&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-java"&gt;try (InputStream inputStream = Files.newInputStream(fileSystem.getPath(resourcePath))) {
    String mimeType = MimeTypesUtil.getContentType(fileName);
    PortletFileRepositoryUtil.addPortletFileEntry(groupId, userId, className, classPK,
        portletId, folderId, inputStream, fileName, mimeType, false);
}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Is it possible to somehow keep the loop in service, but avoid that
  slowness? I've tried runing System.gc() in every iteration, but with
  no progress. &lt;/p&gt;
&lt;p&gt;This behavior I originally found in 6.2, but it is still present in
  7.3 versions. I use PostgreSQL database and standard file system for
  Document Library.&lt;/p&gt;</summary>
    <dc:creator>Jan Tošovský</dc:creator>
    <dc:date>2021-03-11T10:35:52Z</dc:date>
  </entry>
</feed>
