AOP配置事务javax.persistence.TransactionRequiredException: no transaction is in progress

事务配置如下:

serviceimpl类的方法

报错

javax.persistence.TransactionRequiredException: no transaction is in progress
	at org.hibernate.internal.AbstractSharedSessionContract.checkTransactionNeededForUpdateOperation(AbstractSharedSessionContract.java:409)
	at org.hibernate.internal.SessionImpl.checkTransactionNeededForUpdateOperation(SessionImpl.java:3602)
	at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1483)
	at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1479)  

 说明:serviceimpl的方法名是find*();已经在事务中配置为<tx:method name="find*" propagation="SUPPORTS"/> 

            SUPPORTS:支持当前事务,如果当前没有事务,就以非事务方式执行。 

            不明白为什么还是会报javax.persistence.TransactionRequiredException: no transaction is in progress

             后来将<tx:method name="find*" propagation="SUPPORTS"/> 改为<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>问题解决

            read-only应用场合:

          如果你一次执行单条查询语句,则没有必要启用事务支持,数据库默认支持SQL执行期间的读一致性;
          如果你一次执行多条查询语句,例如统计查询,报表查询,在这种场景下,多条查询SQL必须保证整体的读一致性,否则,在前条SQL查询之后,后条SQL查询之前,数据被            其他用户改变,则该次整体的统计查询将会出现读数据不一致的状态,此时,应该启用事务支持。
        【注意是一次执行多次查询来统计某些信息,这时为了保证数据整体的一致性,要用只读事务】

 

posted @ 2019-08-28 16:29  sunshineBaby  阅读(5893)  评论(0编辑  收藏  举报