spring boot,spring data jpa同一事务内修改不可见

目前我遇到这么个问题,在同一事务内,先saveAndFlush()了一条数据,在同一方法内,事务最终提交前,然后立刻查询此数据,但是并查不到。但是据我多年经验,同一事务内,尽管没提交,但是其修改在同一的当前的事务内应该是可见的才对呀。

困惑很久了,什么各种事务隔离性都试了,况且所谓事务隔离性,都是指不同事务间,我这是同一事务内都不可见,求求大侠们帮忙看看, 实在找不到解决思路了。谢谢大家,哎,没有C币,只能乞求大家了。

详细的代码和过程,我已经发在stackoverflow了,链接如下:
https://stackoverflow.com/questions/53449779/the-modifying-can-not-be-visible-in-the-same-transaction-in-spring-data-jpa-why/53455542#53455542

我解决了, 正是事务配置问题。
经过琢磨研究,我把事务配置修改成如下解决了:


package com.hl.core.conf;

import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.interceptor.TransactionInterceptor;

import java.util.Properties;

/**
* @author zhangweilin
* @date 2018/11/24 21:05 
* @Description:  全局声明式事务(此配置解决事务内修改不可见问题 , 此坑消耗了我整整两天一夜研究琢磨,)
*/
//@Component
@Configuration
public class TxConfig2 {
    @Autowired
    private PlatformTransactionManager transactionManager;

    @Bean(name = "txAdvice")
    public TransactionInterceptor getAdvisor() throws Exception {
        System.out.println("transactionManager = " + transactionManager);
        Properties properties = new Properties();
        properties.setProperty("init*", "PROPAGATION_REQUIRED,-Exception");
        properties.setProperty("add*", "PROPAGATION_REQUIRED,-Exception");
        properties.setProperty("insert*", "PROPAGATION_REQUIRED,-Exception");
        properties.setProperty("create*", "PROPAGATION_REQUIRED,-Exception");
        properties.setProperty("persist*", "PROPAGATION_REQUIRED,-Exception");
        properties.setProperty("update*", "PROPAGATION_REQUIRED,-Exception");
        properties.setProperty("modify*", "PROPAGATION_REQUIRED,-Exception");
        properties.setProperty("merge*", "PROPAGATION_REQUIRED,-Exception");
        properties.setProperty("bind*", "PROPAGATION_REQUIRED,-Exception");
        properties.setProperty("del*", "PROPAGATION_REQUIRED,-Exception");
        properties.setProperty("drop*", "PROPAGATION_REQUIRED,-Exception");
        properties.setProperty("remove*", "PROPAGATION_REQUIRED,-Exception");
        properties.setProperty("reset*", "PROPAGATION_REQUIRED,-Exception");
        properties.setProperty("cancel*", "PROPAGATION_REQUIRED,-Exception");
        properties.setProperty("login*", "PROPAGATION_REQUIRED,-Exception");
        properties.setProperty("save*", "PROPAGATION_REQUIRED,-Exception");
        properties.setProperty("update*", "PROPAGATION_REQUIRED,-Exception");
        properties.setProperty("delete*", "PROPAGATION_REQUIRED,-Exception");
        properties.setProperty("*", "PROPAGATION_REQUIRED,-Exception,readOnly");
        TransactionInterceptor tsi = new TransactionInterceptor(transactionManager, properties);
        return tsi;
    }
    @Bean
    public BeanNameAutoProxyCreator txProxy() {
        BeanNameAutoProxyCreator creator = new BeanNameAutoProxyCreator();
        creator.setInterceptorNames("txAdvice");
        creator.setBeanNames("*Service","*Dao");
        creator.setProxyTargetClass(true);
        return creator;
    }
}

这个坑,踩了我很久,此解决方案,已同步更新到stackoverflow了

同一事物内是可见的,你这个问题可能不是事务导致的,有种可能是你提交的数据库是写数据库,你读的数据库是读库,在你读的时候读库和写库还没有同步,也会出现明明提交了但是还是读不到的问题。你具体在看下吧。

我测试了,这个配置可以做到