derby 嵌入式数据库不采用来自 application.properties 的路径

derby embedded database does not take path from application.properties

我想让 derby 持久化,但在 spring 批处理中在 application.properties 处给出路径,但是 derby 正在初始化为内存中的方式而没有识别路径。无论如何我可以指定路径在嵌入式 derby 的数据源中

这里是 application.properties

spring.batch.initialize-schema=ALWAYS
spring.datasource.url=jdbc:derby:c:/new123/mydb;create=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.DerbyTenSevenDialect
spring.jpa.hibernate.ddl-auto=create

这里是配置class

package com.example.demo;

import java.io.FileNotFoundException;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;
import javax.xml.stream.XMLStreamException;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.database.ItemPreparedStatementSetter;
import org.springframework.batch.item.database.JdbcBatchItemWriter;
import org.springframework.batch.item.xml.StaxEventItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.jdbc.datasource.embedded.ConnectionProperties;
import org.springframework.jdbc.datasource.embedded.DataSourceFactory;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.oxm.xstream.XStreamMarshaller;

import com.example.demo.maintablelogic.XmlMainTable;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.mapper.CannotResolveClassException;
import com.thoughtworks.xstream.mapper.MapperWrapper;

@Configuration
@EnableBatchProcessing
@PropertySource("application.properties")
public class BatchConfiguration {

    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    @Autowired
    private DataSource dataSource;

    
      @Bean public DataSource dataSource() { EmbeddedDatabaseBuilder builder = new
      EmbeddedDatabaseBuilder(); EmbeddedDatabase db =
      builder.setType(EmbeddedDatabaseType.DERBY).addScript("test.sql").build();
      return db;
     
      }
    
    @Bean
    public StaxEventItemReader<MessageDto> reader() {
        StaxEventItemReader<MessageDto> reader = new StaxEventItemReader<MessageDto>();
        reader.setResource(new ClassPathResource("ssa_msg_list.xml"));
        reader.setFragmentRootElementName("Message");

        Map<String, String> aliases = new HashMap<String, String>();
        aliases.put("Message", "com.example.demo.MessageDto");
        CustomXStreamMarshaller xStreamMarshaller = new CustomXStreamMarshaller();
        xStreamMarshaller.setAliases(aliases);
        reader.setUnmarshaller(xStreamMarshaller);

        return reader;
    }

    @Bean
    public JdbcBatchItemWriter<MessageDto> writer() {
        JdbcBatchItemWriter<MessageDto> writer = new JdbcBatchItemWriter<MessageDto>();
        writer.setDataSource(dataSource);

        writer.setSql(
                "INSERT INTO ssa_msg_list (Uumid,UumidSuffix,BIC,Service, Status,FormatName,Message_Identifier, Date,Subformat, CreationDate, UETR,MURID) values (?,?,?,?,?,?,?,?,?,?,?,?)");
        writer.setItemPreparedStatementSetter(new UserItemPreparedStmSetter());
        

        return writer;
    }

    private class UserItemPreparedStmSetter implements ItemPreparedStatementSetter<MessageDto> {

        @Override
        public void setValues(MessageDto user, PreparedStatement ps) throws SQLException {
            XmlMainTable obj = new XmlMainTable();
            try {
                obj.createmaintable(user, ps);
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (XMLStreamException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

    @Bean
    public Step step1() {
        return stepBuilderFactory.get("step1").<MessageDto, MessageDto>chunk(10).reader(reader()).writer(writer())
                .build();
    }

    @Bean
    public Job importUserJob() {
        return jobBuilderFactory.get("importUserJob").incrementer(new RunIdIncrementer()).flow(step1()).end().build();

    }
}

这里是控制台日志

( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.4.0)

2020-11-26 14:42:40.694  INFO 17552 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication using Java 15.0.1 on LAPTOP-RODPBL05 with PID 17552 (C:\Users\shanil\Desktop\demov4\demo\target\classes started by shanil in C:\Users\shanil\Desktop\demov4\demo)
2020-11-26 14:42:40.696  INFO 17552 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to default profiles: default
2020-11-26 14:42:41.586  INFO 17552 --- [           main] o.s.j.d.e.EmbeddedDatabaseFactory        : Starting embedded database: url='jdbc:derby:memory:testdb;create=true', username='sa'
successsfullly
2020-11-26 14:42:42.525  INFO 17552 --- [           main] o.s.b.c.r.s.JobRepositoryFactoryBean     : No database type set, using meta data indicating: DERBY
2020-11-26 14:42:42.714  INFO 17552 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : No TaskExecutor has been set, defaulting to synchronous executor.
2020-11-26 14:42:42.824  INFO 17552 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 2.554 seconds (JVM running for 2.971)
2020-11-26 14:42:42.827  INFO 17552 --- [           main] o.s.b.a.b.JobLauncherApplicationRunner   : Running default command line with: []
2020-11-26 14:42:43.057  INFO 17552 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : Job: [FlowJob: [name=importUserJob]] launched with the following parameters: [{run.id=1}]
2020-11-26 14:42:43.178  INFO 17552 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Executing step: [step1]
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.thoughtworks.xstream.core.util.Fields (file:/C:/Users/shanil/.m2/repository/com/thoughtworks/xstream/xstream/1.4.11.1/xstream-1.4.11.1.jar) to field java.util.TreeMap.comparator
WARNING: Please consider reporting this to the maintainers of com.thoughtworks.xstream.core.util.Fields
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
Security framework of XStream not initialized, XStream is probably vulnerable.
2020-11-26 14:42:44.070  INFO 17552 --- [           main] o.s.batch.core.step.AbstractStep         : Step: [step1] executed in 891ms
2020-11-26 14:42:44.085  INFO 17552 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : Job: [FlowJob: [name=importUserJob]] completed with the following parameters: [{run.id=1}] and the following status: [COMPLETED] in 1s1ms
2020-11-26 14:42:44.089  INFO 17552 --- [extShutdownHook] o.s.j.d.e.EmbeddedDatabaseFactory        : Shutting down embedded database: url='jdbc:derby:memory:testdb;create=true'

原因是您定义了以下嵌入式数据源:

  @Bean 
  public DataSource dataSource() {
     EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
     EmbeddedDatabase db = builder.setType(EmbeddedDatabaseType.DERBY)
         .addScript("test.sql")
         .build();
     return db;
  }

您需要删除该 bean 并让 Spring Boot 从 application.properties 为您创建数据源,或者定义一个常规数据源(不是嵌入式数据源),例如:

@Bean
public DataSource datasource() {
    org.apache.commons.dbcp2.BasicDataSource dataSource = new org.apache.commons.dbcp2.BasicDataSource();
    // the following properties should be injected from application.properties
    dataSource.setDriverClassName("");
    dataSource.setUrl("url");
    dataSource.setUsername("");
    dataSource.setPassword("");
    return dataSource;
}