Unable to manage declarative transactions for spring framework

Asked 2 years ago, Updated 2 years ago, 130 views


Annotated using Spring Framework and MyBatis Attempting to perform declarative transaction management does not roll back.

I think there is something wrong with the setting, but I can't consider what's wrong.
I would appreciate it if you could let me know if there are any deficiencies.

I modified the code and tried programmed transaction management, and it was possible.

[pom.xml]

<!-- Abbreviated-->
    <properties>
        <java-version>1.8</java-version>
        <org.springframework-version>4.0.3.RELEASE</org.springframework-version>
        <org.aspectj-version>1.6.10</org.aspectj-version>
        <org.slf4j-version>1.6.6</org.slf4j-version>
    </properties>
<!-- Abbreviated-->
        <!--MyBatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version> 3.2.8</version>
        </dependency>
         <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis-spring</artifactId>
          <version>1.2.2</version>
        </dependency>
<!-- Abbreviated-->

[root-context.xml]

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

    <!--Root Context:defines shared resources visible to all other web components-->
    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
        <property name="driverClassName" value="org.postgresql.Driver"/>
        <property name="url" value="jdbc:postgresql://****.com:5432/****"/>
        <property name="username" value="****"/>
        <property name="password" value="****"/>
    </bean>

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


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

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource"ref="dataSource"/>
        <property name="mapperLocations" value="classpath*:com/hatak/photomap/domain/mapper/*.xml"/>
        <property name="typeAliasesPackage" value="com.hatak.photomap.domain.entity"/>
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.hatak.photomap.domain.mapper"/>
    </bean>
</beans>

[Service class]

@Service
public class HomeServiceImplementationsHomeService{

    @Autowired
    private MessagesMapper messagesMapper;

    @Transactional(readOnly=false, rollbackFor=RuntimeException.class)
    public void execute(HomeServiceDto homeServiceDto)throwsException{

        // DB Update
        MessagesEntity = new MessagesEntity();
        entity.setId(13);
        entity.setMessage(homeServiceDto.getMessage());
        int count = messagesMapper.update(entity);

        // forced rollback
        if(count==1){
            through new RuntimeException("No Rollback";
        }
    }
}

java spring

2022-09-30 17:28

1 Answers

Thank you for your hard work.

Perhaps AutoCommit was enabled.It appears that you are using Commons-DBCP for DataSource retrieval, so please add the defaultAutoCommit="false" property.

Spring's declarative transaction definition seems fine.

That's all.


2022-09-30 17:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.