在kar文件的热部署过程中遇到问题

时间:2017-12-06 14:38:40

标签: java hibernate jpa osgi blueprint-osgi

我的kar文件包含以下软件包:

a) common-bundle (包含hibernate作为JPA提供者)

b) client-bundle (访问common-bundle中存在的entityManagerFactory并执行业务逻辑)

在karaf容器上执行kar文件的热部署时,我遇到了一个奇怪的问题。

如果我尝试进行正常部署(例如,通过停止karaf容器,将kar文件放入deploy文件夹并重新启动karaf容器),两个捆绑包都可以正常启动并运行。

但是,当我尝试在karaf容器上热部署kar文件时,它会抛出以下异常:

  

Dec 06 13:50:50 INFO 141 HHH000204:处理PersistenceUnitInfo [           name:MyModelClass           ...]   12月16日13:50:51 INFO 141 HHH000130:实例化显式连接提供者:org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider   Dec 06 13:50:51 ERROR 187无法找到bundle common-bundle / 0.0.1.SNAPSHOT所需的DataSource osgi:service / javax.sql.DataSource /(osgi.jndi.service.name = jdbc / ds) 。   javax.naming.NameNotFoundException:osgi:service / javax.sql.DataSource /"(osgi.jndi.service.name = jdbc / ds)"           在org.apache.aries.jndi.url.ServiceRegistryContext.lookup(ServiceRegistryContext.java:113)[66:org.apache.aries.jndi.url:1.1.0]           在org.apache.aries.jndi.url.ServiceRegistryContext.lookup(ServiceRegistryContext.java:138)[66:org.apache.aries.jndi.url:1.1.0]           在org.apache.aries.jndi.DelegateContext.lookup(DelegateContext.java:161)[64:org.apache.aries.jndi.core:1.0.2]           在javax.naming.InitialContext.lookup(InitialContext.java:417)[:1.8.0_141]           at org.apache.aries.jpa.container.unit.impl.JndiDataSource.getDs(JndiDataSource.java:66)[187:org.apache.aries.jpa.container:1.0.4]           at org.apache.aries.jpa.container.unit.impl.DelayedLookupDataSource.getConnection(DelayedLookupDataSource.java:36)[187:org.apache.aries.jpa.container:1.0.4]           at org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider.getConnection(InjectedDataSourceConnectionProvider.java:70)[213:org.hibernate.entitymanager:4.2.21.Final]           at org.hibernate.engine.jdbc.internal.JdbcServicesImpl $ ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcServicesImpl.java:242)[212:org.hibernate.core:4.2.21.Final]           在org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:117)[212:org.hibernate.core:4.2.21.Final]           在org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:85)[212:org.hibernate.core:4.2.21.Final]           在org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:184)[212:org.hibernate.core:4.2.21.Final]           at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:156)[212:org.hibernate.core:4.2.21.Final]           在org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1827)[212:org.hibernate.core:4.2.21.Final]           在org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1785)[212:org.hibernate.core:4.2.21.Final]           在org.hibernate.ejb.EntityManagerFactoryImpl。(EntityManagerFactoryImpl.java:96)[213:org.hibernate.entitymanager:4.2.21.Final]           在org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:915)[213:org.hibernate.entitymanager:4.2.21.Final]           在org.hibernate.osgi.OsgiPersistenceProvider.createContainerEntityManagerFactory(OsgiPersistenceProvider.java:99)[214:org.hibernate.osgi:4.2.21.Final]           at org.apache.aries.jpa.container.impl.EntityManagerFactoryManager.createEntityManagerFactories(EntityManagerFactoryManager.java:432)[187:org.apache.aries.jpa.container:1.0.4]           at org.apache.aries.jpa.container.impl.EntityManagerFactoryManager.registerEntityManagerFactories(EntityManagerFactoryManager.java:292)[187:org.apache.aries.jpa.container:1.0.4]           at org.apache.aries.jpa.container.impl.EntityManagerFactoryManager.access $ 000(EntityManagerFactoryManager.java:74)[187:org.apache.aries.jpa.container:1.0.4]           在org.apache.aries.jpa.container.impl.EntityManagerFactoryManager $ 1.call(EntityManagerFactoryManager.java:203)[187:org.apache.aries.jpa.container:1.0.4]           在org.apache.aries.jpa.container.impl.EntityManagerFactoryManager $ 1.call(EntityManagerFactoryManager.java:199)[187:org.apache.aries.jpa.container:1.0.4]           at java.util.concurrent.FutureTask.run(FutureTask.java:266)[:1.8.0_141]           at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)[:1.8.0_141]           at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:624)[:1.8.0_141]           在java.lang.Thread.run(Thread.java:748)[:1.8.0_141]   Dec 06 13:50:51 WARN 187创建EntityManagerFactory时出错   java.lang.RuntimeException:找不到bundle common-bundle / 0.0.1.SNAPSHOT所需的DataSource osgi:service / javax.sql.DataSource /(osgi.jndi.service.name = jdbc / ds)。           at org.apache.aries.jpa.container.unit.impl.JndiDataSource.getDs(JndiDataSource.java:87)[187:org.apache.aries.jpa.container:1.0.4]           at org.apache.aries.jpa.container.unit.impl.DelayedLookupDataSource.getConnection(DelayedLookupDataSource.java:36)[187:org.apache.aries.jpa.container:1.0.4]           at org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider.getConnection(InjectedDataSourceConnectionProvider.java:70)[213:org.hibernate.entitymanager:4.2.21.Final]           at org.hibernate.engine.jdbc.internal.JdbcServicesImpl $ ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcServicesImpl.java:242)[212:org.hibernate.core:4.2.21.Final]           在org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:117)[212:org.hibernate.core:4.2.21.Final]           在org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:85)[212:org.hibernate.core:4.2.21.Final]           在org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:184)[212:org.hibernate.core:4.2.21.Final]           at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:156)[212:org.hibernate.core:4.2.21.Final]           在org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1827)[212:org.hibernate.core:4.2.21.Final]           在org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1785)[212:org.hibernate.core:4.2.21.Final]           在org.hibernate.ejb.EntityManagerFactoryImpl。(EntityManagerFactoryImpl.java:96)[213:org.hibernate.entitymanager:4.2.21.Final]           在org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:915)[213:org.hibernate.entitymanager:4.2.21.Final]           在org.hibernate.osgi.OsgiPersistenceProvider.createContainerEntityManagerFactory(OsgiPersistenceProvider.java:99)[214:org.hibernate.osgi:4.2.21.Final]           at org.apache.aries.jpa.container.impl.EntityManagerFactoryManager.createEntityManagerFactories(EntityManagerFactoryManager.java:432)[187:org.apache.aries.jpa.container:1.0.4]           at org.apache.aries.jpa.container.impl.EntityManagerFactoryManager.registerEntityManagerFactories(EntityManagerFactoryManager.java:292)[187:org.apache.aries.jpa.container:1.0.4]           at org.apache.aries.jpa.container.impl.EntityManagerFactoryManager.access $ 000(EntityManagerFactoryManager.java:74)[187:org.apache.aries.jpa.container:1.0.4]           在org.apache.aries.jpa.container.impl.EntityManagerFactoryManager $ 1.call(EntityManagerFactoryManager.java:203)[187:org.apache.aries.jpa.container:1.0.4]           在org.apache.aries.jpa.container.impl.EntityManagerFactoryManager $ 1.call(EntityManagerFactoryManager.java:199)[187:org.apache.aries.jpa.container:1.0.4]           at java.util.concurrent.FutureTask.run(FutureTask.java:266)[:1.8.0_141]           at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)[:1.8.0_141]           at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:624)[:1.8.0_141]           在java.lang.Thread.run(Thread.java:748)[:1.8.0_141]   引起:javax.naming.NameNotFoundException:osgi:service / javax.sql.DataSource /"(osgi.jndi.service.name = jdbc / ds)"           在org.apache.aries.jndi.url.ServiceRegistryContext.lookup(ServiceRegistryContext.java:113)[66:org.apache.aries.jndi.url:1.1.0]           在org.apache.aries.jndi.url.ServiceRegistryContext.lookup(ServiceRegistryContext.java:138)[66:org.apache.aries.jndi.url:1.1.0]           在org.apache.aries.jndi.DelegateContext.lookup(DelegateContext.java:161)[64:org.apache.aries.jndi.core:1.0.2]           在javax.naming.InitialContext.lookup(InitialContext.java:417)[:1.8.0_141]           at org.apache.aries.jpa.container.unit.impl.JndiDataSource.getDs(JndiDataSource.java:66)[187:org.apache.aries.jpa.container:1.0.4]           ......还有21个   12月06日13:50:51 INFO 141 HHH000204:处理PersistenceUnitInfo [           name:MyModelClass           ...]   Dec 06 13:50:52 INFO 141 HHH000130:实例化显式连接提供者:org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider   Dec 06 13:50:55 INFO 141 HHH000400:使用方言:org.hibernate.dialect.Oracle10gDialect   12月16日13:50:55 INFO 141 HHH000268:交易策略:org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory   Dec 06 13:50:55 INFO 141 HHH000397:使用ASTQueryTranslatorFactory   Dec 06 13:50:56 WARN 141 HHH000008:JTASessionContext与JDBCTransactionFactory一起使用;使用getCurrentSession()

,自动刷新将无法正常运行

以下是捆绑包中最重要的内容:

常见束:

的persistence.xml:

<persistence version="2.0"
         xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/persistence">

<persistence-unit name="my-persistent-unit" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <non-jta-data-source>osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=jdbc/ds)</non-jta-data-source>
    <class>MyModelClass</class>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
</persistence-unit>

blueprint.xml:

<cm:property-placeholder persistent-id='my-persistent-id'
                         update-strategy='reload'
                         placeholder-prefix="${"
                         placeholder-suffix="}">
    <cm:default-properties>
        <cm:property name="driver" value="undefined"/>
        <cm:property name="url" value="undefined"/>
        <cm:property name="username" value="undefined"/>
        <cm:property name="password" value="undefined"/>
    </cm:default-properties>
</cm:property-placeholder>

<jasypt:property-placeholder encryptor-ref="encryptor"/>
<reference id="encryptor" interface="org.jasypt.encryption.StringEncryptor" availability="mandatory"/>

<bean id='myDs'
      class="CustomConnection">
    <property name="connectionFactoryClassName" value="oracle.jdbc.pool.OracleDataSource"/>
    <property name="URL" value="${url}"/>
    <property name="user" value="${username}"/>
    <property name="password" value="${password}"/>
    <property name="validateConnectionOnBorrow" value="true"/>
</bean>

<service ref='myDs' interface='javax.sql.DataSource'>
    <service-properties>
        <entry key='osgi.jndi.service.name' value='jdbc/ds'/>
    </service-properties>
</service>

客户束:

blueprint.xml

<bean id="myController"
      class="MyController">
    <argument ref="entityManagerFactory"/>
</bean>
<reference id="entityManagerFactory"
           interface="javax.persistence.EntityManagerFactory"
           filter="(osgi.unit.name=my-persistent-unit)"
           availability="mandatory"/>

请帮忙。

1 个答案:

答案 0 :(得分:0)

显然每个捆绑包都使用数据源,但没有人创建它。

  

javax.naming.NameNotFoundException:osgi:service / javax.sql.DataSource /“(osgi.jndi.service.name = jdbc / ds)”

您需要创建此数据源,例如将一个简单的XML文件放入deploy dir:

<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="org.postgresql.Driver" />
        <property name="url" value="jdbc:postgresql://localhost:5432/yourdb"/>
        <property name="username" value="XXX"/>
        <property name="password" value="XXX"/>
    </bean>

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

您需要安装 jdbc jndi 功能。