Home » Java » java – Propagation.NEVER vs No Transaction vs Propagation.Required-Exceptionshub

java – Propagation.NEVER vs No Transaction vs Propagation.Required-Exceptionshub

Posted by: admin February 23, 2020 Leave a comment

Questions:

I have an integration test where I’m trying to understand the difference in behavior for different propagation types (required and never) vs no transaction at all. Here’s my integration test:

@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional(propagation = Propagation.REQUIRED, transactionManager="transactionManager")
public class MyRepositoryTest {

    @Autowired
    private MyRepository myRepo;

    @Test
    public void test() throws SQLException {
        myRepo.getSession("user", "password");
        myRepo.getDataUsingId(2);
    }

}

My repo is:

public interface MyRepository extends Repository<String, String> {

    @Procedure("create_session")
    String getSession(String loginName, String password);

    @Procedure("get_data")
    Blob getDataUsingId(int id);

}

If I use Propagation.REQUIRED as shown in the test code above, the program hangs when calling getDataUsingId. This is because that stored procedure

  1. Inserts a row
  2. Calls an external service
  3. Waits for that external service to insert a row

It hangs b/c the external service reads the insert (which must be committed) from step 1 before it can insert its row.

If I use no transaction (i.e. remove the @Transactional line), I get an error when calling getDataUsingId. The error complains about an invalid TDS stream. (I’m using the MS SQL Server driver.) This is because the number of bytes are greater than 8000.

If I use Propagation.NEVER, it works. But why?

Update

If set PacketSize=0 in the JDBCURL, it also works when in the case that I have no transaction.

How to&Answers: