在具有H2嵌入式数据库的Android设备上使用JDBC连接池

时间:2018-10-03 08:09:23

标签: java android jdbc h2 connection-pool

我正在开发Android应用程序使用的嵌入式Java应用程序(jar)。 (Android设备(API19)只是视图,我将带有Gradle的JAR导入到Android Studio的项目中。)

我正在使用H2数据库(在Android上经过测试),并且试图将其与连接池一起使用(尝试C3P0HakiriCP)。

这是jar pom文件的内容(使用HakiriCP):

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.app</groupId>
    <artifactId>reactor</artifactId>
    <version>1.0-SNAPSHOT</version>


    <properties>
        <log4j.version>1.2.15</log4j.version>
        <rx.version>1.3.8</rx.version>
        <dbh2.version>1.4.196</dbh2.version>
        <junit.version>4.7</junit.version>
        <json.version>20180813</json.version>
        <slf4j-log4j12.version>1.7.21</slf4j-log4j12.version>
        <log4j.version>1.2.17</log4j.version>
    </properties>

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>3.1.1</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/alternateLocation</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                            <includeScope>provided</includeScope>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.1.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <finalName>uber-${artifactId}-${version}</finalName>
                </configuration>
            </plugin>
        </plugins>
    </build>


    <dependencies>
        <!-- https://mvnrepository.com/artifact/io.reactivex/rxjava -->
        <dependency>
            <groupId>io.reactivex</groupId>
            <artifactId>rxjava</artifactId>
            <version>${rx.version}</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>${dbh2.version}</version>
        </dependency>
        <dependency>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP-java7</artifactId>
            <version>2.4.13</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.json/json -->
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>${json.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j-log4j12.version}</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j-log4j12.version}</version>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/log4j/apache-log4j-extras -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>apache-log4j-extras</artifactId>
            <version>${log4j.version}</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.ghgande</groupId>
            <artifactId>j2mod</artifactId>
            <version>2.5.0</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/src/main/resources/j2mod.jar</systemPath>
        </dependency>
    </dependencies>


</project>

要构建jar,我使用了maven插件(assembly,one-jar ...)来确保所有依赖项都在类路径中。

当我运行JAR时,一切正常,但是当我尝试运行Android应用程序(带有该jar)时,出现了一些错误。 这是我的Android项目Gradle文件的内容:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion '27.0.3'
    defaultConfig {
        applicationId 'com.app.android'
        minSdkVersion 19
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    dataBinding {
        enabled = true
    }
    productFlavors {
    }
}

dependencies {
    def lifecycle_version = "1.1.1"
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    implementation 'com.android.support:recyclerview-v7:26.1.0'
    implementation 'com.android.support:design:26.1.0'
    implementation "android.arch.lifecycle:extensions:$lifecycle_version"
    implementation (group: 'com.zaxxer', name: 'HikariCP-java7', version: '2.4.13')
    implementation files('libs/reactor.jar')
}

我遇到的错误主要是Class-not-found Exception,例如: Caused by: java.lang.ClassNotFoundException: Class io.prometheus.client.Collector not found

HakiriCP中有一些依赖项,它们在provided范围内,因此我尝试将这些依赖项导入我的Android gradle文件中,但仍然出现诸如以下错误:

10-03 10:55:17.960 18268-18281/com.atlantium.aio.android E/dalvikvm: Could not find class 'javax.naming.InitialContext', referenced from method com.zaxxer.hikari.HikariConfig.getObjectOrPerformJndiLookup
10-03 10:55:17.960 18268-18281/com.atlantium.aio.android W/dalvikvm: VFY: unable to resolve new-instance 4434 (Ljavax/naming/InitialContext;) in Lcom/zaxxer/hikari/HikariConfig;
10-03 10:55:17.960 18268-18281/com.atlantium.aio.android D/dalvikvm: VFY: replacing opcode 0x22 at 0x0004
10-03 10:55:17.960 18268-18281/com.atlantium.aio.android W/dalvikvm: VFY: unable to resolve exception class 4437 (Ljavax/naming/NamingException;)
    VFY: unable to find exception handler at addr 0x11
    VFY:  rejected Lcom/zaxxer/hikari/HikariConfig;.getObjectOrPerformJndiLookup (Ljava/lang/Object;)Ljava/lang/Object;
    VFY:  rejecting opcode 0x0d at 0x0011
    VFY:  rejected Lcom/zaxxer/hikari/HikariConfig;.getObjectOrPerformJndiLookup (Ljava/lang/Object;)Ljava/lang/Object;
    Verifier rejected class Lcom/zaxxer/hikari/HikariConfig;

有什么我想念的吗?

是否可以在android上使用JDBC连接池? (根据Establish pooled connection to MySQL in android,虽然有可能,但仍然无法正常工作。)

也许有人可以推荐另一个在android上测试过的连接池?

谢谢

更新

根据H2文档:

  

当前仅支持JDBC API(计划在将来的版本中支持Android数据库API)……

     

限制:由于所需的javax.sql,当前不支持使用连接池。类在Android上不可用。

0 个答案:

没有答案