问题
I have added a unique constraint on a column in my state for business reasons.
When I tried to make multiple calls to my REST API that would break this constraint the flow went to the Hospital and the API never returned.
[INFO ] 2020-11-13T03:14:02,254Z [Node thread-1] statemachine.StaffedFlowHospital. - Flow [13e5039f-0af6-4225-9d93-964897e406a9] admitted to hospital in state StateMachineState
net.corda.node.services.statemachine.StateTransitionException: org.hibernate.exception.ConstraintViolationException: could not execute statement
Caused by: org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: Unique index or primary key violation:
[INFO ] 2020-11-13T03:14:02,575Z [Node thread-1] statemachine.StaffedFlowHospital. - Flow error kept for overnight observation by [net.corda.node.services.statemachine.StaffedFlowHospital$TransitionErrorGeneralPractitioner@73c67012]
[WARN ] 2020-11-13T03:14:09,208Z [FlowHospitalJobTimer] statemachine.StaffedFlowHospital. - There are 1 flows kept for overnight observation. Affected flow ids: 13e5039f-0af6-4225-9d93-964897e406a9
As a side note: that last WARN message is now printed to the log every minute!
My API call looks like this:
FlowHandle<SignedTransaction> flowHandle = proxy.startFlowDynamic(TestFlow.class,
request.getId(),
request.getMake(),
request.getModel()
);
try {
SignedTransaction signedTransaction = flowHandle.getReturnValue().toCompletableFuture().get();
Since the flow goes to the hospital it never completes and also never errors.
How am I supposed to handle this?
Also, if the node never returns anything at all to me, how am I supposed to know that the flow went to the hospital and what the error was that caused it to go to the hospital?
回答1:
- Starting Corda 4.6 you can assign a unique ID to your flow, allowing you to re-attach your client to that flow (even after it completed) in case the connection breaks at some point for any reason. Read about it here.
- I think you need to create a global exception handler for your REST APIs, and add a method to handle
ConstraintViolationException
; it will catch that exception and do something with it (it's up to you, you can return a certain HTTP error code and a specific message). Read about it here.
来源:https://stackoverflow.com/questions/64815369/how-do-i-handle-hospital-exceptions-from-my-api