多线程上下文中的Spring @Transactional可以避免竞争条件

时间:2016-04-19 13:45:20

标签: java spring multithreading hibernate transactions

我正在使用Spring MVC开发Java EE应用程序,Hibernate 4用于持久化。 我有一个服务层,我使用一些方法来更新统计信息,我展示了有问题的方法。

@Transactional
public void addReceived(String instanceId, long received) {
    log.debug("Updating database with received value " + received + " for instance " + instanceId);
    ActionStatus actionStatus = actionStatusJDBCTemplate.getByInstanceId(instanceId);
    if (actionStatus != null) {
        if (actionStatus.getReceived() == null) {
            actionStatus.setReceived(received);
        } else {
            log.debug("actionStatus.getReceived()" + actionStatus.getReceived() + "received " + received);
            actionStatus.setReceived(actionStatus.getReceived() + received);
        }
        actionStatusJDBCTemplate.update(actionStatus);
    } else {
        actionStatus = new ActionStatus();
        actionStatus.setReceived(received);
        actionStatus.setInstanceId(instanceId);
        actionStatusJDBCTemplate.create(actionStatus);
    }
}

actionStatusJDBCTemplate的'update'方法如下:

@Override
public void update(ActionStatus actionStatus) {
    getHibernateTemplate().update(actionStatus);
    getSessionFactory().getCurrentSession().flush();
}

并且actionStatusJDBCTemplate.getByInstanceId方法是:

@Override
public ActionStatus getByInstanceId(String instanceId) {
    List<ActionStatus> result = 
            (List<ActionStatus>) getHibernateTemplate().find("from ActionStatus where instanceId=?", instanceId);
    if(result.size() == 0)
        return null;
    // forces refresh of the object to avoid the cached version
    getSessionFactory().getCurrentSession().refresh(result.get(0));
    return result.get(0);
}

但是当我使用addReceived方法有多个线程时,我有竞争条件,我看到如下日志:

...
Updating database with received value 1 for instance foo
Updating database with received value 1 for instance foo // both threads inside transactional function?
actionStatus.getReceived() 3 received 1
actionStatus.getReceived() 3 received 1 // error
Updating database with received value 1 for instance foo
Updating database with received value 1 for instance foo
actionStatus.getReceived() 4 received 1
actionStatus.getReceived() 4 received 1
...

和我分享我的春天背景

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/mvc
                           http://www.springframework.org/schema/mvc/spring-mvc.xsd
                           http://www.springframework.org/schema/tx
                            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <context:annotation-config />
    <context:component-scan base-package="com.sysdata" />
    <context:property-placeholder
        location="${pathFileProps:}/application.properties" />

    <tx:annotation-driven/>
    <mvc:annotation-driven />
    <!-- Handles HTTP GET requests for /images/** by efficiently serving up 
        static resources in the ${webappRoot}/images directory -->
    <mvc:resources mapping="/images/**" location="/images/" />
    <mvc:resources mapping="/css/**" location="/css/" />

    <!-- Initialization for data source -->
    <bean id="dataSource"
        class="org.apache.commons.dbcp2.BasicDataSource">
        <property name="driverClassName" value="${db.driver}" />
        <property name="url" value="${db.url}" />
        <property name="username" value="${db.user}" />
        <property name="password" value="${db.pass}" />
        <property name="defaultAutoCommit" value="true" /> 
    </bean>

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
        <property name="redirectHttp10Compatible" value="false" />
    </bean>


    <bean id="actionConfigJDBCTemplate" class="com.sysdata.components.dao.ActionConfigJDBCTemplate">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="scheduledUserJDBCTemplate" class="com.sysdata.components.dao.ScheduledUserJDBCTemplate">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="actionStatusJDBCTemplate" class="com.sysdata.components.dao.ActionStatusJDBCTemplate">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="installConfigJDBCTemplate" class="com.sysdata.components.dao.InstallConfigJDBCTemplate">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="contactJDBCTemplate" class="com.sysdata.components.dao.ContactJDBCTemplate">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="${pathFileProps:}/hibernate.cfg.xml" />
    </bean>

</beans>

所以我的问题是:当一个函数标有@Transactional时,是否可以在不将syncronized关键字添加到签名的情况下使其成为原子?

修改 我试图将synchronized添加到方法,但现在问题是在休眠。似乎加载的对象未更新到上一版本。 我刷新了对象

@Override
public ActionStatus getByInstanceId(String instanceId) {
    List<ActionStatus> result = 
        (List<ActionStatus>) getHibernateTemplate().find("from ActionStatus where instanceId=?", instanceId);
    if(result.size() == 0)
        return null;
    // forces refresh of the object to avoid problem in case of concurrent reads
    getSessionFactory().getCurrentSession().refresh(result.get(0));
    return result.get(0);
}

但我仍然有一个过时的对象

0 个答案:

没有答案