Java中的回滚注释

时间:2017-09-15 18:40:25

标签: java spring transactions

我的应用程序中有服务调用,可以对其他服务和数据库调用进行远程网络调用。 Spring Boot很好地支持使用factory( global );回滚不良事务,但我想知道是否可以使用注释定义自定义回滚过程。

我需要回滚其他服务以及数据库上的数据。

在代码中,我可以这样做:

@Transactional

但是我希望我能做到这样的事情:

@Transactional
public void doSomethingComplicated() {
    try {
        srvcOne.makeRemoteNetworkCall();
        srvcTwo.makeDatabaseCall();
    } catch(Exception e) {
       srvcOne.rollBackNetworkCall();
    }
}

并在处理程序中:

@Transactional
@MyCustomRollbackListener(handler = MyCustomRollBackHandler.class)
public void doSomethingComplicated() {
    srvcOne.makeRemoteNetworkCall();
    srvcTwo.makeDatabaseCall();
}

我实现了一个全局异常监听器,我能够获得异常来自的类,但我无法获取该方法并检索其上的任何注释。这是我最初的尝试:

public class MyCustomRollBackHandler {

    public void handleRollback() {
        srvcOne.rollBackNetworkCall();
    }

}

无论如何都要做这样的事情?

1 个答案:

答案 0 :(得分:1)

参数不是Stacktrace的一部分。如果该方法是唯一的,即没有重载,您可以使用java.lang.SecurityException: at android.os.Parcel.readException (Parcel.java:1683) at android.os.Parcel.readException (Parcel.java:1636) at android.app.ActivityManagerProxy.startActivity (ActivityManagerNative.java:3159) at android.app.Instrumentation.execStartActivity (Instrumentation.java:1584) at android.app.Activity.startActivityForResult (Activity.java:4228) at android.support.v4.app.k.startActivityForResult (SourceFile:50) at android.support.v4.app.p.startActivityForResult (SourceFile:79) at android.app.Activity.startActivityForResult (Activity.java:4187) at android.support.v4.app.p.startActivityForResult (SourceFile:859) at android.app.Activity.startActivity (Activity.java:4515) at android.app.Activity.startActivity (Activity.java:4483) at android.text.style.URLSpan.onClick (URLSpan.java:75) at android.text.method.LinkMovementMethod.onTouchEvent (LinkMovementMethod.java:217) at android.widget.TextView.onTouchEvent (TextView.java:8524) at android.view.View.dispatchTouchEvent (View.java:9942) at android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2672) at android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2358) at android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2672) at android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2358) at android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2672) at android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2358) at android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2672) at android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2358) at android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2672) at android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2358) at android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2672) at android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2358) at android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2672) at android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2358) at android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2672) at android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2358) at com.android.internal.policy.DecorView.superDispatchTouchEvent (DecorView.java:411) at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent (PhoneWindow.java:1810) at android.app.Activity.dispatchTouchEvent (Activity.java:3065) at android.support.v7.view.i.dispatchTouchEvent (SourceFile:71) at android.support.v7.view.i.dispatchTouchEvent (SourceFile:71) at com.android.internal.policy.DecorView.dispatchTouchEvent (DecorView.java:373) at android.view.View.dispatchPointerEvent (View.java:10162) at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent (ViewRootImpl.java:4446) at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess (ViewRootImpl.java:4314) at android.view.ViewRootImpl$InputStage.deliver (ViewRootImpl.java:3861) at android.view.ViewRootImpl$InputStage.onDeliverToNext (ViewRootImpl.java:3914) at android.view.ViewRootImpl$InputStage.forward (ViewRootImpl.java:3880) at android.view.ViewRootImpl$AsyncInputStage.forward (ViewRootImpl.java:4007) at android.view.ViewRootImpl$InputStage.apply (ViewRootImpl.java:3888) at android.view.ViewRootImpl$AsyncInputStage.apply (ViewRootImpl.java:4064) at android.view.ViewRootImpl$InputStage.deliver (ViewRootImpl.java:3861) at android.view.ViewRootImpl$InputStage.onDeliverToNext (ViewRootImpl.java:3914) at android.view.ViewRootImpl$InputStage.forward (ViewRootImpl.java:3880) at android.view.ViewRootImpl$InputStage.apply (ViewRootImpl.java:3888) at android.view.ViewRootImpl$InputStage.deliver (ViewRootImpl.java:3861) at android.view.ViewRootImpl.deliverInputEvent (ViewRootImpl.java:6257) at android.view.ViewRootImpl.doProcessInputEvents (ViewRootImpl.java:6196) at android.view.ViewRootImpl.enqueueInputEvent (ViewRootImpl.java:6157) at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent (ViewRootImpl.java:6360) at android.view.InputEventReceiver.dispatchInputEvent (InputEventReceiver.java:192) at android.os.MessageQueue.nativePollOnce (Native Method) at android.os.MessageQueue.next (MessageQueue.java:323) at android.os.Looper.loop (Looper.java:136) at android.app.ActivityThread.main (ActivityThread.java:6123) at java.lang.reflect.Method.invoke (Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:867) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:757) 找到它?想到的其他东西,也许你可以看看Aspects在执行之前将方法包装在某个处理程序中。可以在编译时或运行时完成。

方面可以自己进行回滚,它可以使用您需要的信息来丰富异常,或者可以在重新抛出异常之前使用方法中定义的处理程序类设置一些getMethods()变量。然后,您可以在捕获异常的位置从ThreadLocal获取此值。