【问题标题】:Is c3p0 configurable on the server instead of WAR file?c3p0 是否可以在服务器而不是 WAR 文件上配置?
【发布时间】:2017-11-30 04:01:15
【问题描述】:

我正在学习如何使用 tomcat 配置休眠。现在 我想在 tomcat 上配置连接池。

我想在服务器上配置连接池以与两个应用程序共享池。目前我有一个包含我所有 jar 文件的 java 应用程序。 -Mysql-连接器- -休眠- -c3p0-

Tomcat 有一个内置池,可以通过 JNDI 获得连接。 现在如何使用 c3p0 与多个应用共享我的池?

我必须使用内置池吗?我不想用我的 WAR 文件部署多个池。

【问题讨论】:

    标签: java hibernate tomcat connection-pooling


    【解决方案1】:

    您可以对 Tomcat 和 JNDI 使用任何您希望的连接池。您可以使用 Resource 标记的 type 属性指定池。如果您希望多个 Web 应用程序可以访问它,则需要在 server.xml 中指定资源

    例如

    <Resource name="jdbc/myDataSource" 
      auth="Container"
      factory="org.apache.naming.factory.BeanFactory"
      type="com.mchange.v2.c3p0.ComboPooledDataSource" 
      driverClassName="com.mysql.jdbc.Driver"
      jdbcUrl="jdbc:mysql://localhost/myDataSource" 
      user="user" password="password"
      minPoolSize="3" 
      maxPoolSize="15" 
      maxIdleTime="5000"
      idleConnectionTestPeriod="300" 
      acquireIncrement="3" />
    

    【讨论】:

    • 我必须复制tomcat lib文件夹中的c3p0 jar文件吗?
    • 好的。我的带有 cp30 设置的休眠配置文件也在我的项目中。所以这也应该在服务器上?
    • Web 容器 (Tomcat) 不需要了解 ORM。这是应用程序级别的问题。我不熟悉hibernate,但我认为任何配置文件都必须放在每个Web应用程序的类路径中(可能是WebContent/WEB-INF/classes
    • 是的,但我的休眠文件中有我的 c3p0 设置:/.
    • 对不起,就像我说的,我使用的是不同的 ORM(无配置)
    【解决方案2】:

    您可以在您的应用程序中使用此配置 (~.cfg.xml) 通过 JNDI 检查数据源

    <?xml version='1.0' encoding='utf-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate 
     Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
     <session-factory>
        <property name="dataSource">java:comp/env/jdbc/JNDI_Name</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property><!-- your dialect-->
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <mapping resource="mapping/xxx.hbm.xml"/>
        <mapping resource="mapping/yyy.hbm.xml"/>
     </session-factory>
    </hibernate-configuration>
    

    如果你使用 Spring xml conf,为 Datasource && sessionFactory && transactionManager 创建一个 xml 文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd">
    
    
    <tx:annotation-driven />
    <context:component-scan base-package="x.y.z.*" />
    <context:annotation-config />
    
    <import resource="classpath:~/~.cfg.xml" />
    
    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="java:comp/env/jdbc/Your_JNDI_Name"/>
    </bean>
    
    <!--  OR ORM HIBERNATE PART -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="configLocation">
            <value>classpath:~/~.cfg.xml</value>
        </property>
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"
          p:sessionFactory-ref="sessionFactory">
    </bean>
    
    <!-- Declaration of DOA beans Hibernate -->
    <bean id="myDao" class="class_dao">
        <property name="sessionFactory">
            <ref bean="sessionFactory"/>
        </property>
    </bean>
    

    希望对你有帮助

    【讨论】:

    • 多解释一点就好了
    • 在这一步中,我假设你的 catalina 暴露了 JNDI(conf tomcat 端完成),第一个 conf 是用于 hiberbate(sessionFactory) 来捕获 JNDI,第二个 conf 是用于将使用sessionFactory.
    • 今天我没有再问一个问题,也许你可以帮助我stackoverflow.com/questions/44801615/…。我不明白如何在 tomcat 上使用 sessionfactory 和 c3p0
    • @Khan 你可以看到,我发布了回复
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-23
    • 2015-12-13
    • 1970-01-01
    • 2019-01-11
    • 2018-11-03
    • 1970-01-01
    相关资源
    最近更新 更多