You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by vcheruvu <vi...@macquarie.com> on 2010/04/14 04:34:58 UTC

JPA component - EntityManagerFactory issue

Hi,

In my project, I am using JPA and using Hibernate vendor to poll tables.
Please see my config below.  I am trying to setup multiple persiste
http://old.nabble.com/file/p28237771/trace-jpa.txt trace-jpa.txt nce.xml
file in my project.

<bean>
<camel-context>
...
	<route>
			<from
uri="jpa:com.MyEntity?consumer.namedQuery=pollRecords&amp;consumeDelete=false&amp;delay=1000&amp;consumer.useFixedDelay=true"/>
			<to uri="bean:rransformerBean?method=transformRecords"/>
		</route>
<camel-context/>
 <bean id="jpaTemplate" class="org.springframework.orm.jpa.JpaTemplate">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
  </bean>
<bean id="myPersistence"
class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
  <property name="persistenceXmlLocations">
    <list>
      <value>classpath*:META-INF/spring/custom/persistence.xml</value>
    </list>
  </property>
 <property name="loadTimeWeaver">
 	<bean
class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
 </property>
</bean>
 <bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
	<property name="persistenceUnitName" value="camel"/>
 	<property name="persistenceUnitManager" ref="myPersistence" />
 	<property name="jpaVendorAdapter">
 		<bean
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
 		 <property name="showSql" value="false"/>
 		</bean>
 	</property>

</bean>
I am using Spring config to setup the multiple peristence xml location, you
can see the example
http://docs.huihoo.com/spring/2.5.x/en-us/orm.html#orm-jpa-setup-lcemfb .  
When running the camel application I have come to know that  Spring has
successfully created entity
http://old.nabble.com/file/p28237771/trace-jpa.txt trace-jpa.txt
ManagerFactory and loaded correct persistence.xml file. However, when camel
starts, JPA endpoint tries to create/instantiate new entityManagerFactory
Object again instead of using existing entityManageFactory that was already
created by spring. In doing so, EJB3Configuration.configure()  is invoked by
Jpa endpoint throws an error stating "Could not find any
META-INF/persistence.xml file in the classpath". Why is camel JPA endpoint
invoking  Persistence.createEntityManagerFactory(persistenceUnit,
getEntityManagerProperties()); instead of reusing entityMangerFactory that
was instantiated by Spring? Is camel getting confused between
LocalEntityManagerFactory  and LocalContainerEntityManagerFactory ? I have
added trace logs of camel, spring and hibernate in the attachment to this
post. Hopefully it helps you explain my questions.

Kind regards,
-Vid-





-- 
View this message in context: http://old.nabble.com/JPA-component---EntityManagerFactory-issue-tp28237771p28237771.html
Sent from the Camel - Users mailing list archive at Nabble.com.


Re: JPA component - EntityManagerFactory issue

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

Yeah that is actually something we can do to improve.

I have created a ticket
https://issues.apache.org/activemq/browse/CAMEL-2647

And implemented it. Now it will auto lookup the EMF and TM from the registry.

You are most welcome to try out 2.3-SNAPSHOT and see if that works for
you as well.


On Wed, Apr 14, 2010 at 6:43 AM, vcheruvu <vi...@macquarie.com> wrote:
>
> Thanks that worked nicely. So if i didn't have this bean definition, Spring
> and Camel would have instantiated the entityManagerFactory twice. As per the
> jpa example in the Camel src, perhaps it would be good idea to add this bean
> as part of the spring based camel config example and avoid duplication of
> instantiating entityMangerFactory. You will notice that in the TRACE level
> logs for Camel JPA example.
>
>
>
> Claus Ibsen-2 wrote:
>>
>> Hi
>>
>> You can set the EntityManagerFactory on the JpaComponent, and Camel
>> will reuse this one.
>>
>> <bean id="jap" class="org...JpaComponent">
>>   <property name="entityManagerFactory" ref="myEntityFac"/>
>> </bean>
>>
>>
>> On Wed, Apr 14, 2010 at 4:34 AM, vcheruvu <vi...@macquarie.com>
>> wrote:
>>>
>>> Hi,
>>>
>>> In my project, I am using JPA and using Hibernate vendor to poll tables.
>>> Please see my config below.  I am trying to setup multiple persiste
>>> http://old.nabble.com/file/p28237771/trace-jpa.txt trace-jpa.txt nce.xml
>>> file in my project.
>>>
>>> <bean>
>>> <camel-context>
>>> ...
>>>        <route>
>>>                        <from
>>> uri="jpa:com.MyEntity?consumer.namedQuery=pollRecords&amp;consumeDelete=false&amp;delay=1000&amp;consumer.useFixedDelay=true"/>
>>>                        <to
>>> uri="bean:rransformerBean?method=transformRecords"/>
>>>                </route>
>>> <camel-context/>
>>>  <bean id="jpaTemplate" class="org.springframework.orm.jpa.JpaTemplate">
>>>    <property name="entityManagerFactory" ref="entityManagerFactory"/>
>>>  </bean>
>>> <bean id="myPersistence"
>>> class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
>>>  <property name="persistenceXmlLocations">
>>>    <list>
>>>      <value>classpath*:META-INF/spring/custom/persistence.xml</value>
>>>    </list>
>>>  </property>
>>>  <property name="loadTimeWeaver">
>>>        <bean
>>> class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
>>>  </property>
>>> </bean>
>>>  <bean id="entityManagerFactory"
>>> class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
>>>        <property name="persistenceUnitName" value="camel"/>
>>>        <property name="persistenceUnitManager" ref="myPersistence" />
>>>        <property name="jpaVendorAdapter">
>>>                <bean
>>> class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
>>>                 <property name="showSql" value="false"/>
>>>                </bean>
>>>        </property>
>>>
>>> </bean>
>>> I am using Spring config to setup the multiple peristence xml location,
>>> you
>>> can see the example
>>> http://docs.huihoo.com/spring/2.5.x/en-us/orm.html#orm-jpa-setup-lcemfb .
>>> When running the camel application I have come to know that  Spring has
>>> successfully created entity
>>> http://old.nabble.com/file/p28237771/trace-jpa.txt trace-jpa.txt
>>> ManagerFactory and loaded correct persistence.xml file. However, when
>>> camel
>>> starts, JPA endpoint tries to create/instantiate new entityManagerFactory
>>> Object again instead of using existing entityManageFactory that was
>>> already
>>> created by spring. In doing so, EJB3Configuration.configure()  is invoked
>>> by
>>> Jpa endpoint throws an error stating "Could not find any
>>> META-INF/persistence.xml file in the classpath". Why is camel JPA
>>> endpoint
>>> invoking  Persistence.createEntityManagerFactory(persistenceUnit,
>>> getEntityManagerProperties()); instead of reusing entityMangerFactory
>>> that
>>> was instantiated by Spring? Is camel getting confused between
>>> LocalEntityManagerFactory  and LocalContainerEntityManagerFactory ? I
>>> have
>>> added trace logs of camel, spring and hibernate in the attachment to this
>>> post. Hopefully it helps you explain my questions.
>>>
>>> Kind regards,
>>> -Vid-
>>>
>>>
>>>
>>>
>>>
>>> --
>>> View this message in context:
>>> http://old.nabble.com/JPA-component---EntityManagerFactory-issue-tp28237771p28237771.html
>>> Sent from the Camel - Users mailing list archive at Nabble.com.
>>>
>>>
>>
>>
>>
>> --
>> Claus Ibsen
>> Apache Camel Committer
>>
>> Author of Camel in Action: http://www.manning.com/ibsen/
>> Open Source Integration: http://fusesource.com
>> Blog: http://davsclaus.blogspot.com/
>> Twitter: http://twitter.com/davsclaus
>>
>>
>
> --
> View this message in context: http://old.nabble.com/JPA-component---EntityManagerFactory-issue-tp28237771p28238297.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>
>



-- 
Claus Ibsen
Apache Camel Committer

Author of Camel in Action: http://www.manning.com/ibsen/
Open Source Integration: http://fusesource.com
Blog: http://davsclaus.blogspot.com/
Twitter: http://twitter.com/davsclaus

Re: JPA component - EntityManagerFactory issue

Posted by vcheruvu <vi...@macquarie.com>.
Thanks that worked nicely. So if i didn't have this bean definition, Spring
and Camel would have instantiated the entityManagerFactory twice. As per the
jpa example in the Camel src, perhaps it would be good idea to add this bean
as part of the spring based camel config example and avoid duplication of
instantiating entityMangerFactory. You will notice that in the TRACE level
logs for Camel JPA example.  



Claus Ibsen-2 wrote:
> 
> Hi
> 
> You can set the EntityManagerFactory on the JpaComponent, and Camel
> will reuse this one.
> 
> <bean id="jap" class="org...JpaComponent">
>   <property name="entityManagerFactory" ref="myEntityFac"/>
> </bean>
> 
> 
> On Wed, Apr 14, 2010 at 4:34 AM, vcheruvu <vi...@macquarie.com>
> wrote:
>>
>> Hi,
>>
>> In my project, I am using JPA and using Hibernate vendor to poll tables.
>> Please see my config below.  I am trying to setup multiple persiste
>> http://old.nabble.com/file/p28237771/trace-jpa.txt trace-jpa.txt nce.xml
>> file in my project.
>>
>> <bean>
>> <camel-context>
>> ...
>>        <route>
>>                        <from
>> uri="jpa:com.MyEntity?consumer.namedQuery=pollRecords&amp;consumeDelete=false&amp;delay=1000&amp;consumer.useFixedDelay=true"/>
>>                        <to
>> uri="bean:rransformerBean?method=transformRecords"/>
>>                </route>
>> <camel-context/>
>>  <bean id="jpaTemplate" class="org.springframework.orm.jpa.JpaTemplate">
>>    <property name="entityManagerFactory" ref="entityManagerFactory"/>
>>  </bean>
>> <bean id="myPersistence"
>> class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
>>  <property name="persistenceXmlLocations">
>>    <list>
>>      <value>classpath*:META-INF/spring/custom/persistence.xml</value>
>>    </list>
>>  </property>
>>  <property name="loadTimeWeaver">
>>        <bean
>> class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
>>  </property>
>> </bean>
>>  <bean id="entityManagerFactory"
>> class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
>>        <property name="persistenceUnitName" value="camel"/>
>>        <property name="persistenceUnitManager" ref="myPersistence" />
>>        <property name="jpaVendorAdapter">
>>                <bean
>> class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
>>                 <property name="showSql" value="false"/>
>>                </bean>
>>        </property>
>>
>> </bean>
>> I am using Spring config to setup the multiple peristence xml location,
>> you
>> can see the example
>> http://docs.huihoo.com/spring/2.5.x/en-us/orm.html#orm-jpa-setup-lcemfb .
>> When running the camel application I have come to know that  Spring has
>> successfully created entity
>> http://old.nabble.com/file/p28237771/trace-jpa.txt trace-jpa.txt
>> ManagerFactory and loaded correct persistence.xml file. However, when
>> camel
>> starts, JPA endpoint tries to create/instantiate new entityManagerFactory
>> Object again instead of using existing entityManageFactory that was
>> already
>> created by spring. In doing so, EJB3Configuration.configure()  is invoked
>> by
>> Jpa endpoint throws an error stating "Could not find any
>> META-INF/persistence.xml file in the classpath". Why is camel JPA
>> endpoint
>> invoking  Persistence.createEntityManagerFactory(persistenceUnit,
>> getEntityManagerProperties()); instead of reusing entityMangerFactory
>> that
>> was instantiated by Spring? Is camel getting confused between
>> LocalEntityManagerFactory  and LocalContainerEntityManagerFactory ? I
>> have
>> added trace logs of camel, spring and hibernate in the attachment to this
>> post. Hopefully it helps you explain my questions.
>>
>> Kind regards,
>> -Vid-
>>
>>
>>
>>
>>
>> --
>> View this message in context:
>> http://old.nabble.com/JPA-component---EntityManagerFactory-issue-tp28237771p28237771.html
>> Sent from the Camel - Users mailing list archive at Nabble.com.
>>
>>
> 
> 
> 
> -- 
> Claus Ibsen
> Apache Camel Committer
> 
> Author of Camel in Action: http://www.manning.com/ibsen/
> Open Source Integration: http://fusesource.com
> Blog: http://davsclaus.blogspot.com/
> Twitter: http://twitter.com/davsclaus
> 
> 

-- 
View this message in context: http://old.nabble.com/JPA-component---EntityManagerFactory-issue-tp28237771p28238297.html
Sent from the Camel - Users mailing list archive at Nabble.com.


Re: JPA component - EntityManagerFactory issue

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

You can set the EntityManagerFactory on the JpaComponent, and Camel
will reuse this one.

<bean id="jap" class="org...JpaComponent">
  <property name="entityManagerFactory" ref="myEntityFac"/>
</bean>


On Wed, Apr 14, 2010 at 4:34 AM, vcheruvu <vi...@macquarie.com> wrote:
>
> Hi,
>
> In my project, I am using JPA and using Hibernate vendor to poll tables.
> Please see my config below.  I am trying to setup multiple persiste
> http://old.nabble.com/file/p28237771/trace-jpa.txt trace-jpa.txt nce.xml
> file in my project.
>
> <bean>
> <camel-context>
> ...
>        <route>
>                        <from
> uri="jpa:com.MyEntity?consumer.namedQuery=pollRecords&amp;consumeDelete=false&amp;delay=1000&amp;consumer.useFixedDelay=true"/>
>                        <to uri="bean:rransformerBean?method=transformRecords"/>
>                </route>
> <camel-context/>
>  <bean id="jpaTemplate" class="org.springframework.orm.jpa.JpaTemplate">
>    <property name="entityManagerFactory" ref="entityManagerFactory"/>
>  </bean>
> <bean id="myPersistence"
> class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
>  <property name="persistenceXmlLocations">
>    <list>
>      <value>classpath*:META-INF/spring/custom/persistence.xml</value>
>    </list>
>  </property>
>  <property name="loadTimeWeaver">
>        <bean
> class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
>  </property>
> </bean>
>  <bean id="entityManagerFactory"
> class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
>        <property name="persistenceUnitName" value="camel"/>
>        <property name="persistenceUnitManager" ref="myPersistence" />
>        <property name="jpaVendorAdapter">
>                <bean
> class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
>                 <property name="showSql" value="false"/>
>                </bean>
>        </property>
>
> </bean>
> I am using Spring config to setup the multiple peristence xml location, you
> can see the example
> http://docs.huihoo.com/spring/2.5.x/en-us/orm.html#orm-jpa-setup-lcemfb .
> When running the camel application I have come to know that  Spring has
> successfully created entity
> http://old.nabble.com/file/p28237771/trace-jpa.txt trace-jpa.txt
> ManagerFactory and loaded correct persistence.xml file. However, when camel
> starts, JPA endpoint tries to create/instantiate new entityManagerFactory
> Object again instead of using existing entityManageFactory that was already
> created by spring. In doing so, EJB3Configuration.configure()  is invoked by
> Jpa endpoint throws an error stating "Could not find any
> META-INF/persistence.xml file in the classpath". Why is camel JPA endpoint
> invoking  Persistence.createEntityManagerFactory(persistenceUnit,
> getEntityManagerProperties()); instead of reusing entityMangerFactory that
> was instantiated by Spring? Is camel getting confused between
> LocalEntityManagerFactory  and LocalContainerEntityManagerFactory ? I have
> added trace logs of camel, spring and hibernate in the attachment to this
> post. Hopefully it helps you explain my questions.
>
> Kind regards,
> -Vid-
>
>
>
>
>
> --
> View this message in context: http://old.nabble.com/JPA-component---EntityManagerFactory-issue-tp28237771p28237771.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>
>



-- 
Claus Ibsen
Apache Camel Committer

Author of Camel in Action: http://www.manning.com/ibsen/
Open Source Integration: http://fusesource.com
Blog: http://davsclaus.blogspot.com/
Twitter: http://twitter.com/davsclaus