Alexander
Alexander

Reputation: 79

How to configure Connection Pool for Java Web Project?

I've been doing a research on connection pool with JDBC api and classes. But still I don't know how to configure a Connection Pool class for a java web project. As you may know, the Connection Pool is a Singleton Class that encapsulates the JDBC apis. But the Connection Class is started once when the web project is deployed on Tomcat Server, I wonder if there's something needed to be done with Web.xml configuration file, to let Tomcat Server load the Connection Pool Class. Thank you very much for your time!

Upvotes: 1

Views: 380

Answers (1)

Paulius Matulionis
Paulius Matulionis

Reputation: 23415

There a many ways of doing this. JNDI is one way - but it is not so "user friendly" in tomcat. The datasource also can be configured in Spring. For e.g.:

<bean id="dataSource"
      class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>

The most simplest way of doing this is to create a ServletContextListener class, create your datasource and put it into ServletContext so that the same instance could be retrieved from ServletContext in your project.

See my answer here how to create ServletContextListener.

See also:

Upvotes: 3

Related Questions