Java同步方法未同步

时间:2016-05-06 10:32:10

标签: java multithreading concurrency synchronized transactional

我有JAX-RS,Guice,MyBatis的项目。方法getToken()通过REST端点调用。由于@Transactional(isolation = Isolation.SERIALIZABLE),它被同步以避免异常。但是,synchronized方法不安全,不同的调用可能会同时影响数据并抛出异常:

Cause: org.postgresql.util.PSQLException: ERROR: could not serialize access due to read/write dependencies among transactions

我试图通过映射器对象进行同步,但它也不起作用。唯一有效的解决方案是删除同步和更改/删除隔离级别。如何使方法同步?

@Singleton
@Path("/forgottenPath")
public class RestEndpoint {

    @Inject
    private oneService oneService;

    @POST
    @Path("/someAction")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public SomeResponse makeSomeAction() {
        ...
        oneService.makeSomeAction();
        ...
    }
}

public class OneServiceImpl implements OneService {

    @Inject
    private AnotherService anotherService;

    @Override
    public SomeRespose makeSomeAction() {
        ...
        anotherService.getToken());
        ....
    }
}

@Singleton
public class AnotherServiceImpl implements AnotherService {

    @Override
    @Transactional(isolation = Isolation.SERIALIZABLE)
    public synchronized Token getToken() {
        // modifies and retrieves info from database
    }
}

1 个答案:

答案 0 :(得分:2)

不是synchronized无法正常工作,而是@Transactional的实施方式。

长话短说:Spring不是直接调用事务方法(在你的情况下是getToken()),而是创建代理类,用这样的东西替换所有的事务方法(非常简化):

// Generated proxy class (either via AOP, dynamic proxy or bytecode generation)
@Override
public Token getToken() {
    try {
        transactionManager.startTransaction(params);
        // Only this call is synchronized
        return super.getToken();
    }
    catch (Throwable e) {
        transactionManager.rollback();
        rethrow();
    }
    finally {
        // Not in synchronized method (lock is not held), but changes are not commited yet
        transactionManager.commit();
        transactionManager.closeTransaction();
    }
}

有关详细信息,请参阅this answer

正如您所看到的,首先打开事务,然后才调用原始getToken(),所以实际上,当您尝试获取锁定(输入synchronized方法)时,事务已经创建。此外,当调用者退出时,getToken()方法锁被释放(退出同步方法),但事务尚未提交。所以可能的比赛在这里:

假设第一个线程打开事务,保持锁定,使用数据库执行操作,退出原始方法,释​​放锁定然后暂停一下。然后第二个线程可以执行相同的操作,第一个线程被唤醒并且它们都尝试提交,因此一个事务应该失败。

回答原始问题,为了避免更改隔离级别并允许序列化访问,您需要同步不在您的服务中,而是围绕它。

三种解决方案:

1)使调用者方法同步(在您的情况下为makeSomeAction

2)如果您不希望同步整个方法,请为其创建锁定:

@Override
public SomeRespose makeSomeAction() {
    ...
    // Instance of ReentrantLock
    lock.lock();
    try {
        anotherService.getToken());
    }
    finally {
        lock.unlock();
    }
    ....
}

3)如果要封装同步逻辑,请创建阻塞适配器:

@Singleton
public class AnotherServiceAdapter implements AnotherService {

    @Autowired
    private AnotherServiceImpl originalService;

    @Override // No transactional here => no aop proxy
    public synchronized Token getToken() {
        // Lock is held before transactional code kicks in
        return originalService.getToken();
    }
}
相关问题