[英]How to configure Connection Pool for Java Web Project?
I've been doing a research on connection pool with JDBC api and classes. 我一直在研究JDBC api和类的连接池。 But still I don't know how to configure a Connection Pool class for a java web project.
但我仍然不知道如何为java Web项目配置连接池类。 As you may know, the Connection Pool is a Singleton Class that encapsulates the JDBC apis.
您可能知道,连接池是一个封装JDBC apis的Singleton类。 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.
但是当在Tomcat服务器上部署Web项目时,Connection类会启动一次,我想知道是否需要使用Web.xml配置文件来完成,让Tomcat Server加载连接池类。 Thank you very much for your time!
非常感谢您的宝贵时间!
There a many ways of doing this. 有很多方法可以做到这一点。 JNDI is one way - but it is not so "user friendly" in tomcat.
JNDI是一种方式 - 但它在tomcat中不那么“用户友好”。 The datasource also can be configured in Spring.
数据源也可以在Spring中配置。 For eg:
例如:
<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. 最简单的方法是创建一个ServletContextListener类,创建数据源并将其放入ServletContext中,以便可以从项目中的
ServletContext
中检索相同的实例。
See my answer here how to create ServletContextListener
. 在这里查看我的答案如何创建
ServletContextListener
。
See also: 也可以看看:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.