Other attributes of the transaction spring (isolation level, rollback, read-only, expired) (iv)

Connect one.

1. Use propagation specified transaction propagation behavior, ie the current transaction method is called A separate transaction method on how to use the transaction, the default value is REQUIRED, that the use of the method call transaction REQUIRES_NEW: Transaction their own affairs, calls the transaction method the transaction is pending.

2. Use isolation specified transaction isolation level, the most commonly used value of READ_COMMITTED.

Spring declarative transaction for all run-time abnormality 3. By default, the rollback can also be provided by a corresponding attribute default value can normally take.

4. readOnly specified transaction is read-only. Indicates that the transaction data is only read but not update the data, which can help to optimize transaction database engine. If that is a value only read the database mechanism, set readOnly = true.

5. Use the specified timeout period before forcing a rollback transaction can occupy.

    // add transaction notes 
    the @Transactional (propagation = Propagation.REQUIRES_NEW,
            isolation=Isolation.READ_COMMITTED,
            noRollbackFor={UserAccountException.class)
    @Override
    public void purchase(String username, String isbn) {...}

Plus noRollbackFor, not rolled back after encountering UserAccountException specified exceptions, we tested testBookShopService, even if we add the Transactional annotation, but does not roll back the balance is insufficient experience, that in 1001 the inventory database or decreased 1.

    @Transactional(propagation=Propagation.REQUIRES_NEW,
            isolation=Isolation.READ_COMMITTED,
            readOnly=false,
            timeout=3)
    @Override
    public void purchase(String username, String isbn) {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {}
        }

Because we want to update the data in the purchase, so we set readOnly = false. Set timeout specified force a rollback of time. We sleep together in the purchase inside, then sleep 2s <3s.

And data in a database reset:

At this point we test testBookShopService, the result is:

We will change 2000 5000, 5s> 3s, then test testBookShopService, the result is:

Although we can re-buy balance, but force a rollback of time = 3s <time program execution, so to force a rollback.

Guess you like

Origin www.cnblogs.com/xiximayou/p/12168690.html