我正在学习如何配置使用tomcat的hibernate。现在,我想在tomcat上配置连接池。
我希望将服务器上的连接池配置为与两个应用程序共享池。目前,我有一个包含所有jar文件的java应用程序。-Mysql-连接器-hibernate - c3p0-
Tomcat在池中有一个可以通过JNDI获得连接的构建。现在我如何使用c3p0与多个应用程序共享我的池?
我必须使用游泳池中的构建吗?我不想用我的WAR文件部署多个池。
发布于 2017-06-27 07:39:15
您可以使用Tomcat和JNDI想要的任何连接池。使用资源标记的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" />发布于 2017-06-27 07:53:28
您可以在应用程序(~.cfg.xml)中使用此配置来通过JNDI检查DataSource
<?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,则为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>
我希望这对你有帮助
https://stackoverflow.com/questions/44774523
复制相似问题