I am using using Jboss Fuse 6.2.0 and trying establish a jdbc connection from my project. 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. Here is my code in 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 :
<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. 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. Can someone show me the way to make this work ?
DriverManagerDataSource does not work well in OSGi. It uses a classloader that can not see the postgres driver.
Instead you should use pax jdbc.
It allows to define you DataSource in a simple config and automatically adds pooling and XA support. You can then use the DataSource as an OSGi service.
Unofrtunately there is a small bug in the pax-jdbc-postgresql feature. So you will have to install the postgresql driver separately:
install -s mvn:org.postgresql/postgresql/9.4-1200-jdbc41