Java Code Examples for org.springframework.jdbc.datasource.AbstractDriverBasedDataSource

Following code examples demonstrate how to use org.springframework.jdbc.datasource.AbstractDriverBasedDataSourcefrom spring-framework. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.springframework.jdbc.datasource.AbstractDriverBasedDataSourceand various code implementation of this class.

    public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {

        AbstractDriverBasedDataSource ds = (AbstractDriverBasedDataSource) dataSource;
        Properties prop = ds.getConnectionProperties();
        Boolean isTest = Boolean.parseBoolean(prop.getProperty("isTest"));

        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(dataSource);
        em.setPackagesToScan(new String[] { "dbmapping" });

        JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);
        em.setJpaProperties(additionalProperties(isTest));


        return em;
    } 


	public void test() throws SQLException {
		

		

		

		

		

		

		

		Properties[] properties = new Properties[] { null };
		Connection connection = Recorder.create("connection", this, Connection.class).p();
		AbstractDriverBasedDataSource ds = new AbstractDriverBasedDataSource() {
			@Override
			protected Connection getConnectionFromDriver(Properties props) throws SQLException {
				properties[0] = props;
				return connection;
			}
		};

		
		ds.setUrl("url");
		ds.setUsername("user1");
		ds.setPassword("password1");
		ds.setCatalog("catalog");
		ds.setSchema("schema");
		Properties connectionProperties = new Properties();
		connectionProperties.setProperty("user", "user2");
		connectionProperties.setProperty("password", "password2");
		connectionProperties.setProperty("foo", "bar");
		ds.setConnectionProperties(connectionProperties);

		aeqr(connection, ds.getConnection("user3", "password3"));

		
		aeq(3, properties[0].size());
		aeq("user3", properties[0].get("user"));
		aeq("password3", properties[0].get("password"));
		aeq("bar", properties[0].get("foo"));

		
		ateq("connection", "setCatalog", "\"catalog\"");
		ateq("connection", "setSchema", "\"schema\"");

		
		ds.getConnection();
		aeq("user1", properties[0].get("user"));
		aeq("password1", properties[0].get("password"));

		
		ds.setUsername(null);
		ds.setPassword(null);
		ds.getConnection();
		aeq("user2", properties[0].get("user"));
		aeq("password2", properties[0].get("password"));

		next(SimpleDriverDataSourceTest.class);
	} 

	public void beforeTestMethod(TestContext testContext) throws Exception {
		super.beforeTestMethod(testContext);

		ApplicationContext applicationContext = testContext.getApplicationContext();
		RecoverDb annotationOnClass = testContext.getTestClass().getAnnotation(RecoverDb.class);
		if (annotationOnClass == null) {
			RecoverDb annotationOnMethod = testContext.getTestMethod().getAnnotation(RecoverDb.class);
			handleOnAnnotation(applicationContext, annotationOnMethod);
		} else {
			handleOnAnnotation(applicationContext, annotationOnClass);
		}
	} 

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        AbstractDriverBasedDataSource dataSource = (AbstractDriverBasedDataSource) context.getBean(DataSource.class);

        System.out.println("url: " + dataSource.getUrl());
        System.out.println("username: " + dataSource.getUsername());
        System.out.println("password: " + dataSource.getPassword());
    } 

	private void assertCorrectSetupForSingleDataSource(String file, Predicate<String> urlPredicate) {
		ConfigurableApplicationContext context = context(file);
		try {
			DataSource dataSource = context.getBean(DataSource.class);
			assertNumRowsInTestTable(new JdbcTemplate(dataSource), 1);
			assertTrue(dataSource instanceof AbstractDriverBasedDataSource);
			AbstractDriverBasedDataSource adbDataSource = (AbstractDriverBasedDataSource) dataSource;
			assertTrue(urlPredicate.test(adbDataSource.getUrl()));
		}
		finally {
			context.close();
		}
	} 

	private void assertCorrectSetupAndCloseContext(String file, int count, String... dataSources) {
		ConfigurableApplicationContext context = context(file);
		try {
			for (String dataSourceName : dataSources) {
				DataSource dataSource = context.getBean(dataSourceName, DataSource.class);
				assertNumRowsInTestTable(new JdbcTemplate(dataSource), count);
				assertTrue(dataSource instanceof AbstractDriverBasedDataSource);
				AbstractDriverBasedDataSource adbDataSource = (AbstractDriverBasedDataSource) dataSource;
				assertThat(adbDataSource.getUrl(), containsString(dataSourceName));
			}
		}
		finally {
			context.close();
		}
	} 

	public static DataSource dataSource() {
		DataSource ds = new DriverManagerDataSource();
		((DriverManagerDataSource) ds).setDriverClassName("oracle.jdbc.driver.OracleDriver");
		((AbstractDriverBasedDataSource) ds).setUrl("jdbc:oracle:thin:@localhost:1521:orcl");
		((AbstractDriverBasedDataSource) ds).setUsername("scott");
		((AbstractDriverBasedDataSource) ds).setPassword("tiger");
		return ds;
	} 

	private AbstractDriverBasedDataSource getDataSource(){
		AbstractDriverBasedDataSource ds = (AbstractDriverBasedDataSource)SessionFactoryUtils.getDataSource(factory);
		return ds;
	} 

Advertisement
Javadoc
Abstract base class for JDBC javax.sql.DataSource implementations that operate on a JDBC java.sql.Dr

iver. @author Juergen Hoeller @since 2.5.5 @see SimpleDriverDataSource @see DriverManagerDataSource

Read More
Advertisement