Srivatsan
Srivatsan

Reputation: 77

Configure Spring 4 JDBC JDBCTemplate with Connection Provider class

Is there a way i can configure the spring 4 JDBCTemplate data source with a Connection provider class like the one hibernate provides?

I have connections managed by connection pool provided by a Java class. I can get connection through the provider class but i'm not sure how to configure the JDBCTemplate datasource with that.

@Configuration
public class MyDataSourceConfig {
/**
 * My data source.
 * 
 * @return the data source
 */
@Bean(name = "myDS")
@Primary
public DataSource myDataSource() {
    // I need to add a way to get a data source object using the connection
    // from the class
    Connection conn = DBConnection.getConnection();
    /**
     * TODO Add code to create data source with the connection provider
     * DBConnection.class
     */
    return dataSource;

}

@Bean(name = "jdbcMydb")
@Autowired
public JdbcTemplate hrdbJdbcTemplate(@Qualifier("myDS") DataSource jdbcMydb) {
    return new JdbcTemplate(jdbcMydb);
}}

Upvotes: 1

Views: 710

Answers (1)

kuhajeyan
kuhajeyan

Reputation: 11017

One solution would be for you to extend AbstractDataSource and override getConnection() method and write new DataSource for you. Or to probably make easier by extending concrete classes like SimpleDriverDataSource

Upvotes: 1

Related Questions