|
|
|
Re: How to inject EntityManager in each bundle? [message #633162 is a reply to message #633093] |
Fri, 15 October 2010 14:27 |
Dmitry Sklyut Messages: 279 Registered: January 2010 |
Senior Member |
|
|
I would like to follow up on the "context-class-loader" property recommendation.
First lets start with how Spring DM deploys services.
All services are deployed (<osgi:service />) and imported (<osgi:reference/>) as proxy.
Spring DM does it for:
1. To manage lifecycle of the beans
2. To give Spring AppContext something concreate to hold to during start up. This allows Spring (static resolution) to work in OSGi (dynamic) environment even if service is not yet deployed in Service registry.
3. To manage TCCL.
By default Spring DM will use TCCL of the client with <osgi:reference />. But sometimes libraries used by the service provider (<osgi:service />) side make some assumptions about TCCL. In those cases it is helpful to managed TCCL. Note (from Spring DM docs):
Quote: |
context class loader management on the importer and exporter
Spring DM has the ability to do context class loader management on both the importer and exporter side. Normally, if Spring DM works on both sides, only one side should have this feature enabled. However, if both sides (importer and exporter) take advantage of this capability, the last entity in the call chain will win. This means that the exporter setting, if enabled, will always override the importer setting (whatever that is).
|
Back to the use case here.
If JPA/Hibernate/ORM solution is used in a scoped plan or a par - there is NO NEED to use TCCL management feature of Spring DM.
When Virgo creates a scoped deployment, TCCL is managed to include all referenced classes from plan/par. Given that, everything ORM tool is using internally can be found with TCCL.
But what about unscoped deployments? This is the use case that I have in the current application. In this situation one can run head first into CNF and other issues. Some ORM tools (ahem Hibernate) want to load everything under the sun through TCCL.
If <osgi:reference /> sets a TCCL to a "client" - than client must have visibility into ALL possible classes that ORM tool might use.
Examples can include:
1. HQL/JPAQL parsing with ANTLR
2. Proxy classes generated by the ORM framework on the fly or with code generation (javasist, cglib, etc)
So if the client only has imports for javax.persistence and does not import any of the framework supporting stuff - you might run into an issue.
This is where switching to service-provider TCCL might help.
This might sound like putting a lipstick on a pig, and basically means that the library you are using is not very OSGi friendly.
Hope this clears up the recommendation a bit.
I believe that recommended way of using ORM in Virgo is to use a scoped deployment. You will be better off in the long run.
|
|
|
|
Re: How to inject EntityManager in each bundle? [message #633732 is a reply to message #633047] |
Tue, 19 October 2010 07:31 |
guofeng zhang Messages: 49 Registered: July 2009 |
Member |
|
|
The EntityManagerFactory in org.planner.service.jpa bundle is published successfully, but when I tried to access it in other bundle (org.planner.service.m ember.management.impl), I got the following message. It is publihsed by
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="plannerDataSource" />
<property name="persistenceUnitName" value="planner" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
<property name="jpaProperties">
<props>
<prop key="eclipselink.session.customizer">org.planner.eclipselink.transaction.session.customizer.TransactionSessionCustomizer</prop>
</props>
</property>
</bean>
<osgi:service ref = "entityManagerFactory" auto-export="all-classes" context-class-loader="service-provider" />
That is, the option #1.
If I only changed this part to publish it using option #2 as the following in my application context implementation(in setApplicationContext()) in the jpa bundle:
String serviceName = javax.persistence.EntityManagerFactory.class.getName() ;
EntityManagerFactoryInfo emi = (EntityManagerFactoryInfo)appContext.getBean( "entityManagerFactory" ) ;
Activator.getBundleContext().registerService( serviceName,
emi.getNativeEntityManagerFactory(),
null);
My application works well: save data and load data from the database without problem.
What's the reason? Is it by final classes, or which classes should be imported?
Thanks for assistance.
[2010-10-19 11:01:35.345] region-dm-1 o.s.osgi.service.importer.support.OsgiServiceProxyFactoryBea n Looking for mandatory OSGi service dependency for bean [myTransactionManager] matching filter (objectClass=org.springframework.transaction.PlatformTransac tionManager)
[2010-10-19 11:01:35.345] region-dm-1 o.s.osgi.service.importer.support.OsgiServiceProxyFactoryBea n Found mandatory OSGi service for bean [myTransactionManager]
[2010-10-19 11:01:35.345] region-dm-1 o.s.osgi.service.exporter.support.OsgiServiceFactoryBean Publishing service under classes [{java.io.Serializable, java.lang.reflect.Proxy, javax.persistence.EntityManagerFactory, org.springframework.orm.jpa.EntityManagerFactoryInfo}]
[2010-10-19 11:01:35.345] region-dm-1 .a.d.i.BlueprintEventPostingOsgiBundleApplicationContextList ener Sending event to topic 'org/osgi/service/blueprint/container/GRACE_PERIOD' with properties '{bundle.version=1.0.0.201010191030, bundle=org.planner.service.member.management.impl_1.0.0.2010 10191030 [91], bundle.symbolicName=org.planner.service.member.management.im pl, type=6, timestamp=1287457295345, bundle.id=91}'
[2010-10-19 11:01:35.361] Thread-3 org.eclipse.virgo.kernel.core.internal.BundleStartTracker Handling event 'org.osgi.service.event.Event [topic=org/osgi/service/blueprint/container/GRACE_PERIOD]'
[2010-10-19 11:01:35.361] Thread-3 o.e.v.k.c.internal.blueprint.ApplicationContextDependencyMon itor Service dependency '(objectClass=javax.persistence.EntityManagerFactory) &entityManagerFactory' has been satisfied
[2010-10-19 11:01:35.361] region-dm-1 o.s.o.e.internal.dependencies.startup.DependencyServiceManag er No unsatisfied OSGi service dependencies; completing initialization for OsgiBundleXmlApplicationContext(bundle=org.planner.service.m ember.management.impl, config=osgibundle:/META-INF/spring/*.xml)
[2010-10-19 11:01:35.361] region-dm-1 o.s.osgi.context.support.OsgiBundleXmlApplicationContext Publishing application context as OSGi service with properties {org.springframework.context.service.name=org.planner.servic e.jpa, Bundle-SymbolicName=org.planner.service.jpa, Bundle-Version=1.0.0.201010191059}
[2010-10-19 11:01:35.361] region-dm-1 .a.d.i.BlueprintEventPostingOsgiBundleApplicationContextList ener Sending event to topic 'org/osgi/service/blueprint/container/CREATED' with properties '{bundle.version=1.0.0.201010191059, bundle=org.planner.service.jpa_1.0.0.201010191059 [88], bundle.symbolicName=org.planner.service.jpa, type=1, timestamp=1287457295361, bundle.id=88}'
[2010-10-19 11:01:35.361] Thread-3 org.eclipse.virgo.kernel.core.internal.BundleStartTracker Handling event 'org.osgi.service.event.Event [topic=org/osgi/service/blueprint/container/CREATED]'
[2010-10-19 11:01:35.361] Thread-3 org.eclipse.virgo.kernel.core.internal.BundleStartTracker Recording created application context for bundle 'org.planner.service.jpa_1.0.0.201010191059 [88]'
[2010-10-19 11:01:35.361] start-signalling-3 org.eclipse.virgo.kernel.core.internal.BundleStartTracker Driving signal 'org.eclipse.virgo.kernel.install.artifact.internal.Abstract InstallArtifact$StateMonitorSignal@1cbeebb'
[2010-10-19 11:01:35.361] start-signalling-3 o.e.v.k.i.artifact.internal.bundle.BundleThreadContextManage r Thread context class loader 'KernelBundleClassLoader: [bundle=org.eclipse.virgo.apps.repository-2.1.0.M06-incubati on-synthetic.context_2.1.0.M06-incubation]' pushed and set to 'KernelBundleClassLoader: [bundle=org.planner.service.jpa_1.0.0.201010191059]'
[2010-10-19 11:01:35.361] start-signalling-3 org.eclipse.virgo.medic.eventlog.default DE0005I Started bundle 'org.planner.service.jpa' version '1.0.0.201010191059'.
[2010-10-19 11:01:35.361] start-signalling-3 o.e.v.k.i.artifact.internal.bundle.BundleThreadContextManage r Thread context class loader 'KernelBundleClassLoader: [bundle=org.planner.service.jpa_1.0.0.201010191059]' popped and set to 'KernelBundleClassLoader: [bundle=org.eclipse.virgo.apps.repository-2.1.0.M06-incubati on-synthetic.context_2.1.0.M06-incubation]'
[2010-10-19 11:01:35.376] region-dm-1 o.s.osgi.extender.internal.activator.ContextLoaderListener Application context successfully refreshed (OsgiBundleXmlApplicationContext(bundle=org.planner.service. jpa, config=osgibundle:/META-INF/spring/*.xml))
[2010-10-19 11:01:35.376] region-dm-5 o.s.beans.factory.support.DefaultListableBeanFactory Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBea nFactory @42c747: defining beans [org.springframework.context.weaving.AspectJWeavingEnabler#0 ,loadTimeWeaver,org.springframework.context.annotation.inter nalConfigurationAnnotationProcessor,org.springframework.cont ext.annotation.internalAutowiredAnnotationProcessor,org.spri ngframework.context.annotation.internalRequiredAnnotationPro cessor,org.springframework.context.annotation.internalCommon AnnotationProcessor,org.springframework.context.annotation.i nternalPersistenceAnnotationProcessor,org.springframework.tr ansaction.config.internalTransactionAspect,org.springframewo rk.orm.jpa.support.PersistenceAnnotationBeanPostProcessor#0, helloWorldServiceBean,helloWorldService,transactionManager,e ntityManagerFactory]; root of factory hierarchy
[2010-10-19 11:01:35.408] region-dm-5 o.s.osgi.service.importer.support.OsgiServiceProxyFactoryBea n Looking for mandatory OSGi service dependency for bean [entityManagerFactory] matching filter (objectClass=javax.persistence.EntityManagerFactory)
[2010-10-19 11:01:35.408] region-dm-5 org.springframework.aop.framework.Cglib2AopProxy Unable to proxy method [public final java.lang.ClassLoader $Proxy161.getBeanClassLoader()] because it is final: All calls to this method via a proxy will be routed directly to the proxy.
[2010-10-19 11:01:35.408] region-dm-5 org.springframework.aop.framework.Cglib2AopProxy Unable to proxy method [public final java.lang.String $Proxy161.getPersistenceUnitName()] because it is final: All calls to this method via a proxy will be routed directly to the proxy.
[2010-10-19 11:01:35.408] region-dm-5 org.springframework.aop.framework.Cglib2AopProxy Unable to proxy method [public final javax.persistence.spi.PersistenceUnitInfo $Proxy161.getPersistenceUnitInfo()] because it is final: All calls to this method via a proxy will be routed directly to the proxy.
[2010-10-19 11:01:35.408] region-dm-5 org.springframework.aop.framework.Cglib2AopProxy Unable to proxy method [public final javax.persistence.EntityManager $Proxy161.createEntityManager(java.util.Map)] because it is final: All calls to this method via a proxy will be routed directly to the proxy.
[2010-10-19 11:01:35.408] region-dm-5 org.springframework.aop.framework.Cglib2AopProxy Unable to proxy method [public final javax.persistence.EntityManager $Proxy161.createEntityManager()] because it is final: All calls to this method via a proxy will be routed directly to the proxy.
[2010-10-19 11:01:35.408] region-dm-5 org.springframework.aop.framework.Cglib2AopProxy Unable to proxy method [public final javax.persistence.criteria.CriteriaBuilder $Proxy161.getCriteriaBuilder()] because it is final: All calls to this method via a proxy will be routed directly to the proxy.
[2010-10-19 11:01:35.408] region-dm-5 org.springframework.aop.framework.Cglib2AopProxy Unable to proxy method [public final javax.persistence.metamodel.Metamodel $Proxy161.getMetamodel()] because it is final: All calls to this method via a proxy will be routed directly to the proxy.
[2010-10-19 11:01:35.408] region-dm-5 org.springframework.aop.framework.Cglib2AopProxy Unable to proxy method [public final javax.persistence.PersistenceUnitUtil $Proxy161.getPersistenceUnitUtil()] because it is final: All calls to this method via a proxy will be routed directly to the proxy.
[2010-10-19 11:01:35.408] region-dm-5 org.springframework.aop.framework.Cglib2AopProxy Unable to proxy method [public final javax.persistence.spi.PersistenceProvider $Proxy161.getPersistenceProvider()] because it is final: All calls to this method via a proxy will be routed directly to the proxy.
[2010-10-19 11:01:35.408] region-dm-5 org.springframework.aop.framework.Cglib2AopProxy Unable to proxy method [public final org.springframework.orm.jpa.JpaDialect $Proxy161.getJpaDialect()] because it is final: All calls to this method via a proxy will be routed directly to the proxy.
[2010-10-19 11:01:35.408] region-dm-5 org.springframework.aop.framework.Cglib2AopProxy Unable to proxy method [public final java.lang.Class $Proxy161.getEntityManagerInterface()] because it is final: All calls to this method via a proxy will be routed directly to the proxy.
[2010-10-19 11:01:35.408] region-dm-5 org.springframework.aop.framework.Cglib2AopProxy Unable to proxy method [public final javax.sql.DataSource $Proxy161.getDataSource()] because it is final: All calls to this method via a proxy will be routed directly to the proxy.
[2010-10-19 11:01:35.408] region-dm-5 org.springframework.aop.framework.Cglib2AopProxy Unable to proxy method [public final javax.persistence.EntityManagerFactory $Proxy161.getNativeEntityManagerFactory()] because it is final: All calls to this method via a proxy will be routed directly to the proxy.
[2010-10-19 11:01:35.408] region-dm-5 org.springframework.aop.framework.Cglib2AopProxy Unable to proxy method [public final boolean $Proxy161.equals(java.lang.Object)] because it is final: All calls to this method via a proxy will be routed directly to the proxy.
[2010-10-19 11:01:35.408] region-dm-5 org.springframework.aop.framework.Cglib2AopProxy Unable to proxy method [public final java.lang.String $Proxy161.toString()] because it is final: All calls to this method via a proxy will be routed directly to the proxy.
[2010-10-19 11:01:35.408] region-dm-5 org.springframework.aop.framework.Cglib2AopProxy Unable to proxy method [public final int $Proxy161.hashCode()] because it is final: All calls to this method via a proxy will be routed directly to the proxy.
[2010-10-19 11:01:35.408] region-dm-5 org.springframework.aop.framework.Cglib2AopProxy Unable to proxy method [public final java.util.Map $Proxy161.getProperties()] because it is final: All calls to this method via a proxy will be routed directly to the proxy.
[2010-10-19 11:01:35.408] region-dm-5 org.springframework.aop.framework.Cglib2AopProxy Unable to proxy method [public final void $Proxy161.close()] because it is final: All calls to this method via a proxy will be routed directly to the proxy.
[2010-10-19 11:01:35.408] region-dm-5 org.springframework.aop.framework.Cglib2AopProxy Unable to proxy method [public final boolean $Proxy161.isOpen()] because it is final: All calls to this method via a proxy will be routed directly to the proxy.
[2010-10-19 11:01:35.408] region-dm-5 org.springframework.aop.framework.Cglib2AopProxy Unable to proxy method [public final javax.persistence.Cache $Proxy161.getCache()] because it is final: All calls to this method via a proxy will be routed directly to the proxy.
[2010-10-19 11:01:35.517] region-dm-5 o.s.o.s.e.support.internal.support.PublishingServiceFactory Cannot create TCCL managed proxy; falling back to the naked object org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class $Proxy161]: Common causes of this problem include using a final class or a non-visible class; nested exception is java.lang.IllegalArgumentException: Cannot subclass final class class $Proxy161
at org.springframework.aop.framework.Cglib2AopProxy.getProxy(Cg lib2AopProxy.java:212)
at org.springframework.aop.framework.ProxyFactory.getProxy(Prox yFactory.java:112)
at org.springframework.osgi.service.util.internal.aop.ProxyUtil s$1.run(ProxyUtils.java:65)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.osgi.service.util.internal.aop.ProxyUtil s.createProxy(ProxyUtils.java:62)
at org.springframework.osgi.service.exporter.support.internal.s upport.PublishingServiceFactory.createCLLProxy(PublishingSer viceFactory.java:138)
at org.springframework.osgi.service.exporter.support.internal.s upport.PublishingServiceFactory.getService(PublishingService Factory.java:117)
at org.eclipse.osgi.internal.serviceregistry.ServiceUse$1.run(S erviceUse.java:120)
at java.security.AccessController.doPrivileged(Native Method)
at org.eclipse.osgi.internal.serviceregistry.ServiceUse.getServ ice(ServiceUse.java:118)
at org.eclipse.osgi.internal.serviceregistry.ServiceRegistratio nImpl.getService(ServiceRegistrationImpl.java:447)
at org.eclipse.osgi.internal.serviceregistry.ServiceRegistry.ge tService(ServiceRegistry.java:430)
at org.eclipse.osgi.framework.internal.core.BundleContextImpl.g etService(BundleContextImpl.java:667)
at org.springframework.osgi.service.importer.support.internal.s upport.ServiceWrapper.getService(ServiceWrapper.java:99)
at org.springframework.osgi.service.importer.support.internal.a op.ServiceDynamicInterceptor$ServiceLookUpCallback.doWithRet ry(ServiceDynamicInterceptor.java:107)
at org.springframework.osgi.service.importer.support.internal.s upport.RetryTemplate.execute(RetryTemplate.java:83)
at org.springframework.osgi.service.importer.support.internal.a op.ServiceDynamicInterceptor.lookupService(ServiceDynamicInt erceptor.java:430)
at org.springframework.osgi.service.importer.support.internal.a op.ServiceDynamicInterceptor.getTarget(ServiceDynamicInterce ptor.java:415)
at org.springframework.osgi.service.importer.support.internal.a op.ServiceDynamicInterceptor.afterPropertiesSet(ServiceDynam icInterceptor.java:472)
at org.springframework.osgi.service.importer.support.OsgiServic eProxyFactoryBean.createProxy(OsgiServiceProxyFactoryBean.ja va:215)
at org.springframework.osgi.service.importer.support.AbstractSe rviceImporterProxyFactoryBean.getObject(AbstractServiceImpor terProxyFactoryBean.java:86)
at org.springframework.osgi.service.importer.support.OsgiServic eProxyFactoryBean.getObject(OsgiServiceProxyFactoryBean.java :161)
at org.springframework.beans.factory.support.FactoryBeanRegistr ySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySuppo rt.java:142)
at org.springframework.beans.factory.support.FactoryBeanRegistr ySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport .java:102)
at org.springframework.beans.factory.support.AbstractBeanFactor y.getObjectForBeanInstance(AbstractBeanFactory.java:1414)
at org.springframework.beans.factory.support.AbstractBeanFactor y.doGetBean(AbstractBeanFactory.java:245)
at org.springframework.beans.factory.support.AbstractBeanFactor y.getBean(AbstractBeanFactory.java:190)
at org.springframework.orm.jpa.support.PersistenceAnnotationBea nPostProcessor.findDefaultEntityManagerFactory(PersistenceAn notationBeanPostProcessor.java:529)
at org.springframework.orm.jpa.support.PersistenceAnnotationBea nPostProcessor.findEntityManagerFactory(PersistenceAnnotatio nBeanPostProcessor.java:495)
at org.springframework.orm.jpa.support.PersistenceAnnotationBea nPostProcessor$PersistenceElement.resolveEntityManager(Persi stenceAnnotationBeanPostProcessor.java:656)
at org.springframework.orm.jpa.support.PersistenceAnnotationBea nPostProcessor$PersistenceElement.getResourceToInject(Persis tenceAnnotationBeanPostProcessor.java:629)
at org.springframework.beans.factory.annotation.InjectionMetada ta$InjectedElement.inject(InjectionMetadata.java:147)
at org.springframework.beans.factory.annotation.InjectionMetada ta.inject(InjectionMetadata.java:84)
at org.springframework.orm.jpa.support.PersistenceAnnotationBea nPostProcessor.postProcessPropertyValues(PersistenceAnnotati onBeanPostProcessor.java:338)
at org.springframework.beans.factory.support.AbstractAutowireCa pableBeanFactory.populateBean(AbstractAutowireCapableBeanFac tory.java:1074)
at org.springframework.beans.factory.support.AbstractAutowireCa pableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFac tory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCa pableBeanFactory.createBean(AbstractAutowireCapableBeanFacto ry.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactor y$1.getObject(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBe anRegistry.getSingleton(DefaultSingletonBeanRegistry.java:22 2)
at org.springframework.beans.factory.support.AbstractBeanFactor y.doGetBean(AbstractBeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactor y.getBean(AbstractBeanFactory.java:190)
at org.springframework.beans.factory.support.DefaultListableBea nFactory.preInstantiateSingletons(DefaultListableBeanFactory .java:580)
at org.springframework.context.support.AbstractApplicationConte xt.finishBeanFactoryInitialization(AbstractApplicationContex t.java:895)
at org.springframework.osgi.context.support.AbstractDelegatedEx ecutionApplicationContext.access$1600(AbstractDelegatedExecu tionApplicationContext.java:69)
at org.springframework.osgi.context.support.AbstractDelegatedEx ecutionApplicationContext$4.run(AbstractDelegatedExecutionAp plicationContext.java:355)
at org.springframework.osgi.util.internal.PrivilegedUtils.execu teWithCustomTCCL(PrivilegedUtils.java:85)
at org.springframework.osgi.context.support.AbstractDelegatedEx ecutionApplicationContext.completeRefresh(AbstractDelegatedE xecutionApplicationContext.java:320)
at org.springframework.osgi.extender.internal.dependencies.star tup.DependencyWaiterApplicationContextExecutor$CompleteRefre shTask.run(DependencyWaiterApplicationContextExecutor.java:1 32)
at org.eclipse.virgo.kernel.agent.dm.ContextPropagatingTaskExec utor$2.run(ContextPropagatingTaskExecutor.java:95)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Threa dPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoo lExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.IllegalArgumentException: Cannot subclass final class class $Proxy161
at net.sf.cglib.proxy.Enhancer.generateClass(Enhancer.java:446)
at net.sf.cglib.transform.TransformingClassGenerator.generateCl ass(TransformingClassGenerator.java:33)
at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultG eneratorStrategy.java:25)
at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClas sGenerator.java:216)
at net.sf.cglib.proxy.Enhancer.createHelper(Enhancer.java:377)
at net.sf.cglib.proxy.Enhancer.create(Enhancer.java:285)
at org.springframework.aop.framework.Cglib2AopProxy.getProxy(Cg lib2AopProxy.java:200)
... 51 common frames omitted
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04354 seconds