<div dir="ltr">@msavy - After looking at the code a bit I have no idea why hibernate is doing this. :(  I can&#39;t see anything in there that would be problematic based on my current level of understanding of hibernate.  That said, the root of the issue is here:<div><br></div><div><a href="https://github.com/apiman/apiman/blob/master/manager/api/rest-impl/src/main/java/io/apiman/manager/api/rest/impl/OrganizationResourceImpl.java#L3446-L3451">https://github.com/apiman/apiman/blob/master/manager/api/rest-impl/src/main/java/io/apiman/manager/api/rest/impl/OrganizationResourceImpl.java#L3446-L3451</a><br></div><div><br></div><div>Clearly no Tx is required to simply read data, so I presume the .beginTx() and .commitTx() calls are not required.  Without that, no data would ever get written back to the DB (which in this case is what we want).  Of course, that&#39;s only true of JPA and not necessarily every storage layer.  I think perhaps we have two choices:</div><div><br></div><div>1) Remove the .beginTx() and .commitTx() calls for ALL read-only operations.  This should work for both existing storage mechanisms now (JPA and ES[1]).</div><div>2) Add a flag to IStorage::beginTx() to indicate whether it should be read-only or not:  </div><div><br></div><div>        public void beginTx(boolean readOnly) throws StorageException;</div><div><br></div><div>For #2, the JPA implementation would simply *not* create a transaction if the readOnly flag was true.</div><div><br></div><div>Lastly, we should probably *also* add a unique constraint to the pd_templates table.  :)</div><div><br></div><div>-Eric</div><div><br></div><div><br></div><div>[1] - the Elasticsearch implementation ignores the Tx related calls because it does not support transactions</div><div><br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Jun 6, 2017 at 8:05 AM, Eric Wittmann <span dir="ltr">&lt;<a href="mailto:eric.wittmann@redhat.com" target="_blank">eric.wittmann@redhat.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Great thanks!  Now we just need to figure out why the heck that would happen...<div><br></div><div>Perhaps the ORM gods are angry.  An offering will be required.</div><div><br></div><div>Note: I wonder what would happen if we put a unique constraint on pd_templates (combination of id and lang columns).  That could be a workaround until we found the root cause.</div><span class="HOEnZb"><font color="#888888"><div><br></div><div>-Eric</div></font></span></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Jun 6, 2017 at 2:37 AM, Andy Yar <span dir="ltr">&lt;<a href="mailto:andyyar66@gmail.com" target="_blank">andyyar66@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I&#39;ve enabled logging of org.hibernate.SQL. Now I can see inserts to<br>
the &quot;pd_templates&quot; in my server.log:<br>
<a href="https://hastebin.com/zovefusoyo.sql" rel="noreferrer" target="_blank">https://hastebin.com/zovefusoy<wbr>o.sql</a>. These are triggered when browsing<br>
an OAuth policy.<br>
<br>
The &quot;policydefs&quot; table seems to be OK - only a single record for the<br>
Keycloak OAuth.<br>
<div class="m_-6118064526170842431HOEnZb"><div class="m_-6118064526170842431h5"><br>
On Fri, Jun 2, 2017 at 2:50 PM, Eric Wittmann &lt;<a href="mailto:eric.wittmann@redhat.com" target="_blank">eric.wittmann@redhat.com</a>&gt; wrote:<br>
&gt; This is a very strange problem.  I&#39;ll add some technical info to this<br>
&gt; discussion in case it helps.<br>
&gt;<br>
&gt; The &quot;pd_templates&quot; table stores potentially multiple html templates for<br>
&gt; displaying the *summary* of a configured instance of a policy.  It can store<br>
&gt; multiple templates by language - this is basically an  i18n feature.  In<br>
&gt; practice, we only really have English language versions, so there should<br>
&gt; only be one row in that table for each row in the &quot;policydefs&quot; table.  The<br>
&gt; &quot;policydefs&quot; table should have one row per policy definition (basically one<br>
&gt; row for each item in the Policy Type drop-down when adding a new policy to<br>
&gt; an API).<br>
&gt;<br>
&gt; The ORM class for the pd_templates table is here:<br>
&gt;<br>
&gt; <a href="https://github.com/apiman/apiman/blob/master/manager/api/beans/src/main/java/io/apiman/manager/api/beans/policies/PolicyDefinitionTemplateBean.java" rel="noreferrer" target="_blank">https://github.com/apiman/apim<wbr>an/blob/master/manager/api/<wbr>beans/src/main/java/io/apiman/<wbr>manager/api/beans/policies/Pol<wbr>icyDefinitionTemplateBean.java</a><br>
&gt;<br>
&gt; And the policy definition ORM bean is here:<br>
&gt;<br>
&gt; <a href="https://github.com/apiman/apiman/blob/master/manager/api/beans/src/main/java/io/apiman/manager/api/beans/policies/PolicyDefinitionBean.java" rel="noreferrer" target="_blank">https://github.com/apiman/apim<wbr>an/blob/master/manager/api/<wbr>beans/src/main/java/io/apiman/<wbr>manager/api/beans/policies/<wbr>PolicyDefinitionBean.java</a><br>
&gt;<br>
&gt; This sounds like hibernate making a mess of things (or more likely my<br>
&gt; mis-understanding of something hibernate is doing).  It&#39;s one of the reasons<br>
&gt; I&#39;m on the fence about ORM technologies in general.  They can be so helpful,<br>
&gt; but if you wrote your own SQL statements you&#39;d never have a weird issue like<br>
&gt; this.<br>
&gt;<br>
&gt; In any case, at this point I have no theory about how this could be<br>
&gt; happening.<br>
&gt;<br>
&gt; I assume your &quot;policydefs&quot; table is sensible.  So the question is why<br>
&gt; hibernate would be duplicating the data in &quot;pd_templates&quot;, especially when<br>
&gt; doing a simple GET of the information (shouldn&#39;t be mutating anything!).<br>
&gt;<br>
&gt; Could try enabling SQL logging in hibernate to see what&#39;s going on.<br>
&gt;<br>
&gt; But of course trying to replicate the problem locally is always the best<br>
&gt; approach. :)<br>
&gt;<br>
&gt; -Eric<br>
&gt;<br>
&gt;<br>
&gt; On Fri, Jun 2, 2017 at 7:49 AM, Marc Savy &lt;<a href="mailto:marc.savy@redhat.com" target="_blank">marc.savy@redhat.com</a>&gt; wrote:<br>
&gt;&gt;<br>
&gt;&gt; Hi Andy,<br>
&gt;&gt;<br>
&gt;&gt; Can you describe how your API, ClientApp &amp; Plan is set up?<br>
&gt;&gt;<br>
&gt;&gt; e.g.<br>
&gt;&gt;<br>
&gt;&gt; API = [Keycloak OAuth2 Policy]<br>
&gt;&gt; Plan = [&lt;nothing&gt;]<br>
&gt;&gt; ClientApp = [Header Policy]<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; Regards,<br>
&gt;&gt; Marc<br>
&gt;&gt;<br>
&gt;&gt; On 2 June 2017 at 12:20, Andy Yar &lt;<a href="mailto:andyyar66@gmail.com" target="_blank">andyyar66@gmail.com</a>&gt; wrote:<br>
&gt;&gt; &gt; Oh, sorry I missed version info.<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; psql (PostgreSQL) 9.4.12<br>
&gt;&gt; &gt; JDBC connector - PostgreSQL 9.4.1212<br>
&gt;&gt; &gt; Apiman 1.2.9.Final<br>
&gt;&gt; &gt; CentOS7<br>
&gt;&gt; &gt; Oracle Java 1.8.0_111<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; No export/import was done prior this issue.<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; I can say I&#39;ve been observed slower and slower responses during<br>
&gt;&gt; &gt; operation with the Manager OAuth related pages.<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; The trigger action seems to be a GET on a URL pattern like this:<br>
&gt;&gt; &gt; apimanui/api-manager/orgs/MyOr<wbr>g/plans/MyOrgKeycloakOAuth/1/<wbr>policies/1155<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; Resulting screen loads a policy config scheme from Keycloak OAuth<br>
&gt;&gt; &gt; Plugin and displays it. It also generates N^2 new rows in<br>
&gt;&gt; &gt; pd_templates. I&#39;ve observed 16, 32, 64, 128 and then 256 added new<br>
&gt;&gt; &gt; rows...<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; Thanks<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; On Fri, Jun 2, 2017 at 12:26 PM, Marc Savy &lt;<a href="mailto:marc.savy@redhat.com" target="_blank">marc.savy@redhat.com</a>&gt; wrote:<br>
&gt;&gt; &gt;&gt; Hi Andy,<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; I spent time yesterday evening trying to replicate this after our chat<br>
&gt;&gt; &gt;&gt; on IRC, but I haven&#39;t been able to trigger it. Perhaps with more<br>
&gt;&gt; &gt;&gt; information we can narrow this down.<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; Which version of Postgres are you using? Which driver version?<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; I&#39;ve been using: Postgres 9.6.2, Driver 42.1.1, apiman 1.3.0.Final<br>
&gt;&gt; &gt;&gt; (should be same as 1.2.9.Final for this).<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; Have you done any export-import?<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; Have you observed the precise action that corresponds with the<br>
&gt;&gt; &gt;&gt; extraneous pd_templates entries being inserted?<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; Regards,<br>
&gt;&gt; &gt;&gt; Marc<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; On 2 June 2017 at 08:05, Andy Yar &lt;<a href="mailto:andyyar66@gmail.com" target="_blank">andyyar66@gmail.com</a>&gt; wrote:<br>
&gt;&gt; &gt;&gt;&gt;<br>
&gt;&gt; &gt;&gt;&gt; Hello,<br>
&gt;&gt; &gt;&gt;&gt; Keycloak OAuth plugin&#39;s presence generates a certain amount of records<br>
&gt;&gt; &gt;&gt;&gt; to pd_templates table on each action of Apiman Manager related to a<br>
&gt;&gt; &gt;&gt;&gt; Keycloak OAuth policy (even read-only ones like listing a plan&#39;s<br>
&gt;&gt; &gt;&gt;&gt; policies, etc.).<br>
&gt;&gt; &gt;&gt;&gt;<br>
&gt;&gt; &gt;&gt;&gt; Over time the number of records in pd_templates table can grow to<br>
&gt;&gt; &gt;&gt;&gt; milions resulting in Apiman Manager OoM exceptions. Given these<br>
&gt;&gt; &gt;&gt;&gt; records are basically just text hints it is really funny.<br>
&gt;&gt; &gt;&gt;&gt;<br>
&gt;&gt; &gt;&gt;&gt; A workaround is to periodically dedup the records.<br>
&gt;&gt; &gt;&gt;&gt;<br>
&gt;&gt; &gt;&gt;&gt; I guess this issue should be an easy fix.<br>
&gt;&gt; &gt;&gt;&gt;<br>
&gt;&gt; &gt;&gt;&gt; Affected version is: Apiman 1.2.9.Final + corresponding Keycloak OAuth<br>
&gt;&gt; &gt;&gt;&gt; plugin.<br>
&gt;&gt; &gt;&gt;&gt; ______________________________<wbr>_________________<br>
&gt;&gt; &gt;&gt;&gt; Apiman-user mailing list<br>
&gt;&gt; &gt;&gt;&gt; <a href="mailto:Apiman-user@lists.jboss.org" target="_blank">Apiman-user@lists.jboss.org</a><br>
&gt;&gt; &gt;&gt;&gt; <a href="https://lists.jboss.org/mailman/listinfo/apiman-user" rel="noreferrer" target="_blank">https://lists.jboss.org/mailma<wbr>n/listinfo/apiman-user</a><br>
&gt;&gt; ______________________________<wbr>_________________<br>
&gt;&gt; Apiman-user mailing list<br>
&gt;&gt; <a href="mailto:Apiman-user@lists.jboss.org" target="_blank">Apiman-user@lists.jboss.org</a><br>
&gt;&gt; <a href="https://lists.jboss.org/mailman/listinfo/apiman-user" rel="noreferrer" target="_blank">https://lists.jboss.org/mailma<wbr>n/listinfo/apiman-user</a><br>
&gt;<br>
&gt;<br>
</div></div></blockquote></div><br></div>
</div></div></blockquote></div><br></div>