首页 文章

Spring事务隔离不起作用

提问于
浏览
1

我正在做一个非常简单的例子来测试Spring隔离 . 我有2个非常相似的Schedule类:

@Service("manager1")
public class Manager1 {

    private Service1 service1;

    @Scheduled(fixedDelay = 15000)
    public void sendScheduledCampaigns() {
        service1.changeCredits();
    }
...

@Service("manager2")
public class Manager2 {

    private Service2 service2;

    @Scheduled(fixedDelay = 15000)
    public void sendScheduledCampaigns() {
        service2.changeCredits();
    }
...

另外,我有2个非常相似的服务类:

@Service("service1")
@Transactional
public class Service1 {

    private UserService userService;

    private static Logger log = Logger.getLogger(Service1.class);

    public void changeCredits() {       
        User user = userService.getUserById(1);

        log.info("Service1 previous credits: " + user.getCredits());
        int newCredit = user.getCredits() + 5;
        user.setCredits(newCredit);

        log.info("Service1 new credits: " + user.getCredits());
    }
...

@Service("service2")
@Transactional
public class Service2 {

    private UserService userService;

    private static Logger log = Logger.getLogger(Service2.class);

    public void changeCredits() {

         User user = userService.getUserById(1);

        log.info("Service2 previous credits: " + user.getCredits());
        int newCredit = user.getCredits() + 5;
        user.setCredits(newCredit);

        log.info("Service2 new credits: " + user.getCredits());
    }

运行此代码后,我可以看到,当两个事务同时执行时,两个事务都从数据库获得相同的值 . 所以,他们忽略了对另一个人的修改 . 我想这是事务隔离中的错误:

INFO  27 Feb 2014 14:14:22 - Service1 previous credits: 0
INFO  27 Feb 2014 14:14:22 - Service2 previous credits: 0
INFO  27 Feb 2014 14:14:22 - Service1 new credits: 5
INFO  27 Feb 2014 14:14:22 - Service2 new credits: 5

Here the credits value should be 10 .

在此之后,由于两个进程没有同时执行,计算开始正常:

Feb 27, 2014 2:14:23 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Feb 27, 2014 2:14:23 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Feb 27, 2014 2:14:23 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 10698 ms
INFO  27 Feb 2014 14:14:37 - Service2 previous credits: 5
INFO  27 Feb 2014 14:14:37 - Service2 new credits: 10
INFO  27 Feb 2014 14:14:38 - Service1 previous credits: 10
INFO  27 Feb 2014 14:14:38 - Service1 new credits: 15

我正在使用MYSQL,这些是我的任务和事务配置:

<task:annotation-driven executor="myExecutor" scheduler="myScheduler"/>
<task:executor id="myExecutor" pool-size="100"/>
<task:scheduler id="myScheduler" pool-size="100"/>

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

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="myDataSource" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
            <prop key="hibernate.show_sql">false</prop>
        </props>
    </property>
    <property name="packagesToScan" value="test.domain" />
</bean>

由于某种原因,我的默认隔离级别是 null . 我从中得到了它:

log.info("isolation: " +TransactionSynchronizationManager.getCurrentTransactionIsolationLevel());

所以,我试图改变两种服务的隔离级别:

@Service("service1")
@Transactional(isolation=Isolation.REPEATABLE_READ)
public class Service1 {

@Service("service2")
@Transactional(isolation=Isolation.REPEATABLE_READ)
public class Service2 {

结果仍然是错误的 .

你知道为什么会这样吗?

提前致谢 .

1 回答

相关问题