ApplicationContext.xml with datasource or hibernate.cfg.xml. Difference?

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

Question 1: What is the difference between 1 and 2. Are they both the same with the other approach?

Question 2:

I asked a question on the Spring forum regarding some problem. The fact that he mentioned the pool below

if you need / want to use the internal connection pool for sleep mode, I would advise against this and just configure a data source that supports pooling and injects it into your sessionfactorybean.

union of internal joins for hibernate = this is the number 2 below . Right?

just set up a data source that supports pooling and inserts it into your sessionfactorybean = This is the number 1 below . is not it?

one # -

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

Information about the pool and connection is in the hibernate.cfg.xml file

 <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml" /> </bean> 
+21
spring java-ee hibernate struts2
Sep 28 '12 at 6:07
source share
2 answers

Answer 1:

Both approaches are the same. By default, hibernate reads the configuration from classpath:hibernate.cfg.xml for the SessionFactory assembly. LocalSessionFactoryBean simply allows you to configure hibernation configuration inside applicationContext.xml instead of hibernate.cfg.xml .

If the same property is specified in both files, depending on the property, it will have an addictive effect, or the properties specified in applicationContext.xml will have a higher priority, so these values ​​in hibernate.cfg.xml will be ignored.

For method 1, annotatedClasses and hibernateProperties should have an addictive effect with the corresponding values ​​in hibernate.cfg.xml . DBCPSouruce data in applicationContext.xml should ignore related properties in hibernate.cfg.xml .

Answer 2:

For method 2, if you do not specify any LocalSessionFactoryBean properties, all hibernation configurations are defined by hibernate.cfg.xml . If the connection pool is not configured in hibernate.cfg.xml , hibernate uses its own pooling algorithm by default , which is rather rudimentary and is not intended for use in a production system, or even for performance testing.

+10
Sep 28 '12 at 8:09
source share

If you want to create a factory session, you will get the same result with both approaches. I do not think that more can be done than others.

In my opinion, you would use the hibernate.cfg.xml method if you are not using Spring. When JUnit tests your DAOs, for example. No need to create a Spring application context for your tests to run faster.

However, when you use Spring, I think it's good to keep your data source separate from the factory session. You use Spring for dependency injection, right? Why not use Spring to give your factory session what it needs?

+4
Sep 28 '12 at 6:50
source share



All Articles