Откат не работает при весенней загрузке с использованием аннотации @Transactional - PullRequest
0 голосов
/ 20 февраля 2020

Я хочу использовать @Transactional при весенней загрузке, но после нескольких попыток я не могу заставить транзакцию работать, несмотря на наличие исключения внутри для отката, поэтому я что-то упускаю?

, если произошло исключение с WebSocketHandler.sendNotificationToVenue в serviceimpl я хочу откат с двумя операторами вставки; (

Вот ServiceImpl

@Service
@Slf4j
public class OrderInfoServiceImpl implements OrderInfoService {

    @Resource
    private OrderInfoMapper orderInfoMapper;
    @Resource
    private OrderDetailsMapper orderdetailsMapper;
    @Resource
    private WebSocketHandler webSockethHandler;


    private OrderDetailsVO od = new OrderDetailsVO();

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Map<String, String> insertOrderInfo(OrderInfoVO order) throws Exception {
        Map<String, String> rMap = new HashMap<>();
        rMap.put("result", "false");

        int oiSum = 0;
        try {
            for (int i = 0; i < order.getOdList().size(); i++) {

                if (order.getOdList().get(i) != null) {
                    int price = order.getOdList().get(i).getMiPrice();
                    int qty = order.getOdList().get(i).getOdQuantity();
                    oiSum += price * qty;
                }
            }
            order.setOiSum(oiSum);

            String oiMsg = "";
            for (int i = 0; i < order.getOiMessage().size(); i++) {
                oiMsg += order.getOiMessage().get(i).get(0) + "/ ";
            }
            oiMsg = oiMsg.substring(0, oiMsg.lastIndexOf("/"));
            order.setOiMsg(oiMsg);
            orderInfoMapper.insertOrderInfo(order);
            for (int i = 0; i < order.getOdList().size(); i++) {
                if (order.getOdList().get(i) != null) {
                    od = order.getOdList().get(i);
                    od.setOiNum(order.getOiNum());
                    orderdetailsMapper.insertOrderDetails(od);
                }
            }
            webSockethHandler.sendNotificatonToVenue(order.getViNum(), order.getOiNum());
            rMap.put("result", "true");
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
        return rMap;
    }

, и это класс транзакции


@Configuration
@EnableAspectJAutoProxy
@EnableTransactionManagement
@Slf4j
public class TransactionAOP {

    @Resource
    private DataSourceTransactionManager dstm;

    @Bean
    @ConfigurationProperties(prefix="spring.datasource.hikari")
    public DataSource getDS() {
        return DataSourceBuilder.create().build();
    }
    @Bean
    public DataSourceTransactionManager txManager() {
        return new DataSourceTransactionManager(getDS());
    }

    @Bean
    public TransactionInterceptor txInterceptor() {
        log.debug("transaction starts...");
        TransactionInterceptor txInterceptor = new TransactionInterceptor();
        Properties prop = new Properties();
        List<RollbackRuleAttribute> rollbackRules = new ArrayList<>();
        rollbackRules.add(new RollbackRuleAttribute(Exception.class));

        DefaultTransactionAttribute readOnly = new DefaultTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED);
        readOnly.setReadOnly(true);
        readOnly.setTimeout(30);
        RuleBasedTransactionAttribute update = new RuleBasedTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED,rollbackRules);
        update.setTimeout(30);
        prop.setProperty("select*", readOnly.toString());
        prop.setProperty("get*", readOnly.toString());
        prop.setProperty("find*", readOnly.toString());
        prop.setProperty("search*", readOnly.toString());
        prop.setProperty("count*", readOnly.toString());
        prop.setProperty("*", update.toString());
        txInterceptor.setTransactionAttributes(prop);
        txInterceptor.setTransactionManager(dstm);
        return txInterceptor;
    }

    @Bean
    public Advisor txAdvisor() {
        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        pointcut.setExpression("execution(* com.grabit.bdi.service..*ServiceImpl.*(..))");
        return new DefaultPointcutAdvisor(pointcut, txInterceptor());
    }
}

спасибо на ваше время заранее!

...