Quantcast
Channel: Spring Community Forums - Data
Viewing all articles
Browse latest Browse all 297

Setting FlushMode.Commint in Spring 3.1 + Hibernate 4.

$
0
0
Hi all,
It's possible to set the FlushMode.COMMIT in HibernateSession using the "org.springframework.orm.hibernate4.support.OpenSe ssionInViewFilter" and "org.springframework.orm.hibernate4.HibernateTrans actionManager" as transactionManager?

With "org.springframework.orm.hibernate3.support.OpenSe ssionInViewFilter" you could set the FlushMode.COMMIT in this way:

Code:

<filter>
    <filter-name>openSessionInViewFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
    <init-param>
        <param-name>flushMode</param-name>
        <param-value>COMMIT</param-value>
    </init-param>
</filter>

but this solution it's not applicable in hibernate4 Filter. In this case it's always setted to MANUAL:

Code:

protected Session openSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException {
                try {
                        Session session = SessionFactoryUtils.openSession(sessionFactory);
                        session.setFlushMode(FlushMode.MANUAL);
                        return session;
                }
                catch (HibernateException ex) {
                        throw new DataAccessResourceFailureException("Could not open Hibernate Session", ex);
                }
        }

and then the HibernateTransactionManager set it to AUTO:

Code:

if (!definition.isReadOnly() && !txObject.isNewSession()) {
                                // We need AUTO or COMMIT for a non-read-only transaction.
                                FlushMode flushMode = session.getFlushMode();
                                if (FlushMode.isManualFlushMode(session.getFlushMode())) {
                                        session.setFlushMode(FlushMode.AUTO);
                                        txObject.getSessionHolder().setPreviousFlushMode(flushMode);
                                }
                        }

So apparently there's no way to change the FlushMode, the only solution seems to be to extend the HibernateTransactionManager to force the FlushMode.COMMIT, but IMHO this is not a great solution. There's some other way?

Viewing all articles
Browse latest Browse all 297

Trending Articles