带有 JdbcTemplate 的 HSQLDB,没有任何东西被保存

HSQLDB with JdbcTemplate, nothing is getting saved

出于某种原因,在对基于文件的 HSQL 数据库进行更改并关闭 java 进程后,数据库中似乎没有保存任何内容。 IE。我可以一遍又一遍地重新运行这个程序而不会遇到 "table already exists" 异常。这到底是怎么回事?!

主要class:

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

import java.io.IOException;
import java.sql.SQLException;

public class TestApp {

    public static void main(String[] args) throws IOException, SQLException, ClassNotFoundException {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(DbConfig.class, TestDao.class);
        JdbcTemplate template = ctx.getBean(JdbcTemplate.class);
        TestDao dao = ctx.getBean(TestDao.class);
        dao.testTransactionality();
    }
}

配置:

import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

@Configuration
public class DbConfig {

    @Bean
    public DataSource getDataSource(){
        BasicDataSource ds = new BasicDataSource();
        ds.setDriverClassName("org.hsqldb.jdbcDriver");
        ds.setUrl("jdbc:hsqldb:file:databaseFiles/test/");
        ds.setUsername("sa");
        ds.setPassword("1");
        return ds;
    }

    @Bean
    JdbcTemplate getJdbcTemplate(DataSource ds){
        return new JdbcTemplate(ds);
    }

    @Bean
    PlatformTransactionManager getTransactionManager(DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }
}

道:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;

@Repository
@EnableTransactionManagement
@Transactional
public class TestDao {

    @Autowired
    private JdbcTemplate template;

    @Transactional
    public void testTransactionality(){
        template.execute("create table LIBRARY (LIBRARY_ID INT, LIBRARY_TITLE VARCHAR(400))");
        template.execute("insert into library values (1, 'Library')");
    }
}

我试过用普通的 JDBC classes 做一些类似的事情以及做显式提交,似乎没有任何帮助。我猜这是一个 HSQLDB 问题。请帮忙

您的数据库 URL 不太正确(不应以斜杠结尾)。您还应该将写入延迟更改为 0 以查看更改:

ds.setUrl("jdbc:hsqldb:file:databaseFiles/test;hsqldb.write_delay_millis=0");