org.springframework.jdbc.datasource.SimpleDriverDataSource Java Examples

The following examples show how to use org.springframework.jdbc.datasource.SimpleDriverDataSource. 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: EmbeddedDatabaseFactory.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Hook to shutdown the embedded database. Subclasses may call this method
 * to force shutdown.
 * <p>After calling, {@link #getDataSource()} returns {@code null}.
 * <p>Does nothing if no embedded database has been initialized.
 */
protected void shutdownDatabase() {
	if (this.dataSource != null) {
		if (logger.isInfoEnabled()) {
			if (this.dataSource instanceof SimpleDriverDataSource) {
				logger.info(String.format("Shutting down embedded database: url='%s'",
					((SimpleDriverDataSource) this.dataSource).getUrl()));
			}
			else {
				logger.info(String.format("Shutting down embedded database '%s'", this.databaseName));
			}
		}
		if (this.databaseConfigurer != null) {
			this.databaseConfigurer.shutdown(this.dataSource, this.databaseName);
		}
		this.dataSource = null;
	}
}
 
Example #2
Source File: EmbeddedDatabaseFactory.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Hook to shutdown the embedded database. Subclasses may call this method
 * to force shutdown.
 * <p>After calling, {@link #getDataSource()} returns {@code null}.
 * <p>Does nothing if no embedded database has been initialized.
 */
protected void shutdownDatabase() {
	if (this.dataSource != null) {

		if (logger.isInfoEnabled()) {
			if (this.dataSource instanceof SimpleDriverDataSource) {
				logger.info(String.format("Shutting down embedded database: url='%s'",
					((SimpleDriverDataSource) this.dataSource).getUrl()));
			}
			else {
				logger.info(String.format("Shutting down embedded database '%s'", this.databaseName));
			}
		}

		this.databaseConfigurer.shutdown(this.dataSource, this.databaseName);
		this.dataSource = null;
	}
}
 
Example #3
Source File: EmbeddedDatabaseFactory.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Hook to shutdown the embedded database. Subclasses may call this method
 * to force shutdown.
 * <p>After calling, {@link #getDataSource()} returns {@code null}.
 * <p>Does nothing if no embedded database has been initialized.
 */
protected void shutdownDatabase() {
	if (this.dataSource != null) {

		if (logger.isInfoEnabled()) {
			if (this.dataSource instanceof SimpleDriverDataSource) {
				logger.info(String.format("Shutting down embedded database: url='%s'",
					((SimpleDriverDataSource) this.dataSource).getUrl()));
			}
			else {
				logger.info(String.format("Shutting down embedded database '%s'", this.databaseName));
			}
		}

		this.databaseConfigurer.shutdown(this.dataSource, this.databaseName);
		this.dataSource = null;
	}
}
 
Example #4
Source File: DatabaseCreator.java    From infobip-spring-data-querydsl with Apache License 2.0 6 votes vote down vote up
DatabaseCreator(String url, String username, String password) {
    String databaseUrlPattern = getDatabaseUrlPattern(url);
    Pattern jdbcBaseUrlWithDbNamePattern = Pattern.compile(databaseUrlPattern);
    Matcher matcher = jdbcBaseUrlWithDbNamePattern.matcher(url);

    if (!matcher.matches()) {
        throw new IllegalArgumentException(url + " does not match " + databaseUrlPattern);
    }

    String jdbcBaseUrl = matcher.group("jdbcBaseUrl");
    String databaseName = matcher.group("databaseName");
    databaseExistsQuery = String.format("SELECT count(*) FROM sys.databases WHERE name='%s'", databaseName);
    createDatabaseQuery = String.format("CREATE DATABASE %s", databaseName);
    this.template = new JdbcTemplate(
            new SimpleDriverDataSource(getDriver(jdbcBaseUrl), jdbcBaseUrl, username, password));
}
 
Example #5
Source File: DataMigrationTool.java    From gerbil with GNU Affero General Public License v3.0 6 votes vote down vote up
public static void main(String[] args) {
    ExperimentDAO source = null;
    ExperimentDAO target = null;
    try {
        source = new ExperimentDAOImpl(
                new SimpleDriverDataSource(new org.hsqldb.jdbc.JDBCDriver(), "jdbc:hsqldb:file:" + SOURCE_DB_PATH));
        source.initialize();
        target = new ExperimentDAOImpl(
                new SimpleDriverDataSource(new org.hsqldb.jdbc.JDBCDriver(), "jdbc:hsqldb:file:" + TARGET_DB_PATH));
        target.initialize();
        performMigration(source, target);
    } finally {
        IOUtils.closeQuietly(source);
        IOUtils.closeQuietly(target);
    }
}
 
Example #6
Source File: SQLFactory.java    From FastSQL with Apache License 2.0 6 votes vote down vote up
public static SQLFactory createUseSimpleDateSource(Driver driver, String url, String username, String password) {
    SQLFactory sqlFactory = new SQLFactory();
    if (url.contains("jdbc:mysql:")) {
        sqlFactory.setDataSourceType(DataSourceType.MY_SQL);
    }
    if (url.contains("jdbc:oracle:")) {
        sqlFactory.setDataSourceType(DataSourceType.ORACLE);
    }

    if (url.contains("jdbc:postgresql:")) {
        sqlFactory.setDataSourceType(DataSourceType.POSTGRESQL);
    }

    SimpleDriverDataSource dataSource = new SimpleDriverDataSource(driver, url, username, password);
    sqlFactory.setDataSource(dataSource);
    return sqlFactory;
}
 
Example #7
Source File: EmbeddedDatabaseFactory.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Hook to shutdown the embedded database. Subclasses may call this method
 * to force shutdown.
 * <p>After calling, {@link #getDataSource()} returns {@code null}.
 * <p>Does nothing if no embedded database has been initialized.
 */
protected void shutdownDatabase() {
	if (this.dataSource != null) {
		if (logger.isInfoEnabled()) {
			if (this.dataSource instanceof SimpleDriverDataSource) {
				logger.info(String.format("Shutting down embedded database: url='%s'",
					((SimpleDriverDataSource) this.dataSource).getUrl()));
			}
			else {
				logger.info(String.format("Shutting down embedded database '%s'", this.databaseName));
			}
		}
		if (this.databaseConfigurer != null) {
			this.databaseConfigurer.shutdown(this.dataSource, this.databaseName);
		}
		this.dataSource = null;
	}
}
 
Example #8
Source File: DatabaseUtil.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
public static void createSchemaIfNeeded(String dbType, String dbAddress, String dbName, String dbUser, String dbPassword, String dbSchema)
        throws SQLException {
    if (!DEFAULT_SCHEMA_NAME.equals(dbSchema)) {
        SimpleDriverDataSource ds = new SimpleDriverDataSource();
        ds.setDriverClass(Driver.class);
        ds.setUrl(String.format("jdbc:%s://%s/%s", dbType, dbAddress, dbName));
        try (Connection conn = ds.getConnection(dbUser, dbPassword); Statement statement = conn.createStatement()) {
            statement.execute("CREATE SCHEMA IF NOT EXISTS " + dbSchema);
        }
    }
}
 
Example #9
Source File: DatabaseConfig.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
private void createSchemaIfNeeded(String dbType, String dbAddress, String dbName, String dbUser, String dbPassword, String dbSchema)
        throws SQLException {
    if (!DEFAULT_SCHEMA_NAME.equals(dbSchema)) {
        SimpleDriverDataSource ds = new SimpleDriverDataSource();
        ds.setDriverClass(Driver.class);
        ds.setUrl(String.format("jdbc:%s://%s/%s", dbType, dbAddress, dbName));
        try (Connection conn = ds.getConnection(dbUser, dbPassword); Statement statement = conn.createStatement()) {
            statement.execute("CREATE SCHEMA IF NOT EXISTS " + dbSchema);
        }
    }
}
 
Example #10
Source File: UrlDecodingDataSource.java    From spring-cloud-connectors with Apache License 2.0 5 votes vote down vote up
private static DataSource newSimpleDriverDataSource(String jdbcUrl) {
	try {
		return new SimpleDriverDataSource(DriverManager.getDriver(jdbcUrl), jdbcUrl);
	} catch (SQLException e) {
		throw new RuntimeException("Unable to construct DataSource", e);
	}
}
 
Example #11
Source File: InitProcessEngineBySpringAnnotation.java    From activiti-in-action-codes with Apache License 2.0 5 votes vote down vote up
/**
 * 定义数据源
 * @return
 * @throws ClassNotFoundException
 */
@Bean
public DataSource dataSource() throws ClassNotFoundException {
    SimpleDriverDataSource simpleDriverDataSource = new SimpleDriverDataSource();
    simpleDriverDataSource.setDriverClass((Class<? extends Driver>) Class.forName("org.h2.Driver"));
    simpleDriverDataSource.setUrl("jdbc:h2:mem:aia-chapter7;DB_CLOSE_DELAY=1000");
    simpleDriverDataSource.setUsername("sa");
    simpleDriverDataSource.setPassword("");
    return simpleDriverDataSource;
}
 
Example #12
Source File: DataSourceXmlConfigTest.java    From spring-cloud-connectors with Apache License 2.0 5 votes vote down vote up
@Test
public void cloudDataSourceWithInvalidDataSource() throws Exception {
	ApplicationContext testContext = getTestApplicationContext("cloud-datasource-with-config.xml",
			createService("my-service"));

	DataSource ds = testContext.getBean("db-pool-invalid", getConnectorType());
	assertDataSourceType(ds, SimpleDriverDataSource.class);
}
 
Example #13
Source File: EngineConfiguration.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@Bean
public DataSource dataSource() {
    SimpleDriverDataSource ds = new SimpleDriverDataSource();
    ds.setDriverClass(jdbcDriver);

    // Connection settings
    ds.setUrl("jdbc:h2:mem:flowable;DB_CLOSE_DELAY=1000");
    ds.setUsername(jdbcUsername);
    ds.setPassword(jdbcPassword);

    return ds;
}
 
Example #14
Source File: EngineConfiguration.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@Bean
public DataSource dataSource() {
    SimpleDriverDataSource ds = new SimpleDriverDataSource();
    ds.setDriverClass(jdbcDriver);

    // Connection settings
    ds.setUrl("jdbc:h2:mem:flowable;DB_CLOSE_DELAY=1000");
    ds.setUsername(jdbcUsername);
    ds.setPassword(jdbcPassword);

    return ds;
}
 
Example #15
Source File: EmbeddedDatabaseFactory.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Hook to initialize the embedded database.
 * <p>If the {@code generateUniqueDatabaseName} flag has been set to {@code true},
 * the current value of the {@linkplain #setDatabaseName database name} will
 * be overridden with an auto-generated name.
 * <p>Subclasses may call this method to force initialization; however,
 * this method should only be invoked once.
 * <p>After calling this method, {@link #getDataSource()} returns the
 * {@link DataSource} providing connectivity to the database.
 */
protected void initDatabase() {
	if (this.generateUniqueDatabaseName) {
		setDatabaseName(UUID.randomUUID().toString());
	}

	// Create the embedded database first
	if (this.databaseConfigurer == null) {
		this.databaseConfigurer = EmbeddedDatabaseConfigurerFactory.getConfigurer(EmbeddedDatabaseType.HSQL);
	}
	this.databaseConfigurer.configureConnectionProperties(
			this.dataSourceFactory.getConnectionProperties(), this.databaseName);
	this.dataSource = this.dataSourceFactory.getDataSource();

	if (logger.isInfoEnabled()) {
		if (this.dataSource instanceof SimpleDriverDataSource) {
			SimpleDriverDataSource simpleDriverDataSource = (SimpleDriverDataSource) this.dataSource;
			logger.info(String.format("Starting embedded database: url='%s', username='%s'",
					simpleDriverDataSource.getUrl(), simpleDriverDataSource.getUsername()));
		}
		else {
			logger.info(String.format("Starting embedded database '%s'", this.databaseName));
		}
	}

	// Now populate the database
	if (this.databasePopulator != null) {
		try {
			DatabasePopulatorUtils.execute(this.databasePopulator, this.dataSource);
		}
		catch (RuntimeException ex) {
			// failed to populate, so leave it as not initialized
			shutdownDatabase();
			throw ex;
		}
	}
}
 
Example #16
Source File: DatabaseConfiguration.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@Bean
public DataSource dataSource() {
    SimpleDriverDataSource ds = new SimpleDriverDataSource();
    ds.setDriverClass(org.h2.Driver.class);

    // Connection settings
    ds.setUrl("jdbc:h2:mem:flowable;DB_CLOSE_DELAY=1000");
    ds.setUsername("sa");

    return ds;
}
 
Example #17
Source File: EngineConfiguration.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@Bean
public DataSource dataSource() {
    SimpleDriverDataSource ds = new SimpleDriverDataSource();
    ds.setDriverClass(jdbcDriver);

    // Connection settings
    ds.setUrl("jdbc:h2:mem:flowable;DB_CLOSE_DELAY=1000");
    ds.setUsername(jdbcUsername);
    ds.setPassword(jdbcPassword);

    return ds;
}
 
Example #18
Source File: EngineConfiguration.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
@Bean
public DataSource dataSource() {
    SimpleDriverDataSource ds = new SimpleDriverDataSource();
    ds.setDriverClass(jdbcDriver);

    // Connection settings
    ds.setUrl("jdbc:h2:mem:flowable;DB_CLOSE_DELAY=1000");
    ds.setUsername(jdbcUsername);
    ds.setPassword(jdbcPassword);

    return ds;
}
 
Example #19
Source File: TransactionalTestConfiguration.java    From tutorials with MIT License 5 votes vote down vote up
@Bean
public DataSource getDataSource() {
    SimpleDriverDataSource simpleDriverDataSource = new SimpleDriverDataSource();
    simpleDriverDataSource.setDriverClass(org.hsqldb.jdbcDriver.class);
    simpleDriverDataSource.setUrl("jdbc:hsqldb:mem:app-db");
    simpleDriverDataSource.setUsername("sa");
    simpleDriverDataSource.setPassword("");
    return simpleDriverDataSource;
}
 
Example #20
Source File: EmbeddedDatabaseFactory.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Hook to initialize the embedded database.
 * <p>If the {@code generateUniqueDatabaseName} flag has been set to {@code true},
 * the current value of the {@linkplain #setDatabaseName database name} will
 * be overridden with an auto-generated name.
 * <p>Subclasses may call this method to force initialization; however,
 * this method should only be invoked once.
 * <p>After calling this method, {@link #getDataSource()} returns the
 * {@link DataSource} providing connectivity to the database.
 */
protected void initDatabase() {
	if (this.generateUniqueDatabaseName) {
		setDatabaseName(UUID.randomUUID().toString());
	}

	// Create the embedded database first
	if (this.databaseConfigurer == null) {
		this.databaseConfigurer = EmbeddedDatabaseConfigurerFactory.getConfigurer(EmbeddedDatabaseType.HSQL);
	}
	this.databaseConfigurer.configureConnectionProperties(
			this.dataSourceFactory.getConnectionProperties(), this.databaseName);
	this.dataSource = this.dataSourceFactory.getDataSource();

	if (logger.isInfoEnabled()) {
		if (this.dataSource instanceof SimpleDriverDataSource) {
			SimpleDriverDataSource simpleDriverDataSource = (SimpleDriverDataSource) this.dataSource;
			logger.info(String.format("Starting embedded database: url='%s', username='%s'",
				simpleDriverDataSource.getUrl(), simpleDriverDataSource.getUsername()));
		}
		else {
			logger.info(String.format("Starting embedded database '%s'", this.databaseName));
		}
	}

	// Now populate the database
	if (this.databasePopulator != null) {
		try {
			DatabasePopulatorUtils.execute(this.databasePopulator, this.dataSource);
		}
		catch (RuntimeException ex) {
			// failed to populate, so leave it as not initialized
			shutdownDatabase();
			throw ex;
		}
	}
}
 
Example #21
Source File: DataSourceConfiguration.java    From event-sourcing-microservices-example with GNU General Public License v3.0 5 votes vote down vote up
@Bean
@ConfigurationProperties("spring.datasource")
@LiquibaseDataSource
public DataSource dataSource(DataSourceProperties properties) {
	return new SimpleDriverDataSource(new org.postgresql.Driver(), properties.getUrl(),
			properties.getDataUsername(), properties.getDataPassword());
}
 
Example #22
Source File: DataSourceConfiguration.java    From event-sourcing-microservices-example with GNU General Public License v3.0 5 votes vote down vote up
@Bean
@ConfigurationProperties("spring.datasource")
@LiquibaseDataSource
public DataSource dataSource(DataSourceProperties properties) {
	return new SimpleDriverDataSource(new org.postgresql.Driver(), properties.getUrl(),
			properties.getDataUsername(), properties.getDataPassword());
}
 
Example #23
Source File: TestJpaConfiguration.java    From jwala with Apache License 2.0 5 votes vote down vote up
@Bean
public DataSource getDataSource() {
    return new SimpleDriverDataSource(new Driver(),
            "jdbc:h2:mem:test-services;DB_CLOSE_DELAY=-1;LOCK_MODE=0",
            "sa",
            "");
}
 
Example #24
Source File: EmbeddedDatabaseFactory.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Hook to initialize the embedded database.
 * <p>If the {@code generateUniqueDatabaseName} flag has been set to {@code true},
 * the current value of the {@linkplain #setDatabaseName database name} will
 * be overridden with an auto-generated name.
 * <p>Subclasses may call this method to force initialization; however,
 * this method should only be invoked once.
 * <p>After calling this method, {@link #getDataSource()} returns the
 * {@link DataSource} providing connectivity to the database.
 */
protected void initDatabase() {
	if (this.generateUniqueDatabaseName) {
		setDatabaseName(UUID.randomUUID().toString());
	}

	// Create the embedded database first
	if (this.databaseConfigurer == null) {
		this.databaseConfigurer = EmbeddedDatabaseConfigurerFactory.getConfigurer(EmbeddedDatabaseType.HSQL);
	}
	this.databaseConfigurer.configureConnectionProperties(
			this.dataSourceFactory.getConnectionProperties(), this.databaseName);
	this.dataSource = this.dataSourceFactory.getDataSource();

	if (logger.isInfoEnabled()) {
		if (this.dataSource instanceof SimpleDriverDataSource) {
			SimpleDriverDataSource simpleDriverDataSource = (SimpleDriverDataSource) this.dataSource;
			logger.info(String.format("Starting embedded database: url='%s', username='%s'",
				simpleDriverDataSource.getUrl(), simpleDriverDataSource.getUsername()));
		}
		else {
			logger.info(String.format("Starting embedded database '%s'", this.databaseName));
		}
	}

	// Now populate the database
	if (this.databasePopulator != null) {
		try {
			DatabasePopulatorUtils.execute(this.databasePopulator, this.dataSource);
		}
		catch (RuntimeException ex) {
			// failed to populate, so leave it as not initialized
			shutdownDatabase();
			throw ex;
		}
	}
}
 
Example #25
Source File: PortalPersonDirUserPasswordDaoTest.java    From uPortal-start with Apache License 2.0 5 votes vote down vote up
@Override
protected void setUp() throws Exception {
    this.dataSource =
            new SimpleDriverDataSource(
                    new org.hsqldb.jdbcDriver(), "jdbc:hsqldb:mem:CasTest", "sa", "");

    this.jdbcTemplate = new JdbcTemplate(this.dataSource);
    this.jdbcTemplate.execute(
            "CREATE TABLE UP_PERSON_DIR (USER_NAME VARCHAR(1000), ENCRPTD_PSWD VARCHAR(1000))");

    this.userPasswordDao = new PortalPersonDirUserPasswordDao();
    this.userPasswordDao.setDataSource(this.dataSource);
}
 
Example #26
Source File: SQLFactoryTest.java    From FastSQL with Apache License 2.0 5 votes vote down vote up
@Test
    public void crateSQL() {
        DataSource dataSource = new SimpleDriverDataSource();

        SQLFactory sqlFactory = new SQLFactory();
        sqlFactory.setDataSource(dataSource);

        SQL sql = sqlFactory.createSQL();
//        Student student = sql.SELECT("*").FROM("student").WHERE("id=101").queryOne(Student.class);
    }
 
Example #27
Source File: SQLFactoryTest.java    From FastSQL with Apache License 2.0 5 votes vote down vote up
@Test
public void crateFactory() {
    DataSource dataSource = new SimpleDriverDataSource();

    SQLFactory sqlFactory = new SQLFactory();
    sqlFactory.setDataSource(dataSource);
}
 
Example #28
Source File: ActivitiEngineConfiguration.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
@Bean
public DataSource dataSource() {
  SimpleDriverDataSource ds = new SimpleDriverDataSource();
  ds.setDriverClass(org.h2.Driver.class);

  // Connection settings
  ds.setUrl("jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000");
  ds.setUsername("sa");

  return ds;
}
 
Example #29
Source File: DatabaseConfiguration.java    From activiti6-boot2 with Apache License 2.0 5 votes vote down vote up
@Bean
public DataSource dataSource() {
  SimpleDriverDataSource ds = new SimpleDriverDataSource();
  ds.setDriverClass(org.h2.Driver.class);

  // Connection settings
  ds.setUrl("jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000");
  ds.setUsername("sa");

  return ds;
}
 
Example #30
Source File: FootballEcosystem.java    From football-events with MIT License 5 votes vote down vote up
private void connectorCreated() {
    // create database and table
    postgres = new JdbcTemplate(new SimpleDriverDataSource(new Driver(),
        "jdbc:postgresql://postgres:5432/postgres", "postgres", "postgres"));
    createPlayersTable();
    createConnector("http://connect:8083/connectors/", "football-connector.json");
    sleep(2000); // some time to init the connector
}