Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring只读事务将数据提交到数据库_Java_Spring_Hibernate_Oracle11g - Fatal编程技术网

Java Spring只读事务将数据提交到数据库

Java Spring只读事务将数据提交到数据库,java,spring,hibernate,oracle11g,Java,Spring,Hibernate,Oracle11g,我试图为服务方法实现一个只读事务,遵循的是第11.5.2项,但该事务仍然会自动将数据提交到数据库 我使用的是Spring3.1.0.RELEASE、Hibernate3.5.5-Final和Oracle11gExpress版本11.2.0.2.0。以下是我的设置: 建议、切入点、顾问和事务管理器的XML: 服务接口: package my.example.service; public interface MyService { void getFoo(); void

我试图为服务方法实现一个
只读
事务,遵循的是第11.5.2项,但该事务仍然会自动将数据提交到数据库

我使用的是Spring3.1.0.RELEASE、Hibernate3.5.5-Final和Oracle11gExpress版本11.2.0.2.0。以下是我的设置:

建议、切入点、顾问和事务管理器的XML:


服务接口:

package my.example.service;

public interface MyService {

    void getFoo();

    void bar();
}
服务实现:

package my.example.service.impl;

import my.example.dao.MyDao;
import my.example.domain.MyEntity;
import my.example.service.MyService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MyServiceImpl implements MyService {

    @Autowired
    private MyDao dao;

    public void getFoo() {
        MyEntity example = this.dao.getMyEntity(1L);
        example.setSomeInteger(2);
        example.setSomeString("three");
    }

    public void bar() {
        MyEntity example = this.dao.getMyEntity(4L);
        example.setSomeInteger(5);
        example.setSomeString("six");
    }
}
调用
getFoo()
bar()
后,数据库将更新,即使
getFoo()
标记为
只读。但如果我改变这两行:

<tx:method name="get*" read-only="true" />
<tx:method name="*"/>

致:


这两种方法都尊重
只读
属性,并且数据不会提交到数据库


发生什么事了?我做错了什么?我错过了什么?

为什么不在
@Transactional
注释上使用readOnly标志呢

使用
@Transactional(readOnly=true)
注释您的方法,Hibernate将尝试以只读方式执行事务,并且(我认为,您可能需要再次检查此项)在尝试写入时将抛出异常


就这一点而言,尝试重新发明轮子是没有意义的。

我发现了为什么要覆盖
只读事务。在
web.xml
中有一个声明,以前的开发人员在加载控制器/界面中的用户及其角色时使用该声明来防止
LazyInitializationException
s

以下是过滤器声明:


openSessionInViewFilter
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
我通过配置过滤器来解决这个问题,让每个事务使用自己的会话(称为
延迟模式

以下是启用了延迟模式的过滤器声明:


openSessionInViewFilter
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
单一会话
假的

请阅读过滤器的Javadoc中的注释。我的DAO类扩展了
org.springframework.orm.hibernate3.support.HibernateDaoSupport
,继承的方法
getSession()
getSessionFactory().getCurrentSession()
返回的Hibernate会话的刷新模式设置为
FlushMode.MANUAL
,而不是
FlushMode.NEVER
,正如我在阅读后解释的那样。

实际的应用程序有39个服务类,每个类有大约20个公共方法,这就是以前的开发人员选择XML方法的原因。现在,如果我在XML上启用
,并将
@Transactional(readOnly=true)
添加到和单个方法中,它将被
忽略/覆盖。数据仍然被提交。如果
SessionFactory,则完全从XML中删除事务性建议、切入点和顾问、添加
、使用
@transactional
@transactional(readOnly=true)
注释单个方法是可行的。getCurrentSession().saveOrUpdate(myEntityInstance)
不是在
只读方法中调用的。如果在
只读
方法中调用
saveOrUpdate()
,则不会引发任何
异常
<tx:method name="*" read-only="true" />