简体   繁体   English

捆绑服务组合:未找到org.sqlite.JDBC

[英]Bundle on service mix: org.sqlite.JDBC not found

I have app that is working with SQLite databse. 我有与SQLite数据库一起使用的应用程序。 I packed it as a bundle and i can see the services on service mix. 我把它打包成捆绑,我可以看到服务组合上的服务。 When i am sending request to Post or Get service i am receiving this error: 当我向Post或Get服务发送请求时,我收到此错误:

java.lang.ClassNotFoundException: org.sqlite.JDBC not found java.lang.ClassNotFoundException:找不到org.sqlite.JDBC

I installed SQLite JDBC driver on servicemix but still error. 我在servicemix上安装了SQLite JDBC驱动程序但仍然出错。

This is my POM: 这是我的POM:

<modelVersion>4.0.0</modelVersion>

<groupId>asd</groupId>
<artifactId>name</artifactId>
<version>0.0.1-SNAPSHOT</version>

<packaging>bundle</packaging>
<name>Name</name>
<description>Bundle Desc</description>

<dependencies>
    <dependency>
        <groupId>org.xerial</groupId>
        <artifactId>sqlite-jdbc</artifactId>
        <version>3.15.1</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-core</artifactId>
        <version>3.1.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-transports-http</artifactId>
        <version>3.1.5</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <version>3.3.0</version>
            <extensions>true</extensions>
            <configuration>
                <instructions>
                    <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
                    <Bundle-Description>${project.description}</Bundle-Description>
                    <Import-Package>
                        javax.jws,
                        javax.wsdl,
                        javax.xml.namespace,
                        org.apache.cxf.helpers,
                        org.osgi.service.blueprint,
                        org.xerial.sqlite-jdbc,
                        *
                    </Import-Package>
                    <Export-Package>
                        my.services.package,
                        org.xerial.sqlite-jdbc
                    </Export-Package>
                </instructions>
            </configuration>
        </plugin>
    </plugins>
</build>

I have tried to put this org.xerial.sqlite-jdbc only as a Export package and only as an Import package but did not succeeded. 我试图将此org.xerial.sqlite-jdbc仅作为Export包并仅作为Import包但未成功。

This is the java code for SQLite connection: 这是SQLite连接的java代码:

private void getConnection() throws ClassNotFoundException, SQLException {
    Class.forName("org.sqlite.JDBC");
    con = DriverManager.getConnection("jdbc:sqlite:SQLiteTest1.db");
    initialise();
}

The app works locally but not on the servicemix. 该应用程序在本地运行,但不在servicemix上运行。

Your java code is not suitable for OSGi. 您的java代码不适合OSGi。 By default in OSGi each class is loaded by the classloader of the bundle where it is located. 默认情况下,在OSGi中,每个类都由它所在的包的类加载器加载。

So your own class is loaded by the classloader of your bundle. 所以你自己的类是由你的bundle的类加载器加载的。 As you have an Import-Package statement for org.sqlite your code can access the sqlite driver classes. 由于您有org.sqlite的Import-Package语句,因此您的代码可以访问sqlite驱动程序类。

The problem is that DriverManager loads the classes itself. 问题是DriverManager本身加载了类。 DriverManager is provided by the system bundle (felix framework bundle). DriverManager由系统包(felix框架包)提供。 This bundle of course has no Import-Package for the sqllite. 这个捆绑当然没有sqllite的Import-Package。 So it can not load this class. 所以它无法加载这个类。

There is a simple workaround though. 但是有一个简单的解决方法。 DriverManager allows you to set a thread context classloader. DriverManager允许您设置线程上下文类加载器。 You can set this classloader to the classloader of your own bundle. 您可以将此类加载器设置为您自己的包的类加载器。 This way DriverManager can see the sqllite classes. 这样DriverManager就可以看到sqllite类。 This is only a workaround though. 这只是一种解决方法。

In OSGi the beast way to avoid problems is to simply not load any classes directly. 在OSGi中,避免问题的野兽方法是不直接加载任何类。 In the case of jdbc this can be done by using DataSource classes intead of the DriverManager. 在jdbc的情况下,这可以通过使用DriverManager的DataSource类intead来完成。 See this post . 这篇文章

Another option is to use pax-jdbc. 另一种选择是使用pax-jdbc。 It allows to create DataSource services from config. 它允许从config创建DataSource服务。 This way you can make your bundle independent of the actual DB driver and still avoid manual class loading. 这样,您可以使捆绑包独立于实际的DB驱动程序,并且仍然可以避免手动加载类。 See this example . 看这个例子

You can try like this: 你可以尝试这样:

private void getConnection() throws ClassNotFoundException, SQLException {
  SQLiteDataSource ds = new SQLiteDataSource();
  ds.setUrl("jdbc:sqlite:SQLiteTest1.db");
  try {
   con = ds.getConnection();
   System.out.println("Connected.");
  } catch (Exception e) {
   e.printStackTrace();
  }
  initialise();
 }

According to @Christian Schneider this can be done by using DataSource. 根据@Christian Schneider的说法,这可以通过使用DataSource来完成。

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

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