2014-04-14 34 views
0

我使用Hibernate从Karaf 2.3.4容器连接到PostgreSQL,当我部署我的包时有一个异常说javax.naming.NameNotFoundException osgi:service/javax 。.sql.DataSource /“(osgi.jndi.service.name = JDBC/postgresds)从在Karaf容器中部署的OSGi应用程序连接到PostgreSQL

这里是我的persistance.xml文件

<?xml version="1.0" encoding="UTF-8"?> 
<persistence xmlns="http://java.sun.com/xml/ns/persistence" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0"> 
    <persistence-unit name="HibernateOSGi_ContainerManaged" 
     transaction-type="JTA"> 
     <jta-data-source>osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=jdbc/postgresds)</jta-data-source> 
     <class>org.hibernate.osgitest.entity.DataPoint</class> 
     <exclude-unlisted-classes>true</exclude-unlisted-classes> 
     <properties> 
      <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" /> 
      <property name="hibernate.hbm2ddl.auto" value="create-drop" /> 
      <property name="hibernate.archive.autodetection" value="class" /> 
      <property name="hibernate.show_sql" value="true" /> 
      <property name="hibernate.format_sql" value="true" /> 
     </properties> 
    </persistence-unit> 
</persistence> 

这里是我的blueprint.xml文件:

<?xml version="1.0" encoding="UTF-8"?> 

<blueprint default-activation="eager" 
    xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:jpa="http://aries.apache.org/xmlns/jpa/v1.0.0" xmlns:tx="http://aries.apache.org/xmlns/transactions/v1.0.0"> 

    <bean id="dpService" class="org.hibernate.osgitest.DataPointServiceImpl"> 
     <jpa:context unitname="HibernateOSGi_ContainerManaged" property="entityManager" /> 
     <tx:transaction method="*" value="Required" /> 
    </bean> 

    <service ref="dpService" interface="org.hibernate.osgitest.DataPointService" /> 

    <command-bundle xmlns="http://karaf.apache.org/xmlns/shell/v1.1.0"> 
     <command name="dp/add"> 
      <action class="org.hibernate.osgitest.command.AddCommand"> 
       <property name="dpService" ref="dpService" /> 
      </action> 
     </command> 
     <command name="dp/getAll"> 
      <action class="org.hibernate.osgitest.command.GetAllCommand"> 
       <property name="dpService" ref="dpService" /> 
      </action> 
     </command> 
     <command name="dp/deleteAll"> 
      <action class="org.hibernate.osgitest.command.DeleteAllCommand"> 
       <property name="dpService" ref="dpService" /> 
      </action> 
     </command> 
    </command-bundle> 
</blueprint> 

这里是我的数据源,postgres.xml文件

<?xml version="1.0" encoding="UTF-8"?> 
<!-- 

Install the driver in Karaf. As it is no bundle we use the wrap protocol to create a suitable Manifest on the fly: 
> install -s wrap:mvn:postgresql/postgresql/9.1-901.jdbc4 

As a last step copy this file to the deploy folder 
--> 
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"> 

    <bean id="dataSource" class="org.postgresql.ds.PGPoolingDataSource" destroy-method="close"> 
     <property name="serverName" value="localhost:5432/postgres" /> 
     <property name="user" value="postgres" /> 
     <property name="password" value="159357123" /> 
     <property name="dataSourceName" value="postgresds" /> 
    </bean> 

    <service interface="javax.sql.DataSource" ref="dataSource"> 
     <service-properties> 
      <entry key="osgi.jndi.service.name" value="jdbc/postgresds" /> 
     </service-properties> 
    </service> 
</blueprint> 

这里是我的features.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<features> 
    <feature name="hibernate-test"> 

     <feature>karaf-framework</feature> 

     <!-- JTA --> 
     <config name="org.apache.aries.transaction"> 
      aries.transaction.recoverable = true 
      aries.transaction.timeout = 600 
      aries.transaction.howl.logFileDir = 
      ${karaf.data}/txlog 
      aries.transaction.howl.maxLogFiles = 2 
      aries.transaction.howl.maxBlocksPerFile = 512 
      aries.transaction.howl.bufferSizeKBytes = 4 
     </config> 
     <bundle start-level="30">mvn:org.apache.geronimo.specs/geronimo-jta_1.1_spec/1.1.1 
     </bundle> 
     <bundle start-level="30">mvn:org.apache.aries.transaction/org.apache.aries.transaction.blueprint/1.0.0 
     </bundle> 
     <bundle start-level="30">mvn:org.apache.aries.transaction/org.apache.aries.transaction.manager/1.0.1 
     </bundle> 

     <!-- JPA --> 
     <!-- <bundle start-level="30">mvn:org.hibernate.javax.persistence/hibernate-jpa-2.1-api/1.0.0-SNAPSHOT</bundle> --> 
     <bundle start-level="30">mvn:org.apache.geronimo.specs/geronimo-jpa_2.0_spec/1.1 
     </bundle> 
     <bundle start-level="30">mvn:org.apache.aries/org.apache.aries.util/1.0.0 
     </bundle> 
     <bundle start-level="30">mvn:org.apache.aries.jpa/org.apache.aries.jpa.api/1.0.0 
     </bundle> 
     <bundle start-level="30">mvn:org.apache.aries.jpa/org.apache.aries.jpa.blueprint.aries/1.0.0 
     </bundle> 
     <bundle start-level="30">mvn:org.apache.aries.jpa/org.apache.aries.jpa.container/1.0.0 
     </bundle> 
     <bundle start-level="30">mvn:org.apache.aries.jpa/org.apache.aries.jpa.container.context/1.0.1 
     </bundle> 

     <!-- JNDI --> 
     <bundle start-level="30">mvn:org.apache.aries.jndi/org.apache.aries.jndi.api/1.0.0 
     </bundle> 
     <bundle start-level="30">mvn:org.apache.aries.jndi/org.apache.aries.jndi.core/1.0.0 
     </bundle> 
     <bundle start-level="30">mvn:org.apache.aries.jndi/org.apache.aries.jndi.rmi/1.0.0 
     </bundle> 
     <bundle start-level="30">mvn:org.apache.aries.jndi/org.apache.aries.jndi.url/1.0.0 
     </bundle> 
     <bundle start-level="30">mvn:org.apache.aries.jndi/org.apache.aries.jndi.legacy.support/1.0.0 
     </bundle> 

     <feature>jdbc</feature> 
     <!-- Taken from Karaf-Tutorial --> 
     <bundle>mvn:org.hibernate.common/com.springsource.org.hibernate.annotations.common/4.1.0.Final</bundle> 
     <bundle>mvn:commons-collections/commons-collections/3.2.1</bundle> 
     <bundle>mvn:commons-pool/commons-pool/1.5.4</bundle> 
     <bundle>mvn:commons-dbcp/commons-dbcp/1.4</bundle> 
     <bundle>mvn:commons-lang/commons-lang/2.6</bundle> 
     <bundle>wrap:mvn:net.sourceforge.serp/serp/1.13.1</bundle> 


     <!-- These do not natively support OSGi, so using 3rd party bundles. --> 
     <bundle>mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.antlr/2.7.7_5 
     </bundle> 
     <bundle>mvn:org.jboss.javassist/com.springsource.javassist/3.15.0.GA 
     </bundle> 
     <bundle>mvn:org.apache.servicemix.specs/org.apache.servicemix.specs.jsr303-api-1.0.0/2.2.0 
     </bundle> 
     <bundle>mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.ant/1.8.2_2 
     </bundle> 
     <bundle>mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.dom4j/1.6.1_5 
     </bundle> 

     <bundle>wrap:mvn:postgresql/postgresql/9.1-901.jdbc4</bundle> 
     <!--<bundle>mvn:mysql/mysql-connector-java/5.1.18</bundle>--> 
     <bundle>blueprint:file:C:/Users/yahya/Desktop/Examples/Nouveau/HibernateOSGi-master/datasource-postgres.xml 
     </bundle> 

     <!-- These do not natively support OSGi, so wrap with BND. --> 
     <bundle>wrap:mvn:org.jboss/jandex/1.1.0.Alpha1</bundle> 

     <bundle>wrap:mvn:org.hibernate.common/hibernate-commons-annotations/4.0.1.Final</bundle> 
     <bundle>mvn:com.fasterxml/classmate/0.5.4</bundle> 
     <bundle>mvn:org.jboss.logging/jboss-logging/3.1.0.GA</bundle> 

     <!-- JACC is optional. --> 
     <!--<bundle>mvn:javax.servlet/javax.servlet-api/3.0.1</bundle> <bundle>mvn:org.jboss.spec.javax.security.jacc/jboss-jacc-api_1.4_spec/1.0.2.Final</bundle> --> 

     <!-- hibernate-validator is optional. --> 
     <!--<bundle>wrap:mvn:javax.validation/validation-api/1.0.0.GA</bundle> 
      <bundle>mvn:org.hibernate/hibernate-validator/4.2.0.Final</bundle> --> 

     <bundle>mvn:org.hibernate/hibernate-core/4.2.2.Final</bundle> 
     <bundle>mvn:org.hibernate/hibernate-entitymanager/4.2.2.Final</bundle> 


     <!-- TODO: It seems that the persistence unit bundle needs to be started 
      before hibernate-osgi. When the BundleActivator is started, the persistence 
      unit is provided even though HibernateOSGi_ContainerManaged hasn't completely 
      started yet. If that happens, you'll get an "illegal bundle state" exception. 
      Is there a way for the activator to watch for bundles with PUs before registering 
      the persistence provider? --> 
     <bundle>mvn:org.hibernate/HibernateOSGi_ContainerManaged/1.0.0 
     </bundle> 

     <bundle>mvn:org.hibernate/hibernate-osgi/4.2.2.Final</bundle> 
    </feature> 
</features> 

完整的堆栈跟踪是:

javax.naming.NameNotFoundException:OSGi的: service/javax.sql.DataSource /“(osgi.jndi.service.name = jdbc/postgresds)” at org.apache.aries.jndi.url.ServiceRegistryContext.lookup(ServiceRegistryContext.java:113)[72:org。 apache.aries.jndi.url:1.0.0] at org.apache.aries.jndi .url.ServiceRegistryContext.lookup(ServiceRegistryContext.java:144)[72:org.apache.aries.jndi.url:1.0.0] at org.apache.aries.jndi.DelegateContext.lookup(DelegateContext.java:161) [70:org.apache.aries.jndi.core:1.0.0] at javax.naming.InitialContext.lookup(InitialContext.java:411)[:1.7.0_51] at org.apache.aries.jpa.container .unit.impl.JndiDataSource.getDs(JndiDataSource.java:65)[67:org.apache.aries.jpa.container:1.0.0] at org.apache.aries.jpa.container.unit.impl.DelayedLookupDataSource。的getConnection(DelayedLookupDataSource.java:36)[67:org.apache.aries.jpa.container:1.0.0] 在org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider.getConnection(InjectedDataSourceConnectionProvider.java:70)[91:有机hibernate.entitymanager:4.2.2.Final] at org.hibernate.engine.jdbc.internal.JdbcServicesImpl $ ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcServicesImpl.java:242)[90:org.hibernate.core:4.2.2.Final] 在org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:117)[90 :org.hibernate.core:4.2.2.Final] 在org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75)[90:org.hibernate.core:4.2.2.Final] 在org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159)[90:org.hibernate.core:4.2.2.Final] 在org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java :131)[90:org.hibernate.core:4.2.2.Final] at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1797)[90:org.hibernate.core:4.2.2.Final ] 在org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1755)[90:org.hibernate.core:4.2.2.Final]。 在org.hibernate.ejb.EntityManagerFactoryImpl(EntityManagerFactoryImpl.java:96) [91:org.hibernate.entitymanager:4.2.2.Final] at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:914)[91:org.hibernate.entitymanager:4.2.2.Final] at org.hibernate.osgi.OsgiPersistenceProvider.createContainerEntityManagerFactory(OsgiPersistenceProvider.java:99)[96:org.hibernate.osgi:4.2.2.Final] at org.apache.aries.jpa.container.impl.EntityManagerFactoryManager.createEntityManagerFactories(EntityManagerFactoryManager .java:329)[67:org.apache.aries.jpa.container:1.0.0] at org.apache.aries.jpa.container.impl.EntityManagerFactoryManager。registerEntityManagerFactories(EntityManagerFactoryManager.java:242)[67:org.apache.aries.jpa.container:1.0.0] 在org.apache.aries.jpa.container.impl.EntityManagerFactoryManager.bundleStateChange(EntityManagerFactoryManager.java:185) 67:org.apache.aries.jpa.container:1.0.0] at org.apache.aries.jpa.container.impl.PersistenceBundleManager.setupManager(PersistenceBundleManager.java:394)[67:org.apache.aries.jpa .container:1.0.0] 在org.apache.aries.jpa.container.impl.PersistenceBundleManager.addingService(PersistenceBundleManager.java:209)[67:org.apache.aries.jpa.container:1.0.0] 在org.osgi.util.tracker.ServiceTracker $ Tracked.customizerAdding(ServiceTracker.java:932)[karaf.jar:2.3.4] at org.osgi.util.tracker.ServiceTracker $ Tracked.customizerAdding(ServiceTracker.java:1 )[karaf.jar:2.3.4] at org.os gi.util.tracker.AbstractTracked.trackAdding(AbstractTracked.java:256)[karaf.jar:2.3.4] at org.osgi.util.tracker.AbstractTracked.track(AbstractTracked.java:229)[karaf.jar: 2.3.4] at org.osgi.util.tracker.ServiceTracker $ Tracked.serviceChanged(ServiceTracker.java:894)[karaf.jar:2.3.4] at org.apache.felix.framework.util.EventDispatcher.invokeServiceListenerCallback (EventDispatcher.java:932)[org.apache.felix.framework-4.0.3.jar:] at org.apache.felix.framework.util.EventDispatcher.fireEventImmediately(EventDispatcher.java:793)[org.apache。 felix.framework-4.0.3.jar:] 在org.apache.felix.framework.util.EventDispatcher.fireServiceEvent(EventDispatcher.java:543)[org.apache.felix.framework-4.0.3.jar:] 在org.apache.felix.framework.Felix.fireServiceEvent(Felix.java:4260)[org.apache.felix.framework-4.0.3.j AR:] 在org.apache.felix.framework.Felix.registerService(Felix.java:3275)[org.apache.felix.framework-4.0.3.jar:] 在org.apache.felix.framework.BundleContextImpl .registerService(BundleContextImpl.java:346)[org.apache.felix.framework-4.0.3.jar:] at org.apache.felix.framework.BundleContextImpl.registerService(BundleContextImpl.java:320)[org.apache。 [org.hibernate.osgi:4.2.2.Final] at org.apache。 felix.framework.util.SecureAction.startActivator(SecureAction.java:645)[org.apache.felix.framework-4.0.3.jar:] at org.apache.felix.framework.Felix.activateBundle(Felix.java: 1977)[org.apache.felix.framework-4.0.3.jar:] at org.apache.felix.framework.Felix.star tBundle(Felix.java:1895)[org.apache.felix.framework-4.0.3.jar:] at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:944)[org.apache.felix .framework-4.0.3.jar:] 在org.apache.felix.framework.BundleImpl.start(BundleImpl.java:931)[org.apache.felix.framework-4.0.3.jar:] 在有机在org.apache.karaf.features.internal.FeaturesServiceImpl.installFeature( (FeaturesServiceImpl.java:405)[25:org.apache.karaf.features.core:2.3.4] at org.apache.karaf.features.internal.FeaturesServiceImpl.installFeature(FeaturesServiceImpl.java:401)[25:org .apache.karaf.features.core:2.3.4] at org.apache.karaf.features.command.InstallFeatureCommand.doExecute(InstallFeatureCommand。 java:62)[27:org.apache.karaf.features.command:2.3.4] at org.apache.karaf.features.command.FeaturesCommandSupport.doExecute(FeaturesCommandSupport.java:41)[27:org.apache。 karaf.features.command:2.3.4] 在org.apache.karaf.shell.console.OsgiCommandSupport.execute(OsgiCommandSupport.java:38)[14:org.apache.karaf.shell.console:2.3.4] 在org.apache.felix.gogo.commands.basic.AbstractCommand.execute(AbstractCommand.java:35)[14:org.apache.karaf.shell.console:2.3.4] at org.apache.felix.gogo。 (org.apache.felix.gogo.runtime.Closure.executeCmd)(Closure.java:474 )[14:org.apache.karaf.shell.console:2.3.4] at org.apache.felix.gogo.runtime.Closure.executeStatement(Closure.java:400)[14:org.apache.karaf.shell .console:2.3。4] at org.apache.felix.gogo.runtime.Pipe.run(Pipe.java:108)[14:org.apache.karaf.shell.console:2.3.4] at org.apache.felix.gogo .runtime.Closure.execute(Closure.java:183)[14:org.apache.karaf.shell.console:2.3.4] at org.apache.felix.gogo.runtime.Closure.execute(Closure.java: 120)[14:org.apache.karaf.shell.console:2.3.4] at org.apache.felix.gogo.runtime.CommandSessionImpl.execute(CommandSessionImpl.java:89)[14:org.apache.karaf。 shell.console:2.3.4] at org.apache.karaf.shell.console.jline.Console.run(Console.java:183)[14:org.apache.karaf.shell.console:2.3.4] 在java.lang.Thread.run(Thread.java:744)[:1.7.0_51]

我解决了这个问题,我更新了文件列表,如M. Cris但是这是另一个奇怪的问题:当重新启动Karaf并尝试通过Karaf控制台插入数据时控制台打印:没有活动的事务,所以我尝试了另一种方式使用EntityManager,问题不再出现。

+0

如何通过karaf控制台检查数据源url(连接到db的完整url)?有没有可用的命令?我已经使用命令confi:list | grep datasource,但它只给出配置文件的数据源和路径名称,而不是数据源连接的实际URL。 – Milesh

回答

1

由于DataSource是在使用持久性单元的相同蓝图上下文中发布的,因此您的代码无法工作。

问题是蓝图将等待EnitityManagerFactory服务注入。所以DataSource永远不会被发布。白羊座JPA会检测到它应该为你的包创建一个EntityManagerFactory,但会尝试查找数据源超时。

因此,把数据源放入一个特殊的包中,它应该可以工作。

+0

谢谢M. Christian Schneider,我使用你的教程解决了这个问题,当重新启动Karaf并尝试通过卡拉夫控制台插入数据时,我将更新我的文章 – user2075227

+0

控制台打印:Theere没有活动事务,并且在尝试从数据库获取数据时它工作!,所以我尝试了另一种方式使用EntityManager和问题不再出现。 – user2075227

+0

没有活动事务意味着您尝试在事务外更改数据库中的数据。如果您未配置容器管理的事务(tx:事务),则会发生这种情况。为了使它工作,你最终改变了什么?将数据源提取到单独的包中? –

相关问题