Since you speak JDBC, I assume you mean Java? Your question seems to have some ambiguity, so I'm not sure if this is what you are looking for, but based on my understanding, I will do it. Anyway, I use the connection pool (Apache commons dbcp) and Spring, which makes it pretty simple.
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost/databasename"/> <property name="username" value="root"/> <property name="password" value="password"/>
Then in the code I use Spring jdbctemplate, and with this setting, the database connections are combined and reused. The data source is managed as a Spring bean and then injected into the dependency where it is used. Spring handled the separation of jdbc sessions for you and voilà! Here's how I do dependency injection with annotations:
private JdbcTemplate jdbcTemplate; @Autowired public void setDataSource(DataSource dataSource) { this.jdbcTemplate = new JdbcTemplate(dataSource); }
Even if you are not using Spring for MVC or anything else, Spring JDBC tools are really nice.
titania424
source share