-
Notifications
You must be signed in to change notification settings - Fork 8.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature] add transaction hook #525
Conversation
Codecov Report
@@ Coverage Diff @@
## develop #525 +/- ##
=============================================
+ Coverage 31.89% 32.28% +0.38%
- Complexity 775 798 +23
=============================================
Files 200 202 +2
Lines 7948 8032 +84
Branches 939 941 +2
=============================================
+ Hits 2535 2593 +58
- Misses 5155 5178 +23
- Partials 258 261 +3
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if hooks are really needed by users. Can you share some ideas about hook use cases?
And also, the hook mechanism here is doing something just like FailureHandler. I think we can replace FailureHandler with TransactionHook after some enhancement on this.
} | ||
// 4. everything is fine, commit. | ||
try { | ||
triggerBeforeCommit(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exception should be properly handled. I don't think users would like to fail committing by their hook.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed it in next commit
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the hook help us to add more function in the transaction life cycle
like
1.trace the transaction execute status
2. customer logger to help they find the bad transaction
3.some case we need to do something when the commit all success
* @author guoyao | ||
* @date 2019/3/4 | ||
*/ | ||
public interface TransactionHook { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As a transaction hook, why not also including before/after rollback?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good idea .i will add it
|
||
} | ||
|
||
try { | ||
triggerAfterCommit(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
merge after tx.commit()?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good idea ,done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I left some comments about code style.
import com.alibaba.fescar.tm.api.transaction.TransactionHookManager; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove this line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
already format
*/ | ||
package com.alibaba.fescar.tm.api.transaction; | ||
|
||
import java.util.*; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't use *
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
copy that
if (transactionHook == null) { | ||
throw new NullPointerException("transactionHook must not be null"); | ||
} | ||
List<TransactionHook> transactionHooks=hooksLocal.get(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the spaces around =
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
*/ | ||
public class TransactionTemplateTest { | ||
|
||
private static final String DEFAULT_XID="123456789"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
@@ -26,7 +26,7 @@ | |||
*/ | |||
public final class TransactionHookManager { | |||
|
|||
private static final ThreadLocal<List<TransactionHook>> hooksLocal=new ThreadLocal<>(); | |||
private static final ThreadLocal<List<TransactionHook>> hooksLocal = new ThreadLocal<>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's call a static final member as uppercase:
private static final ThreadLocal<List<TransactionHook>> LOCAL_HOOKS = new ThreadLocal<>();
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not use spi to load the transaction hook instead of a thread local holder?
tm/src/main/java/com/alibaba/fescar/tm/api/transaction/TransactionHookAdapter.java
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, LGTM!
Ⅰ. Describe what this PR did
Feature add transaction hook
Ⅱ. Does this pull request fix one issue?
add transaction hook in the Transaction Life Cycle
Ⅲ. Why don't you add test cases (unit test/integration test)?
add test in TransactionHookManagerTest
add test in TransactionTemplateTest
Ⅳ. Describe how to verify it
Ⅴ. Special notes for reviews