applicationContext.xml与数据源或hibernate.cfg.xml。区别? [英] applicationContext.xml with datasource or hibernate.cfg.xml. Difference?

查看:123
本文介绍了applicationContext.xml与数据源或hibernate.cfg.xml。区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望澄清一些混淆。我有applicationContext.xml。



问题1: 1& 2.它们是否与不同的方法相同?

问题2:

我在Spring论坛上问了一些问题。如果你需要/希望使用hibernate的内部连接池
,那么他提到的关于pooling的一点是低于


它只需配置一个
支持连接池的数据源,并将它注入到
sessionfactorybean中。


hibernate的内部连接池= 这是下面的数字2 。只需配置一个支持连接池的数据源并将其注入sessionfactorybean = 这是下面的数字1 >。对吗?



1# -

 < bean id =dataSourceclass =org.apache.commons.dbcp.BasicDataSourcedestroy-method =close> 
< property name =driverClassNamevalue =$ {jdbc.driverClassName}/>
< property name =urlvalue =$ {jdbc.url}/>
< property name =usernamevalue =$ {jdbc.username}/>
< property name =passwordvalue =$ {jdbc.password}/>
< property name =maxActivevalue =100/>
< property name =maxIdlevalue =30/>
< property name =maxWaitvalue =16000/>
< property name =minIdlevalue =0/>
< / bean>

<! - Hibernate SessionFactory - >
< bean id =sessionFactoryclass =org.springframework.orm.hibernate4.LocalSessionFactoryBean>
< property name =dataSourceref =dataSource/>
< property name =annotatedClasses>
< list>
< value> com.mkyong.customer.model.Customer< / value>
< / list>
< / property>
< property name =hibernateProperties>
<道具>
< 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>
< /道具>
< / property>
< / bean>

2# -



池和连接信息位于hibernate.cfg.xml中

 < bean id =sessionFactoryclass = org.springframework.orm.hibernate4.LocalSessionFactoryBean> 
< property name =configLocationvalue =classpath:hibernate.cfg.xml/>
< / bean>


解决方案

答案1:



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



如果在两个文件中都指定了相同的属性,则取决于属性,它会产生上瘾效果或者 applicationContext.xml 将具有更高的优先级,因此 hibernate.cfg.xml 中的值将被忽略。



对于方法1, annotatedClasses hibernateProperties 应该具有上瘾效果在 hibernate.cfg.xml 中的相应值。 applicationContext.xml 中的DBCP dataSouruce应该会导致忽略 hibernate.cfg.xml 中的相关属性。



答案2:
$ b

对于方法2,如果您没有指定任何属性 LocalSessionFactoryBean ,所有的hibernate配置都由 hibernate.cfg.xml 指定。如果在 hibernate.cfg.xml 中没有配置连接池,默认情况下使用hibernate自己的连接池算法,这是非常基本的,不适用于生产系统甚至性能测试。 / p>

Want to clear some confusion. I have applicationContext.xml.

Question 1: Whats the difference between 1 & 2. Are they both same with different approach?

Question 2:

I asked question on Spring forum regarding some problem. Onething he mentioned about pooling is below

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.

internal connection pooling for hibernate = This is number 2 below. Right?

simply configure a datasource which supports connection pooling and inject that into your sessionfactorybean = This is number 1 below. right?

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# -

Pooling and connection info is in hibernate.cfg.xml

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

解决方案

Answer 1:

Both approaches are the same. By default , hibernate reads the configuration from classpath:hibernate.cfg.xml to build SessionFactory . LocalSessionFactoryBean just allows you to setup hibernate configuration inside applicationContext.xml instead of hibernate.cfg.xml .

If a same property is specified in both files , depending on the property , it will has addictive effect or the properties specified in applicationContext.xml will take higher priority such that those values in hibernate.cfg.xml will be ignored.

For method 1, annotatedClasses and hibernateProperties should have the addictive effect with the corresponding values in hibernate.cfg.xml . The DBCP dataSouruce in applicationContext.xml should cause the related properties in hibernate.cfg.xml being ignored.

Answer 2:

For method 2 , if you don't specify any properties of LocalSessionFactoryBean , all the hibernate configurations are specified by the hibernate.cfg.xml. If there are no connection pool configured in hibernate.cfg.xml , hibernate's own connection pooling algorithm is used by default , which is quite rudimentary and not intended for use in a production system, or even for performance testing.

这篇关于applicationContext.xml与数据源或hibernate.cfg.xml。区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆