33e6d1c3c6e7e4907d31a596302def6c6ac51fec
[mdsal.git] / common / mdsal-common-api / src / main / java / org / opendaylight / mdsal / common / api / TransactionCommitDeadlockException.java
1 /*
2  * Copyright (c) 2014 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.mdsal.common.api;
9
10 import java.util.function.Supplier;
11 import org.opendaylight.yangtools.yang.common.RpcError;
12 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
13 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
14
15 /**
16  * A type of TransactionCommitFailedException that indicates a situation that would result in a
17  * threading deadlock. This can occur if a caller that submits a write transaction tries to perform
18  * a blocking call via one of the <code>get</code> methods on the returned ListenableFuture. Callers
19  * should process the commit result asynchronously (via Futures#addCallback) to ensure deadlock
20  * won't occur.
21  *
22  * @author Thomas Pantelis
23  */
24 public class TransactionCommitDeadlockException extends TransactionCommitFailedException {
25     private static final long serialVersionUID = 1L;
26     private static final String DEADLOCK_MESSAGE =
27             "An attempt to block on a ListenableFuture via a get method from a write "
28                     +
29                     "transaction submit was detected that would result in deadlock. The commit "
30                     +
31                     "result must be obtained asynchronously, e.g. via Futures#addCallback, to avoid deadlock.";
32     private static final RpcError DEADLOCK_RPCERROR =
33             RpcResultBuilder.newError(ErrorType.APPLICATION, "lock-denied", DEADLOCK_MESSAGE);
34
35     public static final Supplier<Exception> DEADLOCK_EXCEPTION_SUPPLIER =
36         () -> new TransactionCommitDeadlockException(DEADLOCK_MESSAGE, DEADLOCK_RPCERROR);
37
38     public TransactionCommitDeadlockException(final String message, final RpcError... errors) {
39         super(message, errors);
40     }
41 }