当前位置: 首页 > 知识库问答 >
问题:

从部署在Karaf容器中的OSGi应用程序连接到PostgreSQL

常元章
2023-03-14

我正在使用Hibernate从Karaf 2.3连接到PostgreSQL。4容器,当我部署包时,有一个异常是javax。命名。NameNotFoundException osgi:service/javax。sql。DataSource/“(osgi.jndi.service.name=jdbc/postgresds)。

这是我的坚持。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>

完整的堆栈跟踪是:

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

共有1个答案

籍靖
2023-03-14

您的代码无法工作,因为数据源发布在使用持久性单元的同一蓝图上下文中。

问题是蓝图将等待EnitityManagerFactory服务注入。因此DataSource永远不会被发布。Aries JPA将检测到它应该为您的捆绑包创建一个EntityManagerFactory,但是在尝试查找DataSource时会超时。

因此,将数据源放入一个sperate包中,它应该可以工作。

 类似资料:
  • 我正在开发一个OSGI包,用于使用PDFBox库解析一个PDF文件。我使用maven构建项目,使用Karaf作为OSGI容器。PDFBox库是OSGI兼容的,所以我想这会很容易。但我就是无法正确地建立部署模型。 在一个传统的web应用程序中,我将构建一个包含所有依赖项的WAR-file,并将其放入一个Servlet容器中,然后将其部署。另一方面,我想要安装osgi包的唯一方法是手工安装。我必须创建

  • 喂,伙计们! 这是我的第一次,所以我会尽力做到最好。 我想创建一个应用程序,它是运行与Springboot框架,我想把它连接到一个docker容器嵌入MySQL(但Spring启动应用程序不是运行在docker) 所以我一直关注这个帖子 我已经做了我的docker-comact: 我用这个命令运行它: 一切都很好,所以现在我在Spring靴上改变application.properties: 但当

  • 我需要创建和部署一个应用程序到Karaf。但是我需要使用camel组件和java组件(简单的spring bean)。流程应如下:

  • 我正在使用一个Google Cloud SQL实例(第二代),在授权我自己的IP后,我可以成功地在本地连接到它。我有成功从表中检索行的Go代码,还有成功向表中添加行的代码。但是,当我部署应用程序时,应用程序无法连接到服务器。每当调用SQL函数时,服务器都会返回500响应。我的应用被授权连接到此sql实例: 我正在使用以下Go连接代码: 已部署版本和本地版本使用相同的代码库,但已部署版本使用函数中的

  • 我正在尝试将.war、.aar、.xml和.properties文件部署到tomcat docker容器。我使用了下面的Dockerfile和基本的tomcat docker映像。 到目前为止一切正常。它提取war文件并启动容器内的应用程序。 但是现在我需要像这样添加/编辑“/usr/local/tomcat/webapps/root/web-inf//”和“/usr/local/tomcat/w

  • 我花了几个小时在谷歌上搜索,但似乎找不到正确的路径/文档来帮助我走上正确的道路:( 前提很简单。 我有一个在localhost:8080上打开的springboot应用程序。我有一个在localhost:15672上打开的rabbitmq服务器 当这两个应用程序都运行时,springboot应用程序将通过一些用户交互向Rabbitmq服务器发送消息。当我对这两个独立的服务进行dockerize(容