简体   繁体   English

如何为Java Web Project配置连接池?

[英]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.

相关问题 如何使用Hibernate配置连接池? - How to configure a connection pool with Hibernate? 如何配置eclipselink JTA序列连接池 - How to configure an eclipselink JTA sequence connection pool 如何配置连接池Apache Tomcat-&gt; PostgreSQL-&gt; Persistence Java? - How to configure a connection pool Apache Tomcat-> PostgreSQL-> Persistence Java? 如何在不使用Spring的Java中配置MongoDB来访问公共连接池? 如何使用单例模式建立与DB的公共连接? - How to configure MongoDB in java without Spring to access a common connection pool? How to make a common connection to DB with singleton pattern? 如何在Java Web项目中配置自定义日志文件 - How to configure custom log file in java web project 如何配置EclipseLink以将HikariCP用作其连接池 - How to configure EclipseLink to use HikariCP as its connection pool 如何在Java Web应用程序中跟踪连接池 - DBMS_APPLICATION_INFO - How to trace the connection pool in a Java Web application - DBMS_APPLICATION_INFO 线程池和连接池,需要帮助来理解它们是如何工作的 Java - Thread Pool and Connection Pool , need help to understand how they work Java 如何使用Java创建PostgreSQL连接池? - How create an PostgreSQL connection pool using Java? 如何使用Java JDBC连接池? - How to use Java JDBC connection pool?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM