Java Code Examples for org.springframework.jdbc.datasource.init.DataSourceInitializer

The following examples show how to use org.springframework.jdbc.datasource.init.DataSourceInitializer. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Test
public void createAndDestroy() throws Exception {
	ClassPathXmlApplicationContext context = context("jdbc-destroy-config.xml");
	try {
		DataSource dataSource = context.getBean(DataSource.class);
		JdbcTemplate template = new JdbcTemplate(dataSource);
		assertNumRowsInTestTable(template, 1);
		context.getBean(DataSourceInitializer.class).destroy();
		// Table has been dropped
		assertThatExceptionOfType(BadSqlGrammarException.class).isThrownBy(() ->
				assertNumRowsInTestTable(template, 1));
	}
	finally {
		context.close();
	}
}
 
Example 2
Source Project: spring-cloud-task   Source File: TaskLauncherSinkTests.java    License: Apache License 2.0 6 votes vote down vote up
@Before
public void setup() {
	this.properties = new HashMap<>();
	this.properties.put("spring.datasource.url", DATASOURCE_URL);
	this.properties.put("spring.datasource.username", DATASOURCE_USER_NAME);
	this.properties.put("spring.datasource.password", DATASOURCE_USER_PASSWORD);
	this.properties.put("spring.datasource.driverClassName",
			DATASOURCE_DRIVER_CLASS_NAME);
	this.properties.put("spring.application.name", TASK_NAME);

	JdbcTemplate template = new JdbcTemplate(this.dataSource);
	template.execute("DROP ALL OBJECTS");

	DataSourceInitializer initializer = new DataSourceInitializer();

	initializer.setDataSource(this.dataSource);
	ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
	databasePopulator.addScript(
			new ClassPathResource("/org/springframework/cloud/task/schema-h2.sql"));
	initializer.setDatabasePopulator(databasePopulator);

	initializer.afterPropertiesSet();
}
 
Example 3
Source Project: Intro-to-Spring-Hadoop   Source File: Application.java    License: Apache License 2.0 6 votes vote down vote up
@Bean
DataSourceInitializer hiveInitializer(final DataSource dataSource) {
	final String ddl = "create external table if not exists tweetdata (value STRING) LOCATION '" + input + "'";
	final DataSourceInitializer dsi = new DataSourceInitializer();
    dsi.setDataSource(dataSource);
	dsi.setDatabasePopulator(new DatabasePopulator() {
		@Override
		public void populate(Connection conn) throws SQLException,
				ScriptException {
			Statement st = conn.createStatement();
			st.execute(ddl);
			st.close();
		}
	});
	return dsi;
}
 
Example 4
@Override
protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
	BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(DataSourceInitializer.class);
	builder.addPropertyReference("dataSource", element.getAttribute("data-source"));
	builder.addPropertyValue("enabled", element.getAttribute("enabled"));
	DatabasePopulatorConfigUtils.setDatabasePopulator(element, builder);
	builder.getRawBeanDefinition().setSource(parserContext.extractSource(element));
	return builder.getBeanDefinition();
}
 
Example 5
@Override
protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
	BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(DataSourceInitializer.class);
	builder.addPropertyReference("dataSource", element.getAttribute("data-source"));
	builder.addPropertyValue("enabled", element.getAttribute("enabled"));
	DatabasePopulatorConfigUtils.setDatabasePopulator(element, builder);
	builder.getRawBeanDefinition().setSource(parserContext.extractSource(element));
	return builder.getBeanDefinition();
}
 
Example 6
@Test
public void createAndDestroy() throws Exception {
	ClassPathXmlApplicationContext context = context("jdbc-destroy-config.xml");
	try {
		DataSource dataSource = context.getBean(DataSource.class);
		JdbcTemplate template = new JdbcTemplate(dataSource);
		assertNumRowsInTestTable(template, 1);
		context.getBean(DataSourceInitializer.class).destroy();
		expected.expect(BadSqlGrammarException.class); // Table has been dropped
		assertNumRowsInTestTable(template, 1);
	}
	finally {
		context.close();
	}
}
 
Example 7
Source Project: Spring   Source File: TestDataConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public DataSourceInitializer dataSourceInitializer(final DataSource dataSource) {
    final DataSourceInitializer initializer = new DataSourceInitializer();
    initializer.setDataSource(dataSource);
    initializer.setDatabasePopulator(databasePopulator());
    return initializer;
}
 
Example 8
Source Project: oauth2lab   Source File: OAuth2AuthorizationServerConfig.java    License: MIT License 5 votes vote down vote up
@Bean
public DataSourceInitializer dataSourceInitializer(final DataSource dataSource) {
    final DataSourceInitializer initializer = new DataSourceInitializer();
    initializer.setDataSource(dataSource);
    initializer.setDatabasePopulator(databasePopulator());
    return initializer;
}
 
Example 9
Source Project: activiti6-boot2   Source File: DatabaseInititializer.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public DataSourceInitializer dataSourceInitializer() {
  DataSourceInitializer initializer = new DataSourceInitializer();
  initializer.setDataSource(dataSource);
  initializer.setDatabasePopulator(databasePopulator());
  return initializer;
}
 
Example 10
@Bean
public DataSourceInitializer dataSourceInitializer(final DataSource dataSource) {
    final DataSourceInitializer initializer = new DataSourceInitializer();
    initializer.setDataSource(dataSource);
    initializer.setDatabasePopulator(databasePopulator());
    return initializer;
}
 
Example 11
@Bean
public DataSourceInitializer dataSourceInitializer(final DataSource dataSource) {
    final DataSourceInitializer initializer = new DataSourceInitializer();
    initializer.setDataSource(dataSource);
    initializer.setDatabasePopulator(databasePopulator());
    return initializer;
}
 
Example 12
@Override
protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
	BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(DataSourceInitializer.class);
	builder.addPropertyReference("dataSource", element.getAttribute("data-source"));
	builder.addPropertyValue("enabled", element.getAttribute("enabled"));
	DatabasePopulatorConfigUtils.setDatabasePopulator(element, builder);
	builder.getRawBeanDefinition().setSource(parserContext.extractSource(element));
	return builder.getBeanDefinition();
}
 
Example 13
@Override
protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
	BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(DataSourceInitializer.class);
	builder.addPropertyReference("dataSource", element.getAttribute("data-source"));
	builder.addPropertyValue("enabled", element.getAttribute("enabled"));
	DatabasePopulatorConfigUtils.setDatabasePopulator(element, builder);
	builder.getRawBeanDefinition().setSource(parserContext.extractSource(element));
	return builder.getBeanDefinition();
}
 
Example 14
@Test
public void createAndDestroy() throws Exception {
	ClassPathXmlApplicationContext context = context("jdbc-destroy-config.xml");
	try {
		DataSource dataSource = context.getBean(DataSource.class);
		JdbcTemplate template = new JdbcTemplate(dataSource);
		assertNumRowsInTestTable(template, 1);
		context.getBean(DataSourceInitializer.class).destroy();
		expected.expect(BadSqlGrammarException.class); // Table has been dropped
		assertNumRowsInTestTable(template, 1);
	}
	finally {
		context.close();
	}
}
 
Example 15
@Bean
public DataSourceInitializer dataSourceInitializer(final DataSource dataSource) {
    final DataSourceInitializer initializer = new DataSourceInitializer();
    initializer.setDataSource(dataSource);
    initializer.setDatabasePopulator(databasePopulator());
    return initializer;
}
 
Example 16
Source Project: spring-content   Source File: ElasticsearchConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
DataSourceInitializer datasourceInitializer(DataSource dataSource) {
    ResourceDatabasePopulator databasePopulator =
            new ResourceDatabasePopulator();

    databasePopulator.addScript(dropReopsitoryTables);
    databasePopulator.addScript(dataReopsitorySchema);
    databasePopulator.setIgnoreFailedDrops(true);

    DataSourceInitializer initializer = new DataSourceInitializer();
    initializer.setDataSource(dataSource);
    initializer.setDatabasePopulator(databasePopulator);

    return initializer;
}
 
Example 17
@Bean
public DataSourceInitializer dataSourceInitializer(final DataSource dataSource) {
    final DataSourceInitializer initializer = new DataSourceInitializer();
    initializer.setDataSource(dataSource);
    initializer.setDatabasePopulator(databasePopulator());
    return initializer;
}
 
Example 18
@Bean
public DataSourceInitializer dataSourceInitializer(final DataSource dataSource) {
    final DataSourceInitializer initializer = new DataSourceInitializer();
    initializer.setDataSource(dataSource);
    initializer.setDatabasePopulator(databasePopulator());
    return initializer;
}
 
Example 19
Source Project: flowable-engine   Source File: DatabaseInititializer.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public DataSourceInitializer dataSourceInitializer() {
    DataSourceInitializer initializer = new DataSourceInitializer();
    initializer.setDataSource(dataSource);
    initializer.setDatabasePopulator(databasePopulator());
    return initializer;
}
 
Example 20
@ConditionalOnProperty("jdbc.initialize")
@Bean
public DataSourceInitializer nonBootDataSourceInitializer(DataSource dataSource, ResourceLoader resourceLoader) {
	DataSourceInitializer dataSourceInitializer = new DataSourceInitializer();
	dataSourceInitializer.setDataSource(dataSource);
	ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
	databasePopulator.setIgnoreFailedDrops(true);
	dataSourceInitializer.setDatabasePopulator(databasePopulator);
	if ("true".equals(properties.getInitialize())) {
		databasePopulator.addScript(new DefaultInitializationScriptResource(properties));
	} else {
		databasePopulator.addScript(resourceLoader.getResource(properties.getInitialize()));
	}
	return dataSourceInitializer;
}
 
Example 21
Source Project: spring-cloud-task   Source File: TaskStartTests.java    License: Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
	this.properties = new HashMap<>();
	this.properties.put("spring.datasource.url", DATASOURCE_URL);
	this.properties.put("spring.datasource.username", DATASOURCE_USER_NAME);
	this.properties.put("spring.datasource.password", DATASOURCE_USER_PASSWORD);
	this.properties.put("spring.datasource.driverClassName",
			DATASOURCE_DRIVER_CLASS_NAME);
	this.properties.put("spring.application.name", TASK_NAME);
	this.properties.put("spring.cloud.task.initialize-enabled", "false");

	JdbcTemplate template = new JdbcTemplate(this.dataSource);
	template.execute("DROP TABLE IF EXISTS TASK_TASK_BATCH");
	template.execute("DROP TABLE IF EXISTS TASK_SEQ");
	template.execute("DROP TABLE IF EXISTS TASK_EXECUTION_PARAMS");
	template.execute("DROP TABLE IF EXISTS TASK_EXECUTION");
	template.execute("DROP TABLE IF EXISTS TASK_LOCK");
	template.execute("DROP TABLE IF EXISTS BATCH_STEP_EXECUTION_SEQ");
	template.execute("DROP TABLE IF EXISTS BATCH_STEP_EXECUTION_CONTEXT");
	template.execute("DROP TABLE IF EXISTS BATCH_STEP_EXECUTION");
	template.execute("DROP TABLE IF EXISTS BATCH_JOB_SEQ");
	template.execute("DROP TABLE IF EXISTS BATCH_JOB_EXECUTION_SEQ");
	template.execute("DROP TABLE IF EXISTS BATCH_JOB_EXECUTION_PARAMS");
	template.execute("DROP TABLE IF EXISTS BATCH_JOB_EXECUTION_CONTEXT");
	template.execute("DROP TABLE IF EXISTS BATCH_JOB_EXECUTION");
	template.execute("DROP TABLE IF EXISTS BATCH_JOB_INSTANCE");
	template.execute("DROP SEQUENCE IF EXISTS TASK_SEQ");

	DataSourceInitializer initializer = new DataSourceInitializer();

	initializer.setDataSource(this.dataSource);
	ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
	databasePopulator.addScript(
			new ClassPathResource("/org/springframework/cloud/task/schema-h2.sql"));
	initializer.setDatabasePopulator(databasePopulator);
	initializer.afterPropertiesSet();
}
 
Example 22
@Bean
DataSourceInitializer dataSourceInitializer(DataSource dataSource, DatabasePopulator databasePopulator) {
	DataSourceInitializer initializer = new DataSourceInitializer();
	initializer.setDataSource(dataSource);
	initializer.setDatabasePopulator(databasePopulator);
	return initializer;
}
 
Example 23
Source Project: syncope   Source File: MasterDomain.java    License: Apache License 2.0 5 votes vote down vote up
@Bean(name = "MasterDataSourceInitializer")
@ConditionalOnMissingBean(name = "MasterDataSourceInitializer")
public DataSourceInitializer masterDataSourceInitializer() {
    DataSourceInitializer dataSourceInitializer = new DataSourceInitializer();
    dataSourceInitializer.setDataSource((DataSource) Objects.requireNonNull(masterDataSource().getObject()));
    dataSourceInitializer.setEnabled(true);
    dataSourceInitializer.setDatabasePopulator(masterResourceDatabasePopulator());
    return dataSourceInitializer;
}
 
Example 24
@Override
protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
	BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(DataSourceInitializer.class);
	builder.addPropertyReference("dataSource", element.getAttribute("data-source"));
	builder.addPropertyValue("enabled", element.getAttribute("enabled"));
	DatabasePopulatorConfigUtils.setDatabasePopulator(element, builder);
	builder.getRawBeanDefinition().setSource(parserContext.extractSource(element));
	return builder.getBeanDefinition();
}
 
Example 25
@Test
public void createAndDestroy() throws Exception {
	ClassPathXmlApplicationContext context = context("jdbc-destroy-config.xml");
	try {
		DataSource dataSource = context.getBean(DataSource.class);
		JdbcTemplate template = new JdbcTemplate(dataSource);
		assertNumRowsInTestTable(template, 1);
		context.getBean(DataSourceInitializer.class).destroy();
		expected.expect(BadSqlGrammarException.class); // Table has been dropped
		assertNumRowsInTestTable(template, 1);
	}
	finally {
		context.close();
	}
}
 
Example 26
Source Project: tutorials   Source File: SpringConfig.java    License: MIT License 5 votes vote down vote up
@Bean
public DataSourceInitializer dataSourceInitializer(DataSource dataSource) throws MalformedURLException {
    ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();

    databasePopulator.addScript(dropReopsitoryTables);
    databasePopulator.addScript(dataReopsitorySchema);
    databasePopulator.setIgnoreFailedDrops(true);

    DataSourceInitializer initializer = new DataSourceInitializer();
    initializer.setDataSource(dataSource);
    initializer.setDatabasePopulator(databasePopulator);

    return initializer;
}
 
Example 27
Source Project: syncope   Source File: DomainConfFactory.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void register(final Domain domain) {
    HikariConfig hikariConfig = new HikariConfig();
    hikariConfig.setDriverClassName(domain.getJdbcDriver());
    hikariConfig.setJdbcUrl(domain.getJdbcURL());
    hikariConfig.setUsername(domain.getDbUsername());
    hikariConfig.setPassword(domain.getDbPassword());
    hikariConfig.setSchema(domain.getDbSchema());
    hikariConfig.setTransactionIsolation(domain.getTransactionIsolation().name());
    hikariConfig.setMaximumPoolSize(domain.getPoolMaxActive());
    hikariConfig.setMinimumIdle(domain.getPoolMinIdle());

    // domainDataSource
    registerBeanDefinition(
            domain.getKey() + "DataSource",
            BeanDefinitionBuilder.rootBeanDefinition(JndiObjectFactoryBean.class).
                    addPropertyValue("jndiName", "java:comp/env/jdbc/syncope" + domain.getKey() + "DataSource").
                    addPropertyValue("defaultObject", new HikariDataSource(hikariConfig)).
                    getBeanDefinition());
    DataSource initedDataSource = ApplicationContextProvider.getBeanFactory().
            getBean(domain.getKey() + "DataSource", DataSource.class);

    // domainResourceDatabasePopulator
    ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
    databasePopulator.setContinueOnError(true);
    databasePopulator.setIgnoreFailedDrops(true);
    databasePopulator.setSqlScriptEncoding(StandardCharsets.UTF_8.name());
    databasePopulator.addScript(new ClassPathResource("/audit/" + domain.getAuditSql()));
    registerSingleton(domain.getKey().toLowerCase() + "ResourceDatabasePopulator", databasePopulator);

    // domainDataSourceInitializer
    DataSourceInitializer dataSourceInitializer = new DataSourceInitializer();
    dataSourceInitializer.setDataSource(initedDataSource);
    dataSourceInitializer.setEnabled(true);
    dataSourceInitializer.setDatabasePopulator(databasePopulator);
    registerSingleton(domain.getKey().toLowerCase() + "DataSourceInitializer", dataSourceInitializer);

    // domainEntityManagerFactory
    OpenJpaVendorAdapter vendorAdapter = new OpenJpaVendorAdapter();
    vendorAdapter.setShowSql(false);
    vendorAdapter.setGenerateDdl(true);
    vendorAdapter.setDatabasePlatform(domain.getDatabasePlatform());

    BeanDefinitionBuilder emf = BeanDefinitionBuilder.rootBeanDefinition(DomainEntityManagerFactoryBean.class).
            addPropertyValue("mappingResources", domain.getOrm()).
            addPropertyValue("persistenceUnitName", domain.getKey()).
            addPropertyReference("dataSource", domain.getKey() + "DataSource").
            addPropertyValue("jpaVendorAdapter", vendorAdapter).
            addPropertyReference("commonEntityManagerFactoryConf", "commonEMFConf");
    if (env.containsProperty("openjpaMetaDataFactory")) {
        emf.addPropertyValue("jpaPropertyMap", Map.of(
                "openjpa.MetaDataFactory",
                Objects.requireNonNull(env.getProperty("openjpaMetaDataFactory")).
                        replace("##orm##", domain.getOrm())));
    }
    registerBeanDefinition(domain.getKey() + "EntityManagerFactory", emf.getBeanDefinition());
    ApplicationContextProvider.getBeanFactory().getBean(domain.getKey() + "EntityManagerFactory");

    // domainTransactionManager
    AbstractBeanDefinition domainTransactionManager =
            BeanDefinitionBuilder.rootBeanDefinition(JpaTransactionManager.class).
                    addPropertyReference("entityManagerFactory", domain.getKey() + "EntityManagerFactory").
                    getBeanDefinition();
    domainTransactionManager.addQualifier(new AutowireCandidateQualifier(Qualifier.class, domain.getKey()));
    registerBeanDefinition(domain.getKey() + "TransactionManager", domainTransactionManager);

    // domainContentXML
    registerBeanDefinition(domain.getKey() + "ContentXML",
            BeanDefinitionBuilder.rootBeanDefinition(ByteArrayInputStream.class).
                    addConstructorArgValue(domain.getContent().getBytes()).
                    getBeanDefinition());

    // domainKeymasterConfParamsJSON
    registerBeanDefinition(domain.getKey() + "KeymasterConfParamsJSON",
            BeanDefinitionBuilder.rootBeanDefinition(ByteArrayInputStream.class).
                    addConstructorArgValue(domain.getKeymasterConfParams().getBytes()).
                    getBeanDefinition());
}