spring-mvc - Spring MVC JDBC 数据源事务管理器 : Data committed even after readonly=true

标签 spring-mvc jdbc transactions autocommit

我目前正在开发一个Spring MVC应用程序。我已经配置了一个JDBC TransactionManager,并且正在使用AOP XML进行声明式事务管理。但是,即使我将该方法配置为在只读= true上运行,它仍然会提交交易。

数据库:Oracle 10g

我的database-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schem...ring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">


    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${driver}" />
        <property name="url" value="${url}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />
        <property name="defaultAutoCommit" value="false" />
    </bean>

    <bean id="txManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="mapperLocations" value="classpath:com/mybatis/mappers/*.xml" />
    </bean>


    <!--
        the transactional advice (what 'happens'; see the <aop:advisor/> bean
        below)
    -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <!-- the transactional semantics... -->
        <tx:attributes>
            <!-- all methods starting with 'get' are read-only -->
            <tx:method name="get*" read-only="true" />
            <!-- other methods use the default transaction settings (see below) -->
            <tx:method name="*" read-only="true" rollback-for="RuntimeException"/>
        </tx:attributes>
    </tx:advice>

    <!--
        ensure that the above transactional advice runs for any execution of
        an operation defined by the FooService interface
    -->
    <aop:config>
        <aop:pointcut id="fooServiceOperation"
            expression="execution(* com.service.EmployeeService.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceOperation" />
    </aop:config>


</beans>

我的 Controller

package com.service;

import java.util.List;

import com.mybatis.dao.EmployeeMapperInterface;
import com.spring.model.Employee;

public class EmployeeService implements EmployeeBaseService{

    EmployeeMapperInterface employeeMapper;

    public EmployeeMapperInterface getEmployeeMapper() {
        return employeeMapper;
    }

    public void setEmployeeMapper(EmployeeMapperInterface employeeMapper) {
        this.employeeMapper = employeeMapper;
    }

    @Override
    public Employee getEmployeeById(long empId){
        //retrieve from database
        List empList = employeeMapper.getEmployeeWithId(empId);
        if(empList != null && empList.size()>0){
            return (Employee) empList.get(0);   
        }
        return null;

    }




      @Override
    public long saveEmployee(Employee employee){
        long empId = 0l;
        if(employee.getEmpId()==0){
            empId  = new Long( employeeMapper.insertEmployee(employee));
        }else{
             employeeMapper.updateEmployee(employee);
             empId  =  employee.getEmpId();
        }
        try {
            System.out.println("gonna sleep");
            Thread.sleep(10);

        } catch (InterruptedException e) {

            e.printStackTrace();
        }
        return empId;
    }

如何防止自动提交?我还注意到,即使我不放置任何事务管理代码,代码仍然会提交。请注意,但是,当我为 RuntimeException 添加 no-rollback-for 然后执行 1/0 时,会调用事务建议,如果我将与 rollback-for 相同的内容添加到其中,它会正确提交数据并回滚。 我还通过将线程置于 sleep 状态来尝试查询超时,即使这不起作用,但我认为超时可能适用于实际查询,所以这很好。 提前致谢!

最佳答案

建议只读只是建议。当某些东西被标记为只读时,底层事务管理系统并不要求阻止写入,这更多的是作为一种优化提示,表明此方法是只读的,因此您不需要需要担心它会改变事情。如果在只读事务中进行更改,一些事务管理器会提示,有些则不会。一般来说,通过 JNDI 获取的数据源不会。无论如何,您都不应该依赖只读建议来防止更改被写回磁盘。

防止更改持久化的选项有:

  • 标记事务仅回滚或抛出具有相同效果的异常

  • 在更改对象之前从事务 session 中分离/逐出该对象

  • 克隆对象并使用克隆

关于spring-mvc - Spring MVC JDBC 数据源事务管理器 : Data committed even after readonly=true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10477039/

相关文章:

java - 如何使用 Spring @MVC 在请求中填充多个 bean

java - Spring Security 与 Spring MVC 不起作用

java - 使用java在jdbc中的每个循环中只插入一行

mysql - 为什么 START TRANSACTION 不会像它应该的那样隐式影响自动提交

java - 事务结束时 JPA 隐式刷新未发生

java - 如何从 Freemarker 模板中的表单输入中获取数据到 spring MVC Controller 中?

java - Spring 的 BCrypt 实现

java - 在 JDBC 中检查列的字符集和排序规则

java - SQL Server JDBC 错误 : The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption

php - 如何处理动态网站本地化?