<div dir="ltr">thank you Stian.<div><br></div><div>I have found my error. I was using a JTA transaction provider and changed that to a local one and it is now working.</div><div><br></div><div>Just in case anyone is interested this is how my persistence.xml is :</div><div><br></div><div><div>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</div><div>&lt;persistence version=&quot;2.1&quot; xmlns=&quot;<a href="http://xmlns.jcp.org/xml/ns/persistence">http://xmlns.jcp.org/xml/ns/persistence</a>&quot; xmlns:xsi=&quot;<a href="http://www.w3.org/2001/XMLSchema-instance">http://www.w3.org/2001/XMLSchema-instance</a>&quot;</div><div><span class="" style="white-space:pre">        </span>xsi:schemaLocation=&quot;<a href="http://xmlns.jcp.org/xml/ns/persistence">http://xmlns.jcp.org/xml/ns/persistence</a> <a href="http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd</a>&quot;&gt;</div><div><span class="" style="white-space:pre">        </span>&lt;persistence-unit name=&quot;security&quot; transaction-type=&quot;RESOURCE_LOCAL&quot;&gt;</div><div><span class="" style="white-space:pre">                </span>&lt;jta-data-source&gt;java:jboss/datasources/security&lt;/jta-data-source&gt;</div><div><span class="" style="white-space:pre">                </span>&lt;properties&gt;<br></div><div><span class="" style="white-space:pre">                        </span>&lt;property name=&quot;hibernate.hbm2ddl.auto&quot; value=&quot;validate&quot; /&gt;</div><div><span class="" style="white-space:pre">                        </span>&lt;property name=&quot;hibernate.show_sql&quot; value=&quot;true&quot; /&gt;</div><div><span class="" style="white-space:pre">                        </span>&lt;property name=&quot;hibernate.format_sql&quot; value=&quot;true&quot; /&gt;</div><div>            &lt;!-- Set jboss.as.jpa.managed to false to disable container managed JPA access to the persistence unit --&gt;</div><div>            &lt;property name=&quot;jboss.as.jpa.managed&quot; value=&quot;false&quot;/&gt;</div><div><span class="" style="white-space:pre">                </span>&lt;/properties&gt;<br></div><div><span class="" style="white-space:pre">        </span>&lt;/persistence-unit&gt;</div><div>&lt;/persistence&gt;</div></div><div><br></div><div>And this how i create my entity manager to handle the transaction :</div><div><br></div><div><div>EntityManagerFactory emf = Persistence.createEntityManagerFactory(&quot;security&quot;);</div><div>EntityManager em = emf.createEntityManager();</div></div><div>em.getTransaction().begin();<br></div><div>// some code to save data to the db</div><div>em.getTransaction().commit();<br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Nov 28, 2014 at 11:10 AM, Stian Thorgersen <span dir="ltr">&lt;<a href="mailto:stian@redhat.com" target="_blank">stian@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">No, there&#39;s no example for this, but you can look at the Keycloak source as we use JPA ourselves.<br>
<br>
You have two options:<br>
<br>
a) Shared EntityManager instance with internal Keycloak providers - if you&#39;re using the same db for both this is a good option<br>
b) Separate EntityManager instance<br>
<br>
For option a) you should look at extending the default connectionJpa provider to add your own persistence.xml (you need to copy/modify Keycloak&#39;s persistence.xml). Look at <a href="https://github.com/keycloak/keycloak/tree/master/connections/jpa/src/main/java/org/keycloak/connections/jpa" target="_blank">https://github.com/keycloak/keycloak/tree/master/connections/jpa/src/main/java/org/keycloak/connections/jpa</a> and <a href="https://github.com/keycloak/keycloak/tree/master/model/jpa" target="_blank">https://github.com/keycloak/keycloak/tree/master/model/jpa</a>.<br>
<br>
For option b) you can just have your ProviderFactory create the EntityManagerFactory and pass it to the Provider instance. For this you only need to look at <a href="https://github.com/keycloak/keycloak/tree/master/connections/jpa/src/main/java/org/keycloak/connections/jpa" target="_blank">https://github.com/keycloak/keycloak/tree/master/connections/jpa/src/main/java/org/keycloak/connections/jpa</a>.<br>
<span class="im HOEnZb"><br>
----- Original Message -----<br>
&gt; From: &quot;Alexander Chriztopher&quot; &lt;<a href="mailto:alexander.chriztopher@gmail.com">alexander.chriztopher@gmail.com</a>&gt;<br>
</span><div class="HOEnZb"><div class="h5">&gt; To: &quot;Stian Thorgersen&quot; &lt;<a href="mailto:stian@redhat.com">stian@redhat.com</a>&gt;<br>
&gt; Cc: <a href="mailto:keycloak-user@lists.jboss.org">keycloak-user@lists.jboss.org</a><br>
&gt; Sent: Friday, 28 November, 2014 11:01:46 AM<br>
&gt; Subject: Re: [keycloak-user] Providers with CDI<br>
&gt;<br>
&gt; ok ! Are there any examples of using JPA ?<br>
&gt;<br>
&gt; On Fri, Nov 28, 2014 at 10:54 AM, Stian Thorgersen &lt;<a href="mailto:stian@redhat.com">stian@redhat.com</a>&gt; wrote:<br>
&gt;<br>
&gt; &gt; No, afraid we don&#39;t support CDI (or any other managed features such as<br>
&gt; &gt; EJBs) in our providers. They are just POJO&#39;s and Keycloak manages their<br>
&gt; &gt; life-cycle.<br>
&gt; &gt;<br>
&gt; &gt; In the future we may make it simpler to create providers, but this is not<br>
&gt; &gt; a priority atm.<br>
&gt; &gt;<br>
&gt; &gt; ----- Original Message -----<br>
&gt; &gt; &gt; From: &quot;Alexander Chriztopher&quot; &lt;<a href="mailto:alexander.chriztopher@gmail.com">alexander.chriztopher@gmail.com</a>&gt;<br>
&gt; &gt; &gt; To: <a href="mailto:keycloak-user@lists.jboss.org">keycloak-user@lists.jboss.org</a><br>
&gt; &gt; &gt; Sent: Friday, 28 November, 2014 10:14:32 AM<br>
&gt; &gt; &gt; Subject: [keycloak-user] Providers with CDI<br>
&gt; &gt; &gt;<br>
&gt; &gt; &gt; Hi All,<br>
&gt; &gt; &gt;<br>
&gt; &gt; &gt; I there a way to get providers to be managed by CDI.<br>
&gt; &gt; &gt;<br>
&gt; &gt; &gt; The aim is to get access to services such as entity manager injection and<br>
&gt; &gt; &gt; lifecycle management which would be done manually today.<br>
&gt; &gt; &gt;<br>
&gt; &gt; &gt; Thanks for any help.<br>
&gt; &gt; &gt;<br>
&gt; &gt; &gt; _______________________________________________<br>
&gt; &gt; &gt; keycloak-user mailing list<br>
&gt; &gt; &gt; <a href="mailto:keycloak-user@lists.jboss.org">keycloak-user@lists.jboss.org</a><br>
&gt; &gt; &gt; <a href="https://lists.jboss.org/mailman/listinfo/keycloak-user" target="_blank">https://lists.jboss.org/mailman/listinfo/keycloak-user</a><br>
&gt; &gt;<br>
&gt;<br>
</div></div></blockquote></div><br></div>