无法加载JDBC驱动程序类

时间:2017-04-12 09:59:45

标签: apache-camel osgi jbossfuse

我在JBoss Fuse服务器中创建了一个简单的Apache Camel应用程序。 我的骆驼蓝图无法读取驱动程序类。

这是错误消息:

org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class 'oracle.jdbc.driver'
...
Caused by: java.lang.ClassNotFoundException: oracle.jdbc.driver from bundle 144 (fer-cdr)
... 
Caused by: java.lang.ClassNotFoundException: oracle.jdbc.driver not found by jdbccamel
... 

这是项目结构:

+-- Root
+== pom.xml
+-- jdbccamel (Maven Module)
|   +-- pom.xml
|   +-- /src/main/resources/OSGI-INF/blueprint/blueprint.xml
+-- features (Maven Modul => contains fabric feature of the project)
|   +-- pom.xml
|   +-- scr/main/resources/features.xml

pom.xml(jdbccamel)

....
<dependencies>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
    </dependency>
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc6</artifactId>
        <version>11.2.0.3</version>
    </dependency>
    <dependency>
        <groupId>commons-pool</groupId>
        <artifactId>commons-pool</artifactId>
        <version>1.4</version>
    </dependency>
    <dependency>
        <groupId>commons-dbcp</groupId>
        <artifactId>commons-dbcp</artifactId>
        <version>1.4</version>
    </dependency>
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-core</artifactId>
        <version>${camel.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-blueprint</artifactId>
        <version>${camel.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-jdbc</artifactId>
        <version>${camel.version}</version>
    </dependency>
</dependencies>
<build>
    <defaultGoal>install</defaultGoal>
    <plugins>
        <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <version>${version.maven-bundle-plugin}</version>
            <extensions>true</extensions>
            <configuration>
                <instructions>
                    <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
                    <Include-Resource>{maven-resources}</Include-Resource>
                </instructions>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>
....

pom.xml(功能)

....
<dependencies>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc6</artifactId>
        <version>11.2.0.3</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>commons-pool</groupId>
        <artifactId>commons-pool</artifactId>
        <version>1.4</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>commons-dbcp</groupId>
        <artifactId>commons-dbcp</artifactId>
        <version>1.4</version>
        <scope>provided</scope>
    </dependency>
</dependencies>
<build>
    <defaultGoal>install</defaultGoal>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.8</version>
            <executions>
                <execution>
                    <id>attach-artifacts</id>
                    <phase>package</phase>
                    <goals>
                        <goal>attach-artifact</goal>
                    </goals>
                    <configuration>
                        <artifacts>
                            <artifact>
                                <file>target/features/features.xml</file>
                                <type>xml</type>
                                <classifier>features</classifier>
                            </artifact>
                        </artifacts>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
....

features.xml(功能)

....
<?xml version="1.0" encoding="UTF-8"?>
<features name="myfeature" xmlns="http://karaf.apache.org/xmlns/features/v1.0.0">
    <repository>mvn:org.apache.karaf.assemblies.features/standard/2.3.0.redhat-60024/xml/features</repository>
    <repository>mvn:org.apache.karaf.assemblies.features/enterprise/2.3.0.redhat-60024/xml/features</repository>
    <repository>mvn:org.apache.cxf.karaf/apache-cxf/2.7.10/xml/features</repository>
    <repository>mvn:org.apache.camel.karaf/apache-camel/2.12.3/xml/features</repository>
    <repository>mvn:org.apache.activemq/activemq-karaf/5.8.0.redhat-60024/xml/features</repository>
    <feature name="fer-cdr">
        <bundle>wrap:mvn:com.oracle/ojdbc6/11.2.0.3</bundle>
        <bundle>wrap:mvn:mysql/mysql-connector-java/5.1.6</bundle>
        <bundle>wrap:mvn:commons-dbcp/commons-dbcp/1.4</bundle>
        <bundle>wrap:mvn:commons-pool/commons-pool/1.4</bundle>
        <feature version="2.12.3">camel-core</feature>
        <feature version="2.12.3">camel-blueprint</feature>
        <feature version="2.12.3">camel-jdbc</feature>
        <feature version="(3.1,4.0]">spring-jdbc</feature>
        <bundle>mvn:com.project/jdbccamel/1.5.9</bundle>
    </feature>
</features>
....

blueprint.xml(jdbccamel)

....
<bean class="org.apache.commons.dbcp.BasicDataSource" id="dataSource">
    <property name="driverClassName" value="oracle.jdbc.driver" />
    <property name="url" value="${cdr.url}" />
    <property name="username" value="${cdr.username}" />
    <property name="password" value="${cdr.password}" />
    <property name="initialSize" value="${cdr.initialSize}" />
    <property name="maxActive" value="${cdr.maxActive}" />
    <property name="testOnBorrow" value="${cdr.testOnBorrow}" />
    <property name="validationQuery" value="${cdr.validationQuery}" />
</bean>
....

我的发现: 当我在maven-bundle-plugin中导入包时(参见jdbccamel中的pom.xml)

<Import-Package>oracle.jdbc.driver</Import-Package>
像这样:

<plugin>
    <groupId>org.apache.felix</groupId>
    <artifactId>maven-bundle-plugin</artifactId>
    <version>${version.maven-bundle-plugin}</version>
    <extensions>true</extensions>
    <configuration>
        <instructions>
            <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
            <Include-Resource>{maven-resources}</Include-Resource>
        </instructions>
    </configuration>
</plugin>

org.apache.commons.dbcp.BasicDataSource类不会加载,我得到这样的消息:

org.osgi.service.resolver.ResolutionException: Unable to resolve dummy/0.0.0: missing requirement [dummy/0.0.0] osgi.identity; osgi.identity=jdbccamel; type=karaf.feature; version=0 [caused by: Unable to resolve jdbccamel/0.0.0: missing requirement [jdbccamel/0.0.0] osgi.identity; osgi.identity=jdbccamel; type=osgi.bundle; version="[1.5.9,1.5.9]" [caused by: Unable to resolve jdbccamel/1.5.9: missing requirement [jdbccamel/1.5.9] osgi.wiring.package; filter:="(osgi.wiring.package=org.apache.commons.dbcp.BasicDataSource)"]]
  at org.apache.felix.resolver.Candidates.populateResource(Candidates.java:285)
  at org.apache.felix.resolver.Candidates.populate(Candidates.java:153)
  at org.apache.felix.resolver.ResolverImpl.resolve(ResolverImpl.java:148)
  at io.fabric8.agent.DeploymentBuilder.resolve(DeploymentBuilder.java:231)
  at io.fabric8.agent.DeploymentAgent.doUpdate(DeploymentAgent.java:567)
  at io.fabric8.agent.DeploymentAgent$2.run(DeploymentAgent.java:293)
  at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
  at java.util.concurrent.FutureTask.run(FutureTask.java:262)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
  at java.lang.Thread.run(Thread.java:745)

当我添加两者时,如下:

<Import-Package>oracle.jdbc.driver,org.apache.commons.dbcp.BasicDataSource</Import-Package>

它不能像以前一样加载JDBC驱动程序。

1 个答案:

答案 0 :(得分:0)

这在OSGi中效果不佳。 BasicDataSource尝试使用自己没有看到这些类的类加载器来解析oracle类。

最简单的方法是使用OracleDataSource。在这种情况下,没有涉及类加载器魔法,它应该工作。在您直接引用该类时,您的包也将自动获取Import-Package语句。

更好的方法是使用pax-jdbc。 Pax-jdbc为大多数数据库提供包装器,并帮助它们发布DataSourceFactory,这是访问数据库的OSGi标准。

Pax-jdbc-config允许使用plain config从DataSourceFactory创建DataSource。它还允许使用几个池库。

有关小例子,请参阅此example。如果你不需要,你可以省略liquibase部分。