简体   繁体   English

我们如何在没有正在进行的交易的情况下拨打外部电话?

[英]How can we make an external call without an ongoing transaction?

We're using Spring & Hibernate in JBoss 4.2.3 and are calling an external system, which could take a while, as part of processing requests. 我们在JBoss 4.2.3中使用Spring&Hibernate,并且正在调用外部系统,这可能需要一些时间,作为处理请求的一部分。 To eliminate long-lived database locks we're not writing to our database until after the external call has returned. 为了消除长期存在的数据库锁,直到外部调用返回后,我们才写入数据库。 But Hibernate does not like being without a transaction so we have a read-only transaction, started by Spring, until we're doing our writes (in a new read-write subtransaction). 但是Hibernate不喜欢没有事务,因此我们有一个从Spring开始的只读事务,直到我们进行写操作(在新的读写子事务中)。 Unfortunately this still means we're retaining resources during the external call, the allocated connection from the pool. 不幸的是,这仍然意味着我们在外部调用(池中分配的连接)期间保留了资源。 Is there a way to make the external call without an ongoing transaction when the rest of the execution (before and after) needs one? 当其余的执行(之​​前和之后)需要一个时,是否有一种方法可以使外部调用不进行正在进行的事务? Is there a better way of deferring the actual transaction until we really need it and still keep Hibernate happy? 有没有更好的方法可以将实际交易推迟到我们真正需要它之前,仍然让Hibernate满意?

Is it necessary to call the external system in your persistence layer? 是否有必要在持久层中调用外部系统? It seems more logical to do this kind of thing outside, before you hit persistence. 在您坚持不懈之前,在外面做这种事情似乎更合乎逻辑。

You actually don't need a transaction for reads. 您实际上不需要读取事务。

Here is an example where the read call goes straight to the DAO object, but the write call is done within a transaction (using Spring's TransactionTemplate helper object): 这是一个示例,其中读调用直接到达DAO对象,而写调用在事务内完成(使用Spring的TransactionTemplate帮助器对象):

public ContainerType getContainerType(Long id) {
    return this.containerTypeDao.getContainerType(id);
}

public Long saveContainerType(final ContainerType containerType) {
    return (Long) this.transactionTemplate.execute(new TransactionCallback() {

        public Long doInTransaction(TransactionStatus status) {
            try {
                return containerTypeDao.saveContainerType(containerType);
            }
            catch (Exception e) {
                status.setRollbackOnly();
                return null;
            }
        }
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM