Spring jdbc @Transactional批注,尽管抛出适当的异常,过程仍提交事务

时间:2019-06-11 09:41:04

标签: java spring plsql transactions spring-jdbc

Namastey,

We have a class and its methods as below-

    @Service
    @Component
    public class ReqServiceImpl implements ReqService {

    @Autowired
    private SubmitDAO submitDAO;

    @Override
    @Transactional(readOnly = false, rollbackFor = { CustomException.class },propagation=Propagation.REQUIRES_NEW)
    public Map initiateSubmission(DetailsDTO vo) CustomException{

    //it has two method defined within the same class

        if(condition){
           Map resultMap = submitRequest1(map,vo.isTestMode());
        }else{
           Map result = (HashMap) submitRequest2(map,flag,vo.isTestMode());
        }


    }

    @Transactional(readOnly = false, rollbackFor = { CustomException.class }, propagation=Propagation.REQUIRED)
    public void submitRequest1(Map map,boolean testMode) CustomException{

    submitDAO.simpleTableInsert1(map);
    submitDAO.simpleTableInsert2(map);
    submitDAO.procedureCall(map);

    if(!"success".equalIgnorecase(map.get("output"))){
    throw new CustomException("Service Layer | submitRequest1 | Proc Not successs");
    }

    }

    @Transactional(readOnly = false, rollbackFor = { CustomException.class }, 
    propagation=Propagation.REQUIRED)
    public void submitRequest2(Map map,boolean testMode) throws CustomException{
    submitDAO.simpleTableInsert1(map);
    submitDAO.simpleTableInsert2(map);
    submitDAO.procedureCall(map);
    }




    }

下面是我们的spring-servlet.xml文件 在此,我们将注释,代理和默认建议的配置添加到代理。

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

    xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.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">
    <context:annotation-config />
    <context:component-scan base-package="com.company,com.company.security.filter,org.springframework.jdbc" />

    <mvc:annotation-driven>

        <mvc:message-converters register-defaults="true">
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <constructor-arg index="0" name="defaultCharset"
                    value="UTF-8" />
            </bean>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
        </mvc:message-converters>

    </mvc:annotation-driven>

    <aop:aspectj-autoproxy proxy-target-class="true"/>

    <tx:annotation-driven transaction-manager="transactionManager" />

    <bean
        class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator">
        <property name="proxyTargetClass" value="true" />
    </bean>


     <bean id="LogAspect" class="com.company.common.aop.LogAspect">
    </bean>
    <bean id="PerfAspect" class="com.company.common.aop.PerfAspect">
    </bean>
    <bean id="SessionAspect" class="com.company.common.aop.SessionAspect">
    </bean>


    <bean id="mailProperties"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations" >
         <list>
            <value>classpath:db.properties</value>
            <value>classpath:dSource.properties</value>
        </list>
        </property>
        <property name="ignoreUnresolvablePlaceholders" value="true" />
    </bean> 



    <bean id="myDataSource" class="org.springframework.jndi.JndiObjectFactoryBean" primary="true">
        <property name="jndiName" value="${DEVJNDI}"></property>  
    </bean>

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


    <bean id="DataSourceCo" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="${JNDINAMECOM}"></property>
    </bean>

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


     <bean id="myDataSourceI" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="${JNDINAMEI}"></property>
    </bean>

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

    <!-- Create a proxy to generate session-scope -->
     <bean id="userBean"
        class="com.company.common.session.UserDetailsSessionBean"
        scope="session">
        <aop:scoped-proxy />

    </bean> 

    <bean id="multipartResolver"

    class="org.springframework.web.multipart.commons.CommonsMultipartResolver" 
    />`enter code here`


    </beans>

另一个类中的方法“ procedureCall”内部有一个过程调用。 该过程内部没有任何提交。 之后,该过程返回任何错误,我们抛出一个异常,但是所有都是徒劳的。通过过程完成的插入仍会提交,事务不会回滚。

如果有任何错误,则proc给出输出“错误”或“成功”。 如果我们收到任何错误“错误”,那么我们将抛出定制的异常。 它应该回滚proc内部完成的所有操作,但是不会发生。

请提出建议。

0 个答案:

没有答案