org.springframework.jdbc.datasource.init.DataSourceInitializer Java Examples
The following examples show how to use
org.springframework.jdbc.datasource.init.DataSourceInitializer.
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 |
@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: JdbcNamespaceIntegrationTests.java From spring-analysis-note with MIT License | 6 votes |
@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 #3
Source File: Application.java From Intro-to-Spring-Hadoop with Apache License 2.0 | 6 votes |
@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 |
@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: OAuth2AuthorizationServerConfig.java From oauth2lab with MIT License | 6 votes |
@Bean public DataSourceInitializer dataSourceInitializer(final DataSource dataSource) { final DataSourceInitializer initializer = new DataSourceInitializer(); initializer.setDataSource(dataSource); initializer.setDatabasePopulator(databasePopulator()); return initializer; }
Example #6
Source File: DatabaseInititializer.java From flowable-engine with Apache License 2.0 | 5 votes |
@Bean public DataSourceInitializer dataSourceInitializer() { DataSourceInitializer initializer = new DataSourceInitializer(); initializer.setDataSource(dataSource); initializer.setDatabasePopulator(databasePopulator()); return initializer; }
Example #7
Source File: SpringConfig.java From tutorials with MIT License | 5 votes |
@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 #8
Source File: JdbcNamespaceIntegrationTests.java From effectivejava with Apache License 2.0 | 5 votes |
@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 #9
Source File: InitializeDatabaseBeanDefinitionParser.java From effectivejava with Apache License 2.0 | 5 votes |
@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 #10
Source File: MasterDomain.java From syncope with Apache License 2.0 | 5 votes |
@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 #11
Source File: InitializeDatabaseBeanDefinitionParser.java From spring-analysis-note with MIT License | 5 votes |
@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 #12
Source File: AbstractContainerJdbcIndexedSessionRepositoryITests.java From spring-session with Apache License 2.0 | 5 votes |
@Bean DataSourceInitializer dataSourceInitializer(DataSource dataSource, DatabasePopulator databasePopulator) { DataSourceInitializer initializer = new DataSourceInitializer(); initializer.setDataSource(dataSource); initializer.setDatabasePopulator(databasePopulator); return initializer; }
Example #13
Source File: InitializeDatabaseBeanDefinitionParser.java From java-technology-stack with MIT License | 5 votes |
@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
Source File: TaskStartTests.java From spring-cloud-task with Apache License 2.0 | 5 votes |
@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 #15
Source File: JdbcSinkConfiguration.java From spring-cloud-stream-app-starters with Apache License 2.0 | 5 votes |
@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 #16
Source File: TestDataConfig.java From Spring with Apache License 2.0 | 5 votes |
@Bean public DataSourceInitializer dataSourceInitializer(final DataSource dataSource) { final DataSourceInitializer initializer = new DataSourceInitializer(); initializer.setDataSource(dataSource); initializer.setDatabasePopulator(databasePopulator()); return initializer; }
Example #17
Source File: OAuth2AuthorizationServerConfig.java From spring-security-oauth with MIT License | 5 votes |
@Bean public DataSourceInitializer dataSourceInitializer(final DataSource dataSource) { final DataSourceInitializer initializer = new DataSourceInitializer(); initializer.setDataSource(dataSource); initializer.setDatabasePopulator(databasePopulator()); return initializer; }
Example #18
Source File: OAuth2AuthorizationServerConfigInMemory.java From spring-security-oauth with MIT License | 5 votes |
@Bean public DataSourceInitializer dataSourceInitializer(final DataSource dataSource) { final DataSourceInitializer initializer = new DataSourceInitializer(); initializer.setDataSource(dataSource); initializer.setDatabasePopulator(databasePopulator()); return initializer; }
Example #19
Source File: JdbcNamespaceIntegrationTests.java From java-technology-stack with MIT License | 5 votes |
@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 #20
Source File: OAuth2AuthorizationServerConfig.java From spring-boot with Apache License 2.0 | 5 votes |
@Bean public DataSourceInitializer dataSourceInitializer(final DataSource dataSource) { final DataSourceInitializer initializer = new DataSourceInitializer(); initializer.setDataSource(dataSource); initializer.setDatabasePopulator(databasePopulator()); return initializer; }
Example #21
Source File: JdbcNamespaceIntegrationTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@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 #22
Source File: InitializeDatabaseBeanDefinitionParser.java From spring4-understanding with Apache License 2.0 | 5 votes |
@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 #23
Source File: InitializeDatabaseBeanDefinitionParser.java From lams with GNU General Public License v2.0 | 5 votes |
@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 #24
Source File: AppConfig.java From Building-Web-Apps-with-Spring-5-and-Angular with MIT License | 5 votes |
@Bean public DataSourceInitializer dataSourceInitializer(final DataSource dataSource) { final DataSourceInitializer initializer = new DataSourceInitializer(); initializer.setDataSource(dataSource); initializer.setDatabasePopulator(databasePopulator()); return initializer; }
Example #25
Source File: AppConfig.java From Building-Web-Apps-with-Spring-5-and-Angular with MIT License | 5 votes |
@Bean public DataSourceInitializer dataSourceInitializer(final DataSource dataSource) { final DataSourceInitializer initializer = new DataSourceInitializer(); initializer.setDataSource(dataSource); initializer.setDatabasePopulator(databasePopulator()); return initializer; }
Example #26
Source File: DatabaseInititializer.java From activiti6-boot2 with Apache License 2.0 | 5 votes |
@Bean public DataSourceInitializer dataSourceInitializer() { DataSourceInitializer initializer = new DataSourceInitializer(); initializer.setDataSource(dataSource); initializer.setDatabasePopulator(databasePopulator()); return initializer; }
Example #27
Source File: DomainConfFactory.java From syncope with Apache License 2.0 | 4 votes |
@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()); }