gpt4 book ai didi

spring - 带有数据源的 applicationContext.xml 或 hibernate.cfg.xml。区别?

转载 作者:IT老高 更新时间:2023-10-28 13:48:51 26 4
gpt4 key购买 nike

想解开一些困惑。我有 applicationContext.xml。

问题 1: 1 和 2 之间有什么区别。它们是否相同但方法不同?

问题 2:

我在 Spring 论坛上就一些问题提出了问题。下面是他提到的关于池化的内容

if you need/want to use the internal connection pooling for hibernate I would advice against it and simply configure a datasource which supports connection pooling and inject that into your sessionfactorybean.

hibernate 的内部连接池 = 这是下面的第 2 位。对吧?

只需配置一个支持连接池的数据源并将其注入(inject)您的 sessionfactorybean = 这是下面的数字 1。对吧?

1# -

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxActive" value="100"/>
<property name="maxIdle" value="30"/>
<property name="maxWait" value="16000"/>
<property name="minIdle" value="0"/>
</bean>

<!-- Hibernate SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="annotatedClasses">
<list>
<value>com.mkyong.customer.model.Customer</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.generate_statistics">true</prop>
</props>
</property>
</bean>

2#-

池和连接信息在 hibernate.cfg.xml 中

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
</bean>

最佳答案

答案 1:

这两种方法都是一样的。默认情况下,hibernate 从 classpath:hibernate.cfg.xml 读取配置来构建 SessionFactoryLocalSessionFactoryBean 只允许您在 applicationContext.xml 而不是 hibernate.cfg.xml 中设置 hibernate 配置。

如果在两个文件中指定了相同的属性,根据属性,它会产生令人上瘾的效果,或者 applicationContext.xml 中指定的属性将具有更高的优先级,以便 中的那些值hibernate.cfg.xml 将被忽略。

对于方法一,annotatedClasseshibernatePropertieshibernate.cfg.xml 中的对应值应该具有令人上瘾的效果。 applicationContext.xml 中的 DBCP 数据源应该会导致 hibernate.cfg.xml 中的相关属性被忽略。

答案 2:

对于方法2,如果不指定LocalSessionFactoryBean的任何属性,那么所有的hibernate配置都由hibernate.cfg.xml指定。如果 hibernate.cfg.xml 中没有配置连接池,hibernate's own connection pooling algorithm is used by default ,这是相当初级的,不适合在生产系统中使用,甚至不用于性能测试。

关于spring - 带有数据源的 applicationContext.xml 或 hibernate.cfg.xml。区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12634475/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com