Github user ijokarumawak commented on the issue:
https://github.com/apache/nifi/pull/1450
Hi @ToivoAdams , thank you for your contribution. I reviewed it and saw the
sample code using Spring JDBC template. Then I felt that this can be done
outside of DBCPService. Adding getDataSource to DBCPService would be overkill.
Instead of adding getDataSource method, how about adding a test method in
DBCPServiceTest like below:
```java
/**
* Test database queries using Derby through Spring JDBC template.
* Connect, create table, insert, select, drop table.
* This is more of an example to use DBCPService with Spring JDBC.
*/
@Test
public void testSpringJDBCTemplate() throws InitializationException,
SQLException {
final TestRunner runner =
TestRunners.newTestRunner(TestProcessor.class);
final DBCPConnectionPool service = new DBCPConnectionPool();
runner.addControllerService("test-good1", service);
// remove previous test database, if any
final File dbLocation = new File(DB_LOCATION);
dbLocation.delete();
// set embedded Derby database connection url
runner.setProperty(service, DBCPConnectionPool.DATABASE_URL,
"jdbc:derby:" + DB_LOCATION + ";create=true");
runner.setProperty(service, DBCPConnectionPool.DB_USER, "tester");
runner.setProperty(service, DBCPConnectionPool.DB_PASSWORD,
"testerp");
runner.setProperty(service, DBCPConnectionPool.DB_DRIVERNAME,
"org.apache.derby.jdbc.EmbeddedDriver");
runner.enableControllerService(service);
runner.assertValid(service);
final DBCPService dbcpService = (DBCPService)
runner.getProcessContext().getControllerServiceLookup().getControllerService("test-good1");
Assert.assertNotNull(dbcpService);
// Create a jdbcTemplate. Wrap dbcpService so that it can act as a
DataSource.
JdbcTemplate jdbcTemplate = new JdbcTemplate(new BasicDataSource() {
@Override
public Connection getConnection() throws SQLException {
return dbcpService.getConnection();
}
@Override
public Connection getConnection(String user, String pass)
throws SQLException {
throw new UnsupportedOperationException("User and password
can not be overwritten.");
}
});
try {
jdbcTemplate.execute(dropTable);
} catch (final Exception e) {
// table may not exist, this is not serious problem.
}
jdbcTemplate.execute(createTable);
jdbcTemplate.update("insert into restaurants values (1, 'Irifunes',
'San Mateo')");
jdbcTemplate.update("insert into restaurants values (2, 'Estradas',
'Daly City')");
jdbcTemplate.update("insert into restaurants values (3, 'Prime Rib
House', 'San Francisco')");
int nrOfRows = jdbcTemplate.queryForObject("select count(*) from
restaurants", Integer.class);
assertEquals(3, nrOfRows);
}
```
This way, we can let other developers know that there're users using
DBCPService in their custom processors integrated with Spring JDBC framework.
Also if DBCPService changes its signature or behavior in the future, we can
detect that breaking change by this test.
How do you think?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---