Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The logic regarding transaction rollback seems incorrect #837

Open
xutengx opened this issue Jul 7, 2023 · 3 comments
Open

The logic regarding transaction rollback seems incorrect #837

xutengx opened this issue Jul 7, 2023 · 3 comments

Comments

@xutengx
Copy link

xutengx commented Jul 7, 2023

src/main/java/org/mybatis/spring/transaction/SpringManagedTransaction.java

/**
   * {@inheritDoc}
   */
  @Override
  public void rollback() throws SQLException {
    if (this.connection != null && !this.isConnectionTransactional && !this.autoCommit) {
      LOGGER.debug(() -> "Rolling back JDBC Connection [" + this.connection + "]");
      this.connection.rollback();
    }
  }
this.connection != null && !this.isConnectionTransactional && !this.autoCommit
@harawata
Copy link
Member

Hello @xutengx ,

We need the details about the problem caused by the referenced code.

@xutengx
Copy link
Author

xutengx commented Jul 19, 2023

Hi,
My service code :


@Service
public class TService {
      @Resource
      private StudentMapper studentMapper;
      
      @Transactional(rollbackFor = TestException.class)
      public void doSomething(int id, int age) throws TestException {
          studentMapper.updateAgeById(id, age);
          throw new TestException();
      }
}

My mapper code :

@Mapper
public interface StudentMapper {
    @Update("update student set age = #{age} where id = #{id}")
    int updateAgeById(@Param("id") Integer id, @Param("age") Integer age);
}

My business code :

@Resource
private TService tService;

@Test
void test() {
    try {
        tService.doSomething(id, newAge);
    } catch (TestException e) {
        System.out.println("exception");
    }
}

In SpringManagedTransaction

 @Override
  public void rollback() throws SQLException {
    // this.connection != null                                 true
    // !this.isConnectionTransactional                   false
    // !this.autoCommit                                         true
    if (this.connection != null && !this.isConnectionTransactional && !this.autoCommit) {
      //  will not execute
      LOGGER.debug(() -> "Rolling back JDBC Connection [" + this.connection + "]");
      this.connection.rollback();
    }
  }

  @Override
  public void close() throws SQLException {
    // execute this logic
    DataSourceUtils.releaseConnection(this.connection, this.dataSource);
  }

The problem is that when only one transaction manager is used and the connection is closed without rollback, there is no problem.
However, when there are multiple transaction managers, closing the connection without rollback can lead to errors.

@harawata
Copy link
Member

harawata commented Sep 20, 2023

@xutengx ,

Please post the stack trace of the error.
It seems that the issue requires complex configuration for reproduction, so please create a repro project with minimum setup and share it on your repo.
Here are some project templates : https://github.com/harawata/mybatis-issues

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants