Hsqldb存储过程

时间:2015-02-24 18:50:36

标签: java spring testing junit hsqldb

我正在尝试在内存Hsqldb数据库中设置存储过程以进行测试。我正在使用的存储过程是在MySql中开发的,所以我想用HSqlDb设置它以适应我的测试套件

我正在尝试创建一个简化版本的程序,但目前尚无欢乐。

程序

     CREATE PROCEDURE p_recordTaskExecution(IN userTaskId INT, IN isSuccess BOOLEAN, IN statusMessage VARCHAR(2000), IN operationsPerformed INT, INOUT procedureStatus BOOLEAN) 
  BEGIN ATOMIC
  IF userTaskId = 1 Then
        set procedureStatus = true;

  ELSE
    set procedureStatus = false;

  END IF;

  END;

运行测试时遇到的错误如下

Caused by: org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement at line 3 of resource class path resource [databaseTesting/inMemory/createInMemoryDatabase.sql]: CREATE PROCEDURE p_recordTaskExecution(IN userTaskId INT, IN isSuccess BOOLEAN, IN statusMessage VARCHAR(2000), IN operationsPerformed INT, INOUT procedureStatus BOOLEAN) BEGIN ATOMIC if userTaskId = 1 Then set procedureStatus = true; nested exception is java.sql.SQLSyntaxErrorException: unexpected end of statement:  required: ;
    at org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:475)
    at org.springframework.jdbc.datasource.init.ResourceDatabasePopulator.populate(ResourceDatabasePopulator.java:229)
    at org.springframework.jdbc.datasource.init.CompositeDatabasePopulator.populate(CompositeDatabasePopulator.java:60)
    at org.springframework.jdbc.datasource.init.DatabasePopulatorUtils.execute(DatabasePopulatorUtils.java:48)
    at org.springframework.jdbc.datasource.init.DataSourceInitializer.execute(DataSourceInitializer.java:108)
    at org.springframework.jdbc.datasource.init.DataSourceInitializer.afterPropertiesSet(DataSourceInitializer.java:93)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1625)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1562)
    ... 40 more
Caused by: java.sql.SQLSyntaxErrorException: unexpected end of statement:  required: ;
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCStatement.fetchResult(Unknown Source)
    at org.hsqldb.jdbc.JDBCStatement.execute(Unknown Source)
    at com.jolbox.bonecp.StatementHandle.execute(StatementHandle.java:254)
    at org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:460)
    ... 47 more
Caused by: org.hsqldb.HsqlException: unexpected end of statement:  required: ;

3 个答案:

答案 0 :(得分:3)

由于以下stackoverflow链接,我终于得到了答案 HSQL Create Procedure Syntax doesn't seem to match the documentation

为了完整性,这就是我做的事情

  1. 我将hsqldb sql代码分成2个文件。 1只创建表和数据语法,另一个用存储过程

  2. 在此文件中加载时,我可以指定要使用的分隔符

    <jdbc:initialize-database data-source="mctDBDataSource" ignore-failures="DROPS" >
        <jdbc:script location="classpath:databaseTesting/inMemory/createInMemoryDatabase.sql" separator=";"/>
        <jdbc:script location="classpath:databaseTesting/inMemory/createInMemoryDatabaseProcedures.sql" separator="/;"/>
    </jdbc:initialize-database>
    

    。 如前所述,Spring治疗半结肠;作为语句的结尾,所以我能够指定一个不同的分隔符用于我的存储过程文件

  3. 更新了我的存储过程以使用新的分隔符,现在一切都按预期工作

    CREATE PROCEDURE p_recordTaskExecution(IN userTaskId BIGINT, IN isSuccess BOOLEAN, IN statusMessage VARCHAR(2000), IN operationsPerformed BIGINT, INOUT procedureStatus BOOLEAN) 
    BEGIN ATOMIC
        IF userTaskId = 1 Then
            SET procedureStatus = true; 
        ELSE
            SET procedureStatus = false;
        END IF;
    END;
    /;
    

答案 1 :(得分:1)

这个与指定的@Damo相同但是避免了XML配置。

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to java.util.List

这是方法签名:

ScriptUtils.executeSqlScript(ds.getConnection(),  new EncodedResource(
                new InputStreamResource(getClass().getClassLoader().getSystemResourceAsStream("procs.sql"))
                ), false, false, "--", "/;", "--", "--");

答案 2 :(得分:0)

END IFEND之后,您不需要分号。错误消息有点误导。

修改的 您可能还需要在CREATE PROCEDURE...文本中加倍分号,因为Spring本身也将分号视为语句分隔符。