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>
Applicationcontext. xml - It is standard spring context file which contains all beans and the configuration that are common among all the servlets. It is optional file in case of web app. Spring uses ContextLoaderListener to load this file in case of web application. Spring-servlet.
Spring is one of the most used Java EE Framework and Hibernate is the most popular ORM framework. That's why Spring Hibernate combination is used a lot in enterprise applications.
These configurations contain the mapping information that provides different functionalities to Java classes. Generally, we provide database related mappings in the configuration file. Hibernate facilitates to provide the configurations either in an XML file (like hibernate. cfg.
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.
If what you want is to build a session factory, you will get the same result with both approaches. I don't think one can do more than the other.
In my opinion, you would use the hibernate.cfg.xml approach when you are not using Spring. When JUnit testing your DAOs for example. Not having to build a Spring application context makes your tests run faster.
However, when you are using Spring, I think it's a good thing to keep your datasource separated from the session factory. You are using Spring for dependency inject, right? Why not using spring to give your session factory what it needs?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With