1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.ken.util;
17
18 import javax.transaction.Status;
19 import javax.transaction.UserTransaction;
20
21 import org.aopalliance.intercept.MethodInterceptor;
22 import org.aopalliance.intercept.MethodInvocation;
23 import org.apache.log4j.Level;
24 import org.apache.log4j.Logger;
25 import org.springframework.transaction.jta.JtaTransactionManager;
26
27
28
29
30
31 public class TracingTransactionInterceptor implements MethodInterceptor {
32 private static final Logger LOG = Logger.getLogger(TracingTransactionInterceptor.class);
33
34 private Level level;
35 private JtaTransactionManager txManager;
36
37
38
39
40 public void setLevel(String lvl) {
41 level = Level.toLevel(lvl);
42 }
43
44
45
46
47 public void setJtaTransactionManager(JtaTransactionManager manager) {
48 this.txManager = manager;
49 }
50
51
52
53
54 public Object invoke(MethodInvocation invocation) throws Throwable {
55 StringBuilder sb = new StringBuilder();
56 UserTransaction tx = txManager.getUserTransaction();
57 if (tx == null) {
58 sb.append("null");
59 } else {
60 sb.append(toString(tx.getStatus()));
61 }
62 LOG.log(level, invocation.getMethod() +" UserTransaction: " + sb);
63 return invocation.proceed();
64 }
65
66
67
68
69
70 private static final String toString(int txStatus) {
71 switch (txStatus) {
72 case Status.STATUS_ACTIVE: return "STATUS_ACTIVE";
73 case Status.STATUS_COMMITTED: return "STATUS_COMMITTED";
74 case Status.STATUS_COMMITTING: return "STATUS_COMMITTING";
75 case Status.STATUS_MARKED_ROLLBACK: return "STATUS_MARKED_ROLLBACK";
76 case Status.STATUS_NO_TRANSACTION: return "STATUS_NO_TRANSACTION";
77 case Status.STATUS_PREPARED: return "STATUS_PREPARED";
78 case Status.STATUS_PREPARING: return "STATUS_PREPARING";
79 case Status.STATUS_ROLLEDBACK: return "STATUS_ROLLEDBACK";
80 case Status.STATUS_ROLLING_BACK: return "STATUS_ROLLING_BACK";
81 case Status.STATUS_UNKNOWN: return "STATUS_UNKNOWN";
82 default: return "unknown status: " + txStatus;
83 }
84 }
85 }