简体   繁体   English

postgresql驱动程序没有被OSGI容器加载?

[英]postgresql driver not loaded by the OSGI container?

I am using using Jboss Fuse 6.2.0 and trying establish a jdbc connection from my project. 我正在使用Jboss Fuse 6.2.0并尝试从我的项目建立一个jdbc连接。 I am using blueprint.xml to define the postgresql datasource using which I am trying to insert data into the database hosted locally on my system. 我正在使用blueprint.xml来定义postgresql数据源,我正在尝试将数据插入到我的系统本地托管的数据库中。 Here is my code in blueprint.xml : 这是我在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>

I am adding these dependencies below to use this. 我在下面添加这些依赖项来使用它。 Below is the code for my pom.xml : 下面是我的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>

When I am trying to deploy my application in the container(Karaf), I am getting this error. 当我尝试在容器(Karaf)中部署我的应用程序时,我收到此错误。 Find the stack trace below : 找到下面的堆栈跟踪:

>   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$1.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.access$201(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

Here are the list of bundles installed on my container : 以下是我的容器上安装的软件包列表:

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)

I am not able to deploy my bundle. 我无法部署我的捆绑包。 I have added postgres as the dependency. 我添加了postgres作为依赖项。 Can someone show me the way to make this work ? 有人能告诉我如何使这项工作?

DriverManagerDataSource does not work well in OSGi. DriverManagerDataSource在OSGi中不能很好地工作。 It uses a classloader that can not see the postgres driver. 它使用了一个无法看到postgres驱动程序的类加载器。

Instead you should use pax jdbc . 相反,你应该使用pax jdbc

It allows to define you DataSource in a simple config and automatically adds pooling and XA support. 它允许在简单的配置中定义DataSource,并自动添加池和XA支持。 You can then use the DataSource as an OSGi service. 然后,您可以将DataSource用作OSGi服务。

Unofrtunately there is a small bug in the pax-jdbc-postgresql feature. 不幸的是,pax-jdbc-postgresql功能中存在一个小错误。 So you will have to install the postgresql driver separately: 所以你必须单独安装postgresql驱动程序:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM