【SSH异常】InvalidDataAccessApiUsageException异常
今天在整合SSH的時候,一開始我再測試的時候service層添加了注解事務調用DAO可以正常的保存,在環境中我在XML中采用了spring的OpenSessionInViewFilter解決hibernate的no-session問題(防止頁面采用藍懶加載的對象,此過濾器在訪問請求前打開session,訪問結束關閉session),xml配置如下:
<!--Hibernate的session丟失解決方法 --><filter><filter-name>openSessionInView</filter-name><filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class></filter><filter-mapping><filter-name>openSessionInView</filter-name><url-pattern>/*</url-pattern></filter-mapping>?
?
當我在service層將事務注解注釋掉之后報錯,代碼如下,錯誤如下:
?
?錯誤信息:
org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.
at org.springframework.orm.hibernate5.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:1126)
?
原因:
上面的錯誤:
只讀模式下(FlushMode.NEVER/MANUAL)寫操作不被允許:把你的Session改成FlushMode.COMMIT/AUTO或者清除事務定義中的readOnly標記。
?
摘自一位大牛的解釋:
OpenSessionInViewFilter在getSession的時候,會把獲取回來的session的flush mode 設為FlushMode.NEVER。然后把該sessionFactory綁定到TransactionSynchronizationManager,使request的整個過程都使用同一個session,在請求過后再接除該sessionFactory的綁定,最后closeSessionIfNecessary根據該session是否已和transaction綁定來決定是否關閉session。在這個過程中,若HibernateTemplate 發現自當前session有不是readOnly的transaction,就會獲取到FlushMode.AUTO Session,使方法擁有寫權限。也即是,如果有不是readOnly的transaction就可以由Flush.NEVER轉為Flush.AUTO,擁有insert,update,delete操作權限,如果沒有transaction,并且沒有另外人為地設flush model的話,則doFilter的整個過程都是Flush.NEVER。所以受transaction(聲明式的事務)保護的方法有寫權限,沒受保護的則沒有。?
查看OpenSessionInViewFilter源碼:
* Copyright 2002-2015 the original author or authors.package org.springframework.orm.hibernate5.support;import java.io.IOException; import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;import org.hibernate.FlushMode; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory;import org.springframework.dao.DataAccessResourceFailureException; import org.springframework.orm.hibernate5.SessionFactoryUtils; import org.springframework.orm.hibernate5.SessionHolder; import org.springframework.transaction.support.TransactionSynchronizationManager; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.request.async.WebAsyncManager; import org.springframework.web.context.request.async.WebAsyncUtils; import org.springframework.web.context.support.WebApplicationContextUtils; import org.springframework.web.filter.OncePerRequestFilter; public class OpenSessionInViewFilter extends OncePerRequestFilter {public static final String DEFAULT_SESSION_FACTORY_BEAN_NAME = "sessionFactory";private String sessionFactoryBeanName = DEFAULT_SESSION_FACTORY_BEAN_NAME; public void setSessionFactoryBeanName(String sessionFactoryBeanName) {this.sessionFactoryBeanName = sessionFactoryBeanName;} protected String getSessionFactoryBeanName() {return this.sessionFactoryBeanName;}@Overrideprotected boolean shouldNotFilterAsyncDispatch() {return false;}@Overrideprotected boolean shouldNotFilterErrorDispatch() {return false;}@Overrideprotected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)throws ServletException, IOException {SessionFactory sessionFactory = lookupSessionFactory(request);boolean participate = false;WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);String key = getAlreadyFilteredAttributeName();if (TransactionSynchronizationManager.hasResource(sessionFactory)) {// Do not modify the Session: just set the participate flag.participate = true;}else {boolean isFirstRequest = !isAsyncDispatch(request);if (isFirstRequest || !applySessionBindingInterceptor(asyncManager, key)) {logger.debug("Opening Hibernate Session in OpenSessionInViewFilter");Session session = openSession(sessionFactory);SessionHolder sessionHolder = new SessionHolder(session);TransactionSynchronizationManager.bindResource(sessionFactory, sessionHolder);AsyncRequestInterceptor interceptor = new AsyncRequestInterceptor(sessionFactory, sessionHolder);asyncManager.registerCallableInterceptor(key, interceptor);asyncManager.registerDeferredResultInterceptor(key, interceptor);}}try {filterChain.doFilter(request, response);}finally {if (!participate) {SessionHolder sessionHolder =(SessionHolder) TransactionSynchronizationManager.unbindResource(sessionFactory);if (!isAsyncStarted(request)) {logger.debug("Closing Hibernate Session in OpenSessionInViewFilter");SessionFactoryUtils.closeSession(sessionHolder.getSession());}}}} protected SessionFactory lookupSessionFactory(HttpServletRequest request) {return lookupSessionFactory();} protected SessionFactory lookupSessionFactory() {if (logger.isDebugEnabled()) {logger.debug("Using SessionFactory '" + getSessionFactoryBeanName() + "' for OpenSessionInViewFilter");}WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());return wac.getBean(getSessionFactoryBeanName(), SessionFactory.class);} protected Session openSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException {try {Session session = sessionFactory.openSession();session.setFlushMode(FlushMode.MANUAL);return session;}catch (HibernateException ex) {throw new DataAccessResourceFailureException("Could not open Hibernate Session", ex);}}private boolean applySessionBindingInterceptor(WebAsyncManager asyncManager, String key) {if (asyncManager.getCallableInterceptor(key) == null) {return false;}((AsyncRequestInterceptor) asyncManager.getCallableInterceptor(key)).bindSession();return true;}}?
?
上面將FlushMode設置MANUAL,查看FlushMode源碼:
package org.hibernate;import java.util.Locale; public enum FlushMode {@DeprecatedNEVER ( 0 ), MANUAL( 0 ), COMMIT(5 ), AUTO(10 ), ALWAYS(20 );private final int level;private FlushMode(int level) {this.level = level;} public boolean lessThan(FlushMode other) {return this.level < other.level;} @Deprecatedpublic static boolean isManualFlushMode(FlushMode mode) {return MANUAL.level == mode.level;}public static FlushMode interpretExternalSetting(String externalName) {if ( externalName == null ) {return null;}try {return FlushMode.valueOf( externalName.toUpperCase(Locale.ROOT) );}catch ( IllegalArgumentException e ) {throw new MappingException( "unknown FlushMode : " + externalName );}} }?
?
解決辦法:
在遇到上面錯誤之后我先打開@Transactional注解查看里面的源碼,此注解將readonly默認置為false,所以我們在有此注解的情況下可以進行save或者update操作。源碼如下:
* Copyright 2002-2015 the original author or authors.package org.springframework.transaction.annotation;import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;import org.springframework.core.annotation.AliasFor; import org.springframework.transaction.TransactionDefinition; @Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Inherited @Documented public @interface Transactional {@AliasFor("transactionManager")String value() default "";@AliasFor("value")String transactionManager() default "";Propagation propagation() default Propagation.REQUIRED;Isolation isolation() default Isolation.DEFAULT; int timeout() default TransactionDefinition.TIMEOUT_DEFAULT; boolean readOnly() default false; Class<? extends Throwable>[] rollbackFor() default {}; String[] rollbackForClassName() default {};Class<? extends Throwable>[] noRollbackFor() default {};String[] noRollbackForClassName() default {};}?
?
?解決辦法:
(1)在service層打上@Transactional注解,現在想想這個設計還挺合理,無形之中要求我們打上注解。
(2)重寫上面的過濾器,將打開session的FlushMode設為AUTO:(不推薦這種,因為在進行DML操作的時候最好加上事務控制,防止造成數據庫異常)
package cn.qlq.filter;import org.hibernate.FlushMode; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.dao.DataAccessResourceFailureException; import org.springframework.orm.hibernate5.support.OpenSessionInViewFilter; /*** 重寫過濾器去掉session的默認只讀屬性* @author liqiang**/ public class MyOpenSessionInViewFilter extends OpenSessionInViewFilter {@Overrideprotected Session openSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException {Session session = sessionFactory.openSession();session.setFlushMode(FlushMode.AUTO);return session;} }?
web.xml配置:
<!--Hibernate的session丟失解決方法 --><filter><filter-name>myOpenSessionInView</filter-name><filter-class>cn.qlq.filter.MyOpenSessionInViewFilter</filter-class></filter><filter-mapping><filter-name>myOpenSessionInView</filter-name><url-pattern>/*</url-pattern></filter-mapping>?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
FlushMode.COMMIT/AUTO
轉載于:https://www.cnblogs.com/qlqwjy/p/9535500.html
總結
以上是生活随笔為你收集整理的【SSH异常】InvalidDataAccessApiUsageException异常的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: poj3273---Monthly Ex
- 下一篇: 开始食用grpc(之一)