My sample dao: In one single transaction, we will have to execute few methods as shown below which would involve create,update,delete, so we cannot commit in the code. Hence, we rely on Spring Transaction Manager which we configured and it is working as expected.
public class TestDao extends HibernateDaoSupport public Collection searchForItems() { try { Session session = this.getSession(); Criteria criteria = session.createCriteria(); Collection collection= criteria.list(); return collection; } catch (HibernateException e) { } } }
Transaction: The transaction will be taken care of by Spring Transaction Manager configured and this is working fine.
Questions:1. My First Question is, in the above code should we close the session explicitly (like session.close()) or when the Spring Transaction Manager commits the unit of work, it will take care of closing sessions. 2. My Second Question is, if we should close the session (like session.close()) will this trigger a commit meaning will the unit of work to that point will get commited.
Sessionfactory configuration: <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dsJndi"></property> <property name="configLocations"> <list> <value>/WEB-INF/hibernate-config.xml</value> </list> </property> </bean>
|