OSGI 容器未加载 postgresql 驱动程序?

postgresql driver not loaded by the OSGI container?

我正在使用 Jboss Fuse 6.2.0 并尝试从我的项目建立 jdbc 连接。我正在使用 blueprint.xml 来定义 postgresql 数据源,我试图使用它来将数据插入到我系统本地托管的数据库中。这是我在 blueprint.xml 中的代码:

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.postgresql.Driver"/>
        <property name="url" value="jdbc:postgresql://localhost:5432/database-name"/>
        <property name="username" value="admin"/>
        <property name="password" value="admin"/>
    </bean>

    <bean id="transactionManager" class="com.example.TransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

我在下面添加这些依赖项以使用它。下面是我的 pom.xml 的代码:

<dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.4-1200-jdbc41</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.enterprise</artifactId>
            <version>4.2.0</version>
        </dependency>

当我尝试在容器 (Karaf) 中部署我的应用程序时,出现此错误。在下面找到堆栈跟踪:

>   org.osgi.service.blueprint.container.ComponentDefinitionException:
> Error setting property: PropertyDescriptor <name: driverClassName,
> getter: null, setter: [class
> org.springframework.jdbc.datasource.DriverManagerDataSource.setDriverClassName(class
> java.lang.String)]
>       at org.apache.aries.blueprint.container.BeanRecipe.setProperty(BeanRecipe.java:941)[21:org.apache.aries.blueprint.core:1.4.2]
>       at org.apache.aries.blueprint.container.BeanRecipe.setProperties(BeanRecipe.java:907)[21:org.apache.aries.blueprint.core:1.4.2]
>       at org.apache.aries.blueprint.container.BeanRecipe.setProperties(BeanRecipe.java:888)[21:org.apache.aries.blueprint.core:1.4.2]
>       at org.apache.aries.blueprint.container.BeanRecipe.internalCreate2(BeanRecipe.java:820)[21:org.apache.aries.blueprint.core:1.4.2]
>       at org.apache.aries.blueprint.container.BeanRecipe.internalCreate(BeanRecipe.java:787)[21:org.apache.aries.blueprint.core:1.4.2]
>       at org.apache.aries.blueprint.di.AbstractRecipe.call(AbstractRecipe.java:79)[21:org.apache.aries.blueprint.core:1.4.2]
>       at java.util.concurrent.FutureTask.run(FutureTask.java:266)[:1.8.0_45]
>       at org.apache.aries.blueprint.di.AbstractRecipe.create(AbstractRecipe.java:88)[21:org.apache.aries.blueprint.core:1.4.2]
>       at org.apache.aries.blueprint.container.BlueprintRepository.createInstances(BlueprintRepository.java:245)[21:org.apache.aries.blueprint.core:1.4.2]
>       at org.apache.aries.blueprint.container.BlueprintRepository.createAll(BlueprintRepository.java:183)[21:org.apache.aries.blueprint.core:1.4.2]
>       at org.apache.aries.blueprint.container.BlueprintContainerImpl.instantiateEagerComponents(BlueprintContainerImpl.java:682)[21:org.apache.aries.blueprint.core:1.4.2]
>       at org.apache.aries.blueprint.container.BlueprintContainerImpl.doRun(BlueprintContainerImpl.java:377)[21:org.apache.aries.blueprint.core:1.4.2]
>       at org.apache.aries.blueprint.container.BlueprintContainerImpl.run(BlueprintContainerImpl.java:269)[21:org.apache.aries.blueprint.core:1.4.2]
>       at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)[:1.8.0_45]
>       at java.util.concurrent.FutureTask.run(FutureTask.java:266)[:1.8.0_45]
>       at org.apache.aries.blueprint.container.ExecutorServiceWrapper.run(ExecutorServiceWrapper.java:106)[21:org.apache.aries.blueprint.core:1.4.2]
>       at org.apache.aries.blueprint.utils.threading.impl.DiscardableRunnable.run(DiscardableRunnable.java:48)[21:org.apache.aries.blueprint.core:1.4.2]
>       at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)[:1.8.0_45]
>       at java.util.concurrent.FutureTask.run(FutureTask.java:266)[:1.8.0_45]
>       at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access1(ScheduledThreadPoolExecutor.java:180)[:1.8.0_45]
>       at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)[:1.8.0_45]
>       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)[:1.8.0_45]
>       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)[:1.8.0_45]
>       at java.lang.Thread.run(Thread.java:745)[:1.8.0_45]
>     Caused by: java.lang.IllegalStateException: Could not load JDBC driver class [org.postgresql.Driver]
>       at org.springframework.jdbc.datasource.DriverManagerDataSource.setDriverClassName(DriverManagerDataSource.java:150)
>       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)[:1.8.0_45]
>       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)[:1.8.0_45]
>       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)[:1.8.0_45]
>       at java.lang.reflect.Method.invoke(Method.java:497)[:1.8.0_45]
>       at org.apache.aries.blueprint.utils.ReflectionUtils$MethodPropertyDescriptor.internalSet(ReflectionUtils.java:628)[21:org.apache.aries.blueprint.core:1.4.2]
>       at org.apache.aries.blueprint.utils.ReflectionUtils$PropertyDescriptor.set(ReflectionUtils.java:378)[21:org.apache.aries.blueprint.core:1.4.2]
>       at org.apache.aries.blueprint.container.BeanRecipe.setProperty(BeanRecipe.java:939)[21:org.apache.aries.blueprint.core:1.4.2]
>       ... 23 more
>     Caused by: java.lang.ClassNotFoundException: org.postgresql.Driver
>       at java.net.URLClassLoader.findClass(URLClassLoader.java:381)[:1.8.0_45]
>       at java.lang.ClassLoader.loadClass(ClassLoader.java:424)[:1.8.0_45]
>       at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)[:1.8.0_45]
>       at java.lang.ClassLoader.loadClass(ClassLoader.java:357)[:1.8.0_45]
>       at java.lang.Class.forName0(Native Method)[:1.8.0_45]
>       at java.lang.Class.forName(Class.java:348)[:1.8.0_45]
>       at org.springframework.jdbc.datasource.DriverManagerDataSource.setDriverClassName(DriverManagerDataSource.java:147)
>       ... 30 more

这是我的容器上安装的包列表:

hawtio :: project (1.4.0.redhat-133)
 JBoss Fuse :: Support :: Fabric8 (1.2.0.redhat-133)
 hawtio :: Red Hat Fuse Branding (1.4.0.redhat-133)
 jsr311-api (1.1.1)
 Apache ServiceMix :: Bundles :: jdom (1.1.0.4)
 Apache ServiceMix :: Bundles :: oro (2.0.8.6)
 Apache ServiceMix :: Bundles :: ant (1.7.0.6)
 camel-velocity (2.15.1.redhat-620133)
 wrap_mvn_org.springframework_spring-jdbc_3.2.3.RELEASE (0)
 osgi.enterprise (4.2.0.201003190513)

我无法部署我的包。我添加了 postgres 作为依赖项。有人可以告诉我如何完成这项工作吗?

DriverManagerDataSource 在 OSGi 中运行不佳。它使用了一个看不到 postgres 驱动程序的类加载器。

您应该使用 pax jdbc

它允许您在简单的配置中定义数据源,并自动添加池化和 XA 支持。然后您可以将数据源用作 OSGi 服务。

不幸的是,pax-jdbc-postgresql 特性中有一个小错误。所以你必须单独安装 postgresql 驱动程序:

install -s mvn:org.postgresql/postgresql/9.4-1200-jdbc41