Women in Technology

Hear us Roar



Article:
  Database Connection Pooling with Tomcat
Subject:   Old-fashioned article
Date:   2006-04-28 07:54:05
From:   Kunal_Jaggi
Response to: Old-fashioned article

Spring's approach for DBCP is no different from obtaining a DataSource from JNDI (or DI pattern) as shown in the following code snippets.


Spring Code (Enrollment-Config.xml)
-------------------------------------
<beans>


<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>


<property name="url">
<value>jdbc:mysql://localhost/dbcptest</value>
</property>


</bean>


</beans>


Spring Application Code
------------------------------
public class StudentEnroller implements EnrollAStudent{
private Resource resource;
private BeanFactory factory;
private DataSource ds;
private JdbcTemplate jt;


public StudentEnroller(){
try{
resource=new ClassPathResource("Enrollment-Config.xml");
factory=new XmlBeanFactory(resource);
ds=(DataSource)factory.getBean("dataSource");


jt=new JdbcTemplate (ds);


}catch(Exception e){
//....
//log the error
}
}


There's nothing earth-shattering here!

Spring provides a choice though, you can inject dependency rather than looking for it.