<jdbc:embedded-database> in context.xml does not work. Want to to start an sql script for unit testing

21,514

Solution 1

Just to complete the answers, that's the context which works for me (Spring 3.2.1):

<beans xmlns="http://www.springframework.org/schema/beans"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:jdbc="http://www.springframework.org/schema/jdbc"
            xsi:schemaLocation="http://www.springframework.org/schema/beans
                                                    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                                                    http://www.springframework.org/schema/jdbc
                                                    http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">
    <jdbc:embedded-database id="dataSource" type="HSQL">
        <jdbc:script location="classpath:schema.sql" />
        <jdbc:script location="classpath:data.sql" />
    </jdbc:embedded-database>
</beans>

Solution 2

Fixed this by moving the definition xmlns:jdbc="http://www.springframework.org/schema/jdbc" to the top and by adding a version number "spring-jdbc-3.0.xsd" to the definition.

Solution 3

Can you confirm that you have spring-jdbc jar in your classpath

Share:
21,514
nook
Author by

nook

Updated on March 08, 2020

Comments

  • nook
    nook about 4 years

    Here is my context file.:

    <beans default-lazy-init="true" xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd"
           xmlns:jdbc="http://www.springframework.org/schema/jdbc"> <!--Line 11-->
    
    <tx:annotation-driven transaction-manager="transactionManager"/>
    
    
    
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">    <!---->
        <property name="driverClassName" value="org.hsqldb.jdbc.JDBCDriver"/>
        <property name="url" value="jdbc:hsqldb:mem:mydb"/>
        <property name="username" value="sa"/>
        <property name="password" value=""/>
    </bean>
    
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="hibernate.cfg.xml.incDTD"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.connection.shutdown">true</prop>
            </props>
        </property>
    </bean>
    
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
     </bean>
    
    <jdbc:embedded-database id="embedded" type="HSQL"/>
    <jdbc:initialize-database data-source="dataSource">
        <jdbc:script location="classpath:ctl_data-scrubd.sql"/>
    </jdbc:initialize-database>
    

    I get this error

    Caused by: org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 11 in XML document from class path resource [PersistenceHelper-context.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 11; columnNumber: 64; <Line 11, Column 64>: XML-24500: (Error) Can not build schema 'http://www.springframework.org/schema/jdbc' located at 'http://www.springframework.org/schema/jdbc/spring-jdbc.xsd'
    

    I want to start an sql script for testing. I need to start it from inside this xml file. I have read using by it does not work. Does anyone know how I can fix this problem, or an alternative way to start an .sql file from inside. If you want to see more of my code, please let me know.

    Edit::

    I posted this same question on another site, and I got some suggestions. The error is now:

    at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:76)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean   with name 'org.springframework.jdbc.datasource.init.DataSourceInitializer#0': Invocation of init method failed; nested exception is org.springframework.dao.DataAccessResourceFailureException: Failed to execute database script; nested exception is org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement at line 1 of resource class path resource []: bin
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean
    

    Here is my config file now as well. All I did was add a version and move some of the declarations around:

    <?xml version="1.0" encoding="UTF-8"?>
    <!-This is the spring configuration file for test cases.-->
    <beans default-lazy-init="true" xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/jdbc
    http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">
    
    <tx:annotation-driven transaction-manager="transactionManager"/>
    

    All the rest is the same.

  • nook
    nook almost 12 years
    Yes I can. I am using intelliJ and I see it in my lib folder.
  • Biju Kunjummen
    Biju Kunjummen almost 12 years
    Oh, okay, so you were missing the <?xml version="1.0" encoding="UTF-8"?> , I had assumed that you had it in you were showing a snippet without the top part. Regarding your new issue - I think it is because there is something wrong with the content of your sql script file. Another issue that I see is that you have named your embedded datasource 'embedded' but your intializer is running against a datasource called datasource, is this correct, ideally you would want to run the script against the embedded datasource alone right?
  • nook
    nook almost 12 years
    I highly doubt it is my sql script, because it has been run many times before I started to introduce the spring framework into our program. Could you explain what you mean by the second part? My dataSource holds the appropriate values of the Hibernate database to create it ("I want to use the word schema here, but I am not completely sure if that is what it is"). The sql script has the values to insert into those tables. Ideally I want to create the table, then run the script against the table to populate it, if that makes sense.
  • Chris Sim
    Chris Sim about 8 years