Spring 隔离级别读取\未提交不工作

Spring 隔离级别读取\未提交不工作,spring,spring-aop,spring-transactions,Spring,Spring Aop,Spring Transactions,我试图理解Spring事务的隔离级别。以下是我正在使用的示例: @Component public class BookShop { private Object lockObj = new Object(); @Autowired private BookDao bookDao; @Transactional public void increaseStock(int isbn, int increment){ String thr

我试图理解Spring事务的隔离级别。以下是我正在使用的示例:

@Component
public class BookShop {

    private Object lockObj = new Object();

    @Autowired
    private BookDao bookDao;

    @Transactional
    public void increaseStock(int isbn, int increment){
        String threadName = Thread.currentThread().getName();
        synchronized (lockObj) {
            Book book = bookDao.findByIsbn(isbn);
            System.out.println(threadName+" about to increment the stock");
            bookDao.updateStock(book, 5);
        }
        System.out.println(threadName+" has increased the stock ");
        sleep(threadName);
        System.out.println(threadName+" rolled back stock");
        throw new RuntimeException("Stock increased by mistake");
    }

    @Transactional
    public int checkStock(int isbn){
        int stock = 0;
        String threadName = Thread.currentThread().getName();
        synchronized (lockObj) {
            System.out.println(threadName+" checking for the stock");
            stock = bookDao.getStock(isbn);
        }
        System.out.println(threadName+": Stock is: "+stock);
        sleep(threadName);
        return stock;
    }

    private void sleep(String threadName){
        System.out.println(threadName+" sleeping");
        try{
            Thread.sleep(1000);
        }catch (InterruptedException e) {
        }
        System.out.println(threadName+" wakeup!");
    }

}
这就是我调用上述内容的方式:

public void testIsolationLevels(final BookShop bookShop, final int isbn){
        Thread t1 = new Thread(){
            public void run() {
                bookShop.increaseStock(isbn, 5);
            };
        };
        t1.setPriority(Thread.MAX_PRIORITY);
        t1.start();

        Thread t2 = new Thread(){
            public void run() {
                bookShop.checkStock(isbn);
            };
        };
        t2.setPriority(Thread.MIN_PRIORITY);
        t2.start();
    }
下面是事务配置:

<tx:advice id="bookTxn" transaction-manager="transactionManager">
        <tx:attributes> 
            <tx:method name="updateStock" isolation="READ_UNCOMMITTED"/>
            <tx:method name="getStock" isolation="READ_UNCOMMITTED"/>
        </tx:attributes>
    </tx:advice>

    <aop:config>
        <aop:pointcut expression="execution(* com.pramati.springIdol.dao.BookDao+.*(..))" id="bookPC"/>
        <aop:advisor advice-ref="bookTxn"  pointcut-ref="bookPC"/>
    </aop:config>
我认为线程3将能够读取更新的值。但事实并非如此。有人能告诉我哪里出了问题吗


提前谢谢

这里的问题是我从另一个事务方法(BookShop的方法)调用DAO的事务方法

由于我没有为BookShop的方法指定隔离级别,默认的隔离级别是isolation.READ\u COMMITTED

还要注意,我没有为Dao的方法指定传播行为,默认行为是propagation.REQUIRED。因此,dao方法使用与BookShop相同的事务。因此,隔离isolation.READ_COMMITTED将应用于事务

解决方案是:将BookShop方法的隔离级别指定为READ_UNCOMMITTED

Thread-2 about to increment the stock// Initially it is 99
Thread-2 has increased the stock // Now it is 104(99+5)
Thread-2 sleeping
Thread-3 checking for the stock
Thread-3: Stock is: 99//Why is it not 104 even if isolation level is read_uncommitted??
Thread-3 sleeping
Thread-2 wakeup!
Thread-2 rolled back stock
Thread-3 wakeup!
Exception in thread "Thread-2" java.lang.RuntimeException: Stock increased by mistake