1

I have openSessionInView filter in web.xml.

<filter>
    <filter-name>openSessionInView</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>

And I have set allowCreate property of HibernateDaoSupport to true. Now for each database operation if I get the Session by getSession and close the Session after transaction like:

public List<User> getAllUsers() {
    Session session = getSession();
    session.enableFetchProfile("USER-ROLE-PROFILE");
    Transaction transaction = session.beginTransaction();
    DetachedCriteria criteria = DetachedCriteria.forClass(User.class);
    List<User> users = criteria.getExecutableCriteria(session).list();
    transaction.commit();
    session.disableFetchProfile("USER-ROLE-PROFILE");
    session.close();
    return users;
}

then would this Session close create any problem in openSessionInView?

Another question: Is this a good way to do various hibernate operation? In the above code the entity User has a fetch profile.

Thanks and regards.

Tapas Bose
  • 28,796
  • 74
  • 215
  • 331

2 Answers2

2

if the getAllUsers() is the last thing to do in a request lifecycle, this approach could be acceptable. But if you want to do more database operations, then you have to open a new session, because you have closed it. Moreover, if you don't close your session, it will be closed by filter:

    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {

...// sf is SessionFactory

            sf.getCurrentSession().beginTransaction();

            // Call the next filter (continue request processing)
            chain.doFilter(request, response);

            sf.getCurrentSession().getTransaction().commit();

...
        }
Erhan Bagdemir
  • 5,231
  • 6
  • 34
  • 40
  • @Erhan Bagdemir I read that getSession() returns a new session if we declare allowCreate to true in HibernateDaoSupport property. I am thinking is this have effect on website where multiple user access concurrently? – Tapas Bose May 29 '11 at 17:17
  • if can still protect your code from multiple request (threads) with synchronization. – Erhan Bagdemir May 29 '11 at 17:25
  • @Erhan Bagdemir so it is advisable to write synchronized method for database save/update/delete by adding synchronized keyword. Isn't it? – Tapas Bose May 29 '11 at 17:29
  • 1
    So if you start a transaction and do your operation in it , the other requests won't be problem because of ACID (Unit of Work) rules. If you use openSessionInView pattern, then for each request you have just one transaction. All operations in this transaction will be isolated. If you start your transaction in your method - and also commit in it, you can protect multiple access to this method using sync. in DAO. – Erhan Bagdemir May 29 '11 at 17:35
  • @Erhan Bagdemir so far what I understand that if each method of DAO is synchronized and all the unit operation is done within transaction begin-close and the for each method in DAO if session is opened in the beginning (by getSession) and closed just before the method end then it will not create any problem in openSessionInView. Am I right? – Tapas Bose May 29 '11 at 17:55
  • say if you have one more operation which is called updateUserLog() and which will be called after getAllUsers(), then you have to open a new session, just because you closed it in getAllUsers(). – Erhan Bagdemir May 29 '11 at 18:08
  • my suggestion: use opensessioninview filter, call getCurrentSession() thread-safe method, session.save/update etc. and let your filter close your session or commit transaction. – Erhan Bagdemir May 29 '11 at 18:10
1

Yes, it will cause problems. By using the filter you are declaring that your session is managed (created and closed) externally. So if you close it yourself, the external mechanism won't work (and may throw an exception)

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • thanks. If I remove session.close then it would be okay! Should I do each unit operation within transaction begin-close? – Tapas Bose May 29 '11 at 17:13
  • since you are using spring, you can use spring transaction management as well. Check the docs: http://static.springsource.org/spring/docs/current/spring-framework-reference/html/transaction.html – Bozho May 29 '11 at 17:15