2

我正在制作一个非常简单的示例来测试 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

这里的 credits 值应该是 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 {

结果仍然是错误的。

你知道为什么会这样吗?

提前致谢。

4

1 回答 1

1

在这种情况下,@Transaction 使用默认隔离,这取决于您使用的数据库,尝试使用隔离属性和 Isolation.REPEATABLE_READ 来获得所需的行为。

Enum(Isolation) 表示与 Transactional 注释一起使用的事务隔离级别的枚举,对应于 TransactionDefinition 接口。

http://docs.spring.io/spring/docs/3.2.x/javadoc-api/org/springframework/transaction/annotation/Isolation.html#DEFAULT

当允许事务从已被另一个正在运行的事务修改但尚未提交的行中读取数据时,就会发生脏读。

于 2014-02-09T04:21:22.683 回答