S2SH使用OpenSessionInViewFilter出现InvalidDataAccessApiUsageException

    org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.


    项目里是基于struts2、hibernate、spring框架,,关于事务出现的异常问题使用hibernate的OpenSessionViewFilter来管理session,所以:

    重写如下:

public class OpenSessionInViewFilter extends org.springframework.orm.hibernate3.support.OpenSessionInViewFilter{

/* (non-Javadoc)
 * @see org.springframework.orm.hibernate3.support.OpenSessionInViewFilter#getSession(org.hibernate.SessionFactory)
 */
@Override
protected Session getSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException {
Session session = SessionFactoryUtils.getSession(sessionFactory, true);
session.setFlushMode(FlushMode.COMMIT);
return session;
}

/* (non-Javadoc)
 * @see org.springframework.orm.hibernate3.support.OpenSessionInViewFilter#closeSession(org.hibernate.Session, org.hibernate.SessionFactory)
 */
@Override
protected void closeSession(Session session, SessionFactory sessionFactory) {
session.flush();
super.closeSession(session, sessionFactory);
}




你可能感兴趣的:(S2SH使用OpenSessionInViewFilter出现InvalidDataAccessApiUsageException)