SQLite + Mybatis-guice抛出ArrayIndexOutOfBoundsException

时间:2017-01-31 22:57:31

标签: sqlite jdbc guice mybatis guacamole

我尝试实现一个Guice模块,让Guacamole使用SQLite作为后端。 Guacamole项目有一个通用的JDBC基础模块。这使您可以使用更少的代码实现特定数据存储的模块。大多数代码行最终都在映射器XML文件中。该项目提供了PostgreSQL and MySQL implementations

我将这个SQLite模块基于MySQL模块。对于映射器XML文件,SQLite和MySQL足够相似,我没有做任何更改。但是,当我尝试使用SQLite模块时,我收到此错误:

### Error querying database.  Cause: java.lang.ArrayIndexOutOfBoundsException: 2
### The error may exist in org/apache/guacamole/auth/jdbc/connectiongroup/ConnectionGroupMapper.xml
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: SELECT             guacamole_connection_group.connection_group_id,             connection_group_name,             parent_id,             type,             max_connections,             max_connections_per_user,             enable_session_affinity         FROM guacamole_connection_group         JOIN guacamole_connection_group_permission ON guacamole_connection_group_permission.connection_group_id = guacamole_connection_group.connection_group_id         WHERE guacamole_connection_group.connection_group_id IN              (                   ?              )              AND user_id = ?             AND permission = 'READ';          SELECT parent_id, guacamole_connection_group.connection_group_id         FROM guacamole_connection_group         JOIN guacamole_connection_group_permission ON guacamole_connection_group_permission.connection_group_id = guacamole_connection_group.connection_group_id         WHERE parent_id IN              (                   ?              )              AND user_id = ?             AND permission = 'READ';          SELECT parent_id, guacamole_connection.connection_id         FROM guacamole_connection         JOIN guacamole_connection_permission ON guacamole_connection_permission.connection_id = guacamole_connection.connection_id         WHERE parent_id IN              (                   ?              )              AND user_id = ?             AND permission = 'READ';
### Cause: java.lang.ArrayIndexOutOfBoundsException: 2

看起来问题是两个参数传递给查询,但每个参数重复三次。当MyBatis生成PreparedStatement时,它就好像有六个参数需要传入。

以下是有问题的查询:

<!-- Select multiple connection groups by identifier only if readable -->
<select id="selectReadable" resultMap="ConnectionGroupResultMap"
        resultSets="connectionGroups,childConnectionGroups,childConnections">

    SELECT
        guacamole_connection_group.connection_group_id,
        connection_group_name,
        parent_id,
        type,
        max_connections,
        max_connections_per_user,
        enable_session_affinity
    FROM guacamole_connection_group
    JOIN guacamole_connection_group_permission ON guacamole_connection_group_permission.connection_group_id = guacamole_connection_group.connection_group_id
    WHERE guacamole_connection_group.connection_group_id IN
        <foreach collection="identifiers" item="identifier"
                 open="(" separator="," close=")">
            #{identifier,jdbcType=VARCHAR}
        </foreach>
        AND user_id = #{user.objectID,jdbcType=INTEGER}
        AND permission = 'READ';

    SELECT parent_id, guacamole_connection_group.connection_group_id
    FROM guacamole_connection_group
    JOIN guacamole_connection_group_permission ON guacamole_connection_group_permission.connection_group_id = guacamole_connection_group.connection_group_id
    WHERE parent_id IN
        <foreach collection="identifiers" item="identifier"
                 open="(" separator="," close=")">
            #{identifier,jdbcType=VARCHAR}
        </foreach>
        AND user_id = #{user.objectID,jdbcType=INTEGER}
        AND permission = 'READ';

    SELECT parent_id, guacamole_connection.connection_id
    FROM guacamole_connection
    JOIN guacamole_connection_permission ON guacamole_connection_permission.connection_id = guacamole_connection.connection_id
    WHERE parent_id IN
        <foreach collection="identifiers" item="identifier"
                 open="(" separator="," close=")">
            #{identifier,jdbcType=VARCHAR}
        </foreach>
        AND user_id = #{user.objectID,jdbcType=INTEGER}
        AND permission = 'READ';

</select>

如果我手动填充参数,我可以对SQLite数据库执行此操作。此外,MySQL版本工作正常。

到底发生了什么事?我该怎么做才能调试这个?它是MyBatis问题还是JDBC连接器的问题?

如果有帮助,您可以看到模块here的代码。

这是映射器与此查询相关的参数的方法。 ConnectionGroup的完整映射器类是herehere。我的SQLite模块的完整映射器XML是here

Collection<ModelType> selectReadable(@Param("user") UserModel user,
        @Param("identifiers") Collection<String> identifiers);

这就是ConnectionGroupResultMap的样子:

<resultMap id="ConnectionGroupResultMap" type="org.apache.guacamole.auth.jdbc.connectiongroup.ConnectionGroupModel" >

    <!-- Connection group properties -->
    <id     column="connection_group_id"      property="objectID"               jdbcType="INTEGER"/>
    <result column="connection_group_name"    property="name"                   jdbcType="VARCHAR"/>
    <result column="parent_id"                property="parentIdentifier"       jdbcType="INTEGER"/>
    <result column="type"                     property="type"                   jdbcType="VARCHAR"
            javaType="org.apache.guacamole.net.auth.ConnectionGroup$Type"/>
    <result column="max_connections"          property="maxConnections"         jdbcType="INTEGER"/>
    <result column="max_connections_per_user" property="maxConnectionsPerUser"  jdbcType="INTEGER"/>
    <result column="enable_session_affinity"  property="sessionAffinityEnabled" jdbcType="BOOLEAN"/>

    <!-- Child connection groups -->
    <collection property="connectionGroupIdentifiers" resultSet="childConnectionGroups" ofType="java.lang.String"
                column="connection_group_id" foreignColumn="parent_id">
        <result column="connection_group_id"/>
    </collection>

    <!-- Child connections -->
    <collection property="connectionIdentifiers" resultSet="childConnections" ofType="java.lang.String"
                column="connection_group_id" foreignColumn="parent_id">
        <result column="connection_id"/>
    </collection>

</resultMap>

1 个答案:

答案 0 :(得分:1)

大卫,

我知道这有点旧,但我也试图实现一个SQLite JDBC模块,并遇到了完全相同的问题。我设法找到了问题的根源,并在JDBC SQLite github页面上提出了一个问题:

https://github.com/xerial/sqlite-jdbc/issues/277

基本上,SQLite JDBC驱动程序根据预准备语句的参数计数来计算其数组大小之一。但是,在您提到的情况下,有多个SELECT语句采用相同的参数,因此数组需要是x * y(x =参数计数,y = select语句的数量),而不仅仅是参数的数量。当它尝试准备语句时,它会超出参数计数的第一个位置并生成此异常。

其他JDBC模块--MySQL,PostgreSQL和SQL Server,以及我正在搞砸的Oracle和H2 - 似乎正确处理了这种情况(好吧,Oracle有点......特别.. 。),但SQLite驱动程序没有。

通过创建两个不同的结果映射,一个用于泛型选择,一个用于检查READ权限的读取,然后分解每个选择,我能够以真正,非常复杂的方式解决该问题。查询自己的SELECT块并从结果映射中的集合中调用它们。它没有现有代码那么优雅,但它确实有效。