Java Code Examples for org.springframework.jdbc.datasource.init.DataSourceInitializer#setDatabasePopulator()

The following examples show how to use org.springframework.jdbc.datasource.init.DataSourceInitializer#setDatabasePopulator() . 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
Source File: TaskLauncherSinkTests.java    From spring-cloud-task with Apache License 2.0 7 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 2
Source File: OAuth2AuthorizationServerConfig.java    From oauth2lab with MIT License 6 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 3
Source File: Application.java    From Intro-to-Spring-Hadoop with 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
Source File: ElasticsearchConfig.java    From spring-content with Apache License 2.0 6 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 5
Source File: SpringConfig.java    From tutorials with 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 6
Source File: TestDataConfig.java    From Spring with 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 7
Source File: MasterDomain.java    From syncope with 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 8
Source File: AbstractContainerJdbcIndexedSessionRepositoryITests.java    From spring-session with Apache License 2.0 5 votes vote down vote up
@Bean
DataSourceInitializer dataSourceInitializer(DataSource dataSource, DatabasePopulator databasePopulator) {
	DataSourceInitializer initializer = new DataSourceInitializer();
	initializer.setDataSource(dataSource);
	initializer.setDatabasePopulator(databasePopulator);
	return initializer;
}
 
Example 9
Source File: TaskStartTests.java    From spring-cloud-task with 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 10
Source File: JdbcSinkConfiguration.java    From spring-cloud-stream-app-starters with Apache License 2.0 5 votes vote down vote up
@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 11
Source File: DatabaseInititializer.java    From flowable-engine with 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 12
Source File: OAuth2AuthorizationServerConfig.java    From spring-security-oauth with 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 13
Source File: OAuth2AuthorizationServerConfigInMemory.java    From spring-security-oauth with 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 14
Source File: OAuth2AuthorizationServerConfig.java    From spring-boot with 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 15
Source File: AppConfig.java    From Building-Web-Apps-with-Spring-5-and-Angular with 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 16
Source File: AppConfig.java    From Building-Web-Apps-with-Spring-5-and-Angular with 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 17
Source File: DatabaseInititializer.java    From activiti6-boot2 with 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 18
Source File: DomainConfFactory.java    From syncope with 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());
}