Package org.springframework.jdbc.datasource.init

Examples of org.springframework.jdbc.datasource.init.ResourceDatabasePopulator


  }

  @Test
  public void testFactoryBeanLifecycle() throws Exception {
    EmbeddedDatabaseFactoryBean bean = new EmbeddedDatabaseFactoryBean();
    ResourceDatabasePopulator populator = new ResourceDatabasePopulator(resource("db-schema.sql"),
      resource("db-test-data.sql"));
    bean.setDatabasePopulator(populator);
    bean.afterPropertiesSet();
    DataSource ds = bean.getObject();
    JdbcTemplate template = new JdbcTemplate(ds);
View Full Code Here


   * Create a new embedded database builder with the given {@link ResourceLoader}.
   * @param resourceLoader the {@code ResourceLoader} to delegate to
   */
  public EmbeddedDatabaseBuilder(ResourceLoader resourceLoader) {
    this.databaseFactory = new EmbeddedDatabaseFactory();
    this.databasePopulator = new ResourceDatabasePopulator();
    this.databaseFactory.setDatabasePopulator(this.databasePopulator);
    this.resourceLoader = resourceLoader;
  }
View Full Code Here

   * @see ResourceDatabasePopulator
   * @see #setSqlScriptEncoding
   */
  protected void executeSqlScript(String sqlResourcePath, boolean continueOnError) throws DataAccessException {
    Resource resource = this.applicationContext.getResource(sqlResourcePath);
    new ResourceDatabasePopulator(continueOnError, false, this.sqlScriptEncoding, resource).execute(jdbcTemplate.getDataSource());
  }
View Full Code Here

   * or {@link org.springframework.jdbc.datasource.init.ResourceDatabasePopulator}.
   */
  @Deprecated
  public static void executeSqlScript(JdbcTemplate jdbcTemplate, EncodedResource resource, boolean continueOnError)
      throws DataAccessException {
    new ResourceDatabasePopulator(continueOnError, false, resource.getEncoding(), resource.getResource()).execute(jdbcTemplate.getDataSource());
  }
View Full Code Here

    if (logger.isDebugEnabled()) {
      logger.debug(String.format("Processing %s for execution phase [%s] and test context %s.", mergedSqlConfig,
        executionPhase, testContext));
    }

    final ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
    populator.setSqlScriptEncoding(mergedSqlConfig.getEncoding());
    populator.setSeparator(mergedSqlConfig.getSeparator());
    populator.setCommentPrefix(mergedSqlConfig.getCommentPrefix());
    populator.setBlockCommentStartDelimiter(mergedSqlConfig.getBlockCommentStartDelimiter());
    populator.setBlockCommentEndDelimiter(mergedSqlConfig.getBlockCommentEndDelimiter());
    populator.setContinueOnError(mergedSqlConfig.getErrorMode() == ErrorMode.CONTINUE_ON_ERROR);
    populator.setIgnoreFailedDrops(mergedSqlConfig.getErrorMode() == ErrorMode.IGNORE_FAILED_DROPS);

    String[] scripts = getScripts(sql, testContext, classLevel);
    scripts = TestContextResourceUtils.convertToClasspathResourcePaths(testContext.getTestClass(), scripts);
    populator.setScripts(TestContextResourceUtils.convertToResources(testContext.getApplicationContext(), scripts));
    if (logger.isDebugEnabled()) {
      logger.debug("Executing SQL scripts: " + ObjectUtils.nullSafeToString(scripts));
    }

    String dsName = mergedSqlConfig.getDataSource();
    String tmName = mergedSqlConfig.getTransactionManager();
    DataSource dataSource = TestContextTransactionUtils.retrieveDataSource(testContext, dsName);
    final PlatformTransactionManager transactionManager = TestContextTransactionUtils.retrieveTransactionManager(
      testContext, tmName);
    final boolean newTxRequired = mergedSqlConfig.getTransactionMode() == TransactionMode.ISOLATED;

    if (transactionManager == null) {
      if (newTxRequired) {
        throw new IllegalStateException(String.format("Failed to execute SQL scripts for test context %s: "
            + "cannot execute SQL scripts using Transaction Mode "
            + "[%s] without a PlatformTransactionManager.", testContext, TransactionMode.ISOLATED));
      }

      if (dataSource == null) {
        throw new IllegalStateException(String.format("Failed to execute SQL scripts for test context %s: "
            + "supply at least a DataSource or PlatformTransactionManager.", testContext));
      }

      // Execute scripts directly against the DataSource
      populator.execute(dataSource);
    }
    else {
      DataSource dataSourceFromTxMgr = getDataSourceFromTransactionManager(transactionManager);

      // Ensure user configured an appropriate DataSource/TransactionManager pair.
      if ((dataSource != null) && (dataSourceFromTxMgr != null) && !dataSource.equals(dataSourceFromTxMgr)) {
        throw new IllegalStateException(String.format("Failed to execute SQL scripts for test context %s: "
            + "the configured DataSource [%s] (named '%s') is not the one associated "
            + "with transaction manager [%s] (named '%s').", testContext, dataSource.getClass().getName(),
          dsName, transactionManager.getClass().getName(), tmName));
      }

      if (dataSource == null) {
        dataSource = dataSourceFromTxMgr;
        if (dataSource == null) {
          throw new IllegalStateException(String.format("Failed to execute SQL scripts for test context %s: "
              + "could not obtain DataSource from transaction manager [%s] (named '%s').", testContext,
            transactionManager.getClass().getName(), tmName));
        }
      }

      final DataSource finalDataSource = dataSource;
      int propagation = newTxRequired ? TransactionDefinition.PROPAGATION_REQUIRES_NEW
          : TransactionDefinition.PROPAGATION_REQUIRED;

      TransactionAttribute transactionAttribute = TestContextTransactionUtils.createDelegatingTransactionAttribute(
        testContext, new DefaultTransactionAttribute(propagation));

      new TransactionTemplate(transactionManager, transactionAttribute).execute(new TransactionCallbackWithoutResult() {

        @Override
        public void doInTransactionWithoutResult(TransactionStatus status) {
          populator.execute(finalDataSource);
        }
      });
    }
  }
View Full Code Here

   * Create a new embedded database builder with the given ResourceLoader.
   * @param resourceLoader the ResourceLoader to delegate to
   */
  public EmbeddedDatabaseBuilder(ResourceLoader resourceLoader) {
    this.databaseFactory = new EmbeddedDatabaseFactory();
    this.databasePopulator = new ResourceDatabasePopulator();
    this.databaseFactory.setDatabasePopulator(this.databasePopulator);
    this.resourceLoader = resourceLoader;
  }
View Full Code Here

  @Value("${spring.datasource.schema.script}")
  private Resource ddlScriptResource;

  @Bean
  public DatabasePopulator databasePopulator(final DataSource dataSource) {
    final ResourceDatabasePopulator populator = new ResourceDatabasePopulator();

    populator.setContinueOnError(true);
    populator.setIgnoreFailedDrops(true);
    if (ddlScriptResource != null) {
      populator.addScript(ddlScriptResource);
    }
    if (batchScriptResource != null) {
      populator.addScript(batchScriptResource);
    }
    try {
      populator.populate(dataSource.getConnection());
    } catch (final SQLException ignored) {
    }

    return populator;
  }
View Full Code Here

   * @see ResourceDatabasePopulator
   * @see #setSqlScriptEncoding
   */
  protected void executeSqlScript(String sqlResourcePath, boolean continueOnError) throws DataAccessException {
    Resource resource = this.applicationContext.getResource(sqlResourcePath);
    new ResourceDatabasePopulator(continueOnError, false, this.sqlScriptEncoding, resource).execute(jdbcTemplate.getDataSource());
  }
View Full Code Here

    dsInitializer.setDataSource(dataSource);
    String scripts = env.getProperty("scripts", "");
    logger.debug("Init-db :"+initDB+", scripts: "+scripts);
    if(Boolean.parseBoolean(initDB) && StringUtils.hasLength(scripts))
    {
      ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
      String[] files = scripts.split(",");
      for (String file : files) {
        databasePopulator.addScript(new ClassPathResource(file));
      }
      dsInitializer.setDatabasePopulator(databasePopulator);
    }
   
    return dsInitializer;
View Full Code Here

  @Autowired
  private ResourceLoader resourceLoader;

  @PostConstruct
  protected void initialize() {
    ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
    populator.addScript(resourceLoader.getResource(ClassUtils.addResourcePathToPackagePath(Step.class, "schema-hsqldb.sql")));
    populator.setContinueOnError(true);
    DatabasePopulatorUtils.execute(populator, dataSource());
  }
View Full Code Here

TOP

Related Classes of org.springframework.jdbc.datasource.init.ResourceDatabasePopulator

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.