2013-03-18 30 views
3

我使用Spring 3.1.1.RELEASE,JUnit 4.8.1和MySQL 5.5。我想每個測試之前截斷我所有的表中的數據,但我越來越春錯誤,即使我可以執行以下腳本(truncate_tables.sql)通過命令行罰款...使用Spring的「jdbc:initialize-database」,我如何使用存儲過程運行腳本?

drop procedure if exists truncate_tables; 

delimiter # 
create procedure truncate_tables() 
begin 
declare tab_name varchar(64); 
declare done tinyint unsigned default 0; 

declare table_cur cursor for select t.table_name 
from 
    information_schema.schemata s 
    inner join information_schema.tables t on s.schema_name = t.table_schema 
where 
    s.schema_name = database() and t.table_type = 'BASE TABLE'; 

declare continue handler for not found set done = 1; 

open table_cur; 
repeat 
    fetch table_cur into tab_name; 
    set @cmd = concat('truncate table ', tab_name); 

    prepare stmt from @cmd; 
    execute stmt; 
until done end repeat; 

close table_cur; 
end # 

SET FOREIGN_KEY_CHECKS=0; 
CALL truncate_tables; 
SET FOREIGN_KEY_CHECKS=1; 

然而,使用這種Spring應用程序背景...

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
    <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
    <property name="url" value="jdbc:mysql://localhost:3306/my_db" /> 
    <property name="username" value="myuser" /> 
    <property name="password" value="bypass" /> 
</bean> 
... 
<jdbc:initialize-database data-source="dataSource"> 
    <jdbc:script location="classpath:truncate_tables.sql"/> 
    <jdbc:script location="classpath:db-test-data.sql"/>  
</jdbc:initialize-database> 

但是,我得到的錯誤

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near &apos;delimiter # create procedure truncate_tables() begin declare tab_name varchar(6&apos; at line 1 
    at sun.reflect.GeneratedConstructorAccessor419.newInstance(Unknown Source) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513) 
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) 
    at com.mysql.jdbc.Util.getInstance(Util.java:386) 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609) 
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541) 
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2002) 
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2163) 
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2618) 
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2568) 
    at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:842) 
    at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:681) 
    at org.springframework.jdbc.datasource.init.ResourceDatabasePopulator.executeSqlScript(ResourceDatabasePopulator.java:184) 

    ... 50 more 

什麼辦法可以解決上面的腳本?在我的JUnit測試中不能使用「擴展AbstractTransactionalJUnit4SpringContextTests」擴展。

回答

7

<jdbc:script />不允許在包含的腳本中使用DELIMITER語句,但可以使用separator屬性獲得相同的結果。

在你truncate_tables.sql僅使用#分隔符,並在separator財產像這樣指定它:

drop procedure if exists truncate_tables# 

create procedure truncate_tables() 
begin 
declare tab_name varchar(64); 
declare done tinyint unsigned default 0; 

declare table_cur cursor for select t.table_name 
from 
    information_schema.schemata s 
    inner join information_schema.tables t on s.schema_name = t.table_schema 
where 
    s.schema_name = database() and t.table_type = 'BASE TABLE'; 

declare continue handler for not found set done = 1; 

open table_cur; 
repeat 
    fetch table_cur into tab_name; 
    set @cmd = concat('truncate table ', tab_name); 

    prepare stmt from @cmd; 
    execute stmt; 
until done end repeat; 

close table_cur; 
end # 

SET FOREIGN_KEY_CHECKS=0# 
CALL truncate_tables# 
SET FOREIGN_KEY_CHECKS=1# 

,並在應用程序上下文...

<jdbc:initialize-database data-source="dataSource"> 
    <jdbc:script separator="#" location="classpath:truncate_tables.sql"/> 
    <jdbc:script location="classpath:db-test-data.sql"/>  
</jdbc:initialize-database> 
相關問題