1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
package net.sf.infrared.aspects.jdbc; |
23 |
|
|
24 |
|
import java.sql.Connection; |
25 |
|
import java.sql.PreparedStatement; |
26 |
|
import java.sql.ResultSet; |
27 |
|
|
28 |
|
import net.sf.infrared.agent.MonitorConfig; |
29 |
|
import net.sf.infrared.agent.MonitorFacade; |
30 |
|
import net.sf.infrared.agent.MonitorFactory; |
31 |
|
import net.sf.infrared.agent.StatisticsCollector; |
32 |
|
import net.sf.infrared.aspects.AbstractBaseAspect; |
33 |
|
import net.sf.infrared.aspects.api.ApiContext; |
34 |
|
import net.sf.infrared.aspects.jdbc.p6spy.InfraREDP6Factory; |
35 |
|
import net.sf.infrared.base.model.ExecutionTimer; |
36 |
|
import org.codehaus.aspectwerkz.joinpoint.StaticJoinPoint; |
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
0 |
public class NonWrappingJdbcAspect extends AbstractBaseAspect { |
47 |
0 |
private static SqlContextManager ctxMgr = new SqlContextManager(); |
48 |
|
|
49 |
0 |
private static SqlMemory sqlMem = new SqlMemory(); |
50 |
|
|
51 |
|
public Object aroundSimpleSqlExecution(StaticJoinPoint sjp, String sql) throws Throwable { |
52 |
0 |
MonitorFacade facade = MonitorFactory.getFacade(); |
53 |
0 |
if(! isJDBCMonitoringEnabled(facade) ) { |
54 |
0 |
return sjp.proceed(); |
55 |
|
} |
56 |
0 |
SqlExecuteContext ctx = ctxMgr.getExecuteContext(sql); |
57 |
0 |
return recordExecution(ctx, sjp, facade); |
58 |
|
} |
59 |
|
|
60 |
|
public Object aroundFirstStatementOrCallPreparation(StaticJoinPoint sjp, String sql) throws Throwable { |
61 |
0 |
MonitorFacade facade = MonitorFactory.getFacade(); |
62 |
0 |
if(! isJDBCMonitoringEnabled(facade) ) { |
63 |
0 |
Object ps = sjp.proceed(); |
64 |
0 |
sqlMem.memorizeSql(sql, ps); |
65 |
0 |
return ps; |
66 |
|
} |
67 |
|
|
68 |
0 |
SqlPrepareContext ctx = ctxMgr.getPrepareContext(sql); |
69 |
0 |
ExecutionTimer timer = new ExecutionTimer(ctx); |
70 |
0 |
StatisticsCollector collector = facade.recordExecutionBegin(timer); |
71 |
0 |
Object ps = null; |
72 |
|
try { |
73 |
0 |
ps = sjp.proceed(); |
74 |
0 |
return ps; |
75 |
|
} finally { |
76 |
0 |
facade.recordExecutionEnd(timer, collector); |
77 |
0 |
if (ps != null) { |
78 |
0 |
sqlMem.memorizeSql(sql, ps); |
79 |
|
} |
80 |
|
} |
81 |
|
} |
82 |
|
|
83 |
|
public Object aroundStatementOrCallExecution(StaticJoinPoint sjp, PreparedStatement ps) throws Throwable { |
84 |
0 |
MonitorFacade facade = MonitorFactory.getFacade(); |
85 |
0 |
if(! isJDBCMonitoringEnabled(facade) ) { |
86 |
0 |
return sjp.proceed(); |
87 |
|
} |
88 |
|
|
89 |
0 |
String sql = sqlMem.recollectSql(ps); |
90 |
0 |
if (sql == null) { |
91 |
|
|
92 |
|
|
93 |
0 |
return sjp.proceed(); |
94 |
|
} |
95 |
|
|
96 |
0 |
SqlExecuteContext ctx = ctxMgr.getExecuteContext(sql); |
97 |
0 |
ExecutionTimer timer = new ExecutionTimer(ctx); |
98 |
0 |
StatisticsCollector collector = facade.recordExecutionBegin(timer); |
99 |
|
try { |
100 |
0 |
return sjp.proceed(); |
101 |
|
} finally { |
102 |
0 |
facade.recordExecutionEnd(timer, collector); |
103 |
|
} |
104 |
|
} |
105 |
|
|
106 |
|
public Object aroundCommit(StaticJoinPoint sjp) throws Throwable { |
107 |
0 |
MonitorFacade facade = MonitorFactory.getFacade(); |
108 |
0 |
if(! isJDBCMonitoringEnabled(facade) ) { |
109 |
0 |
return sjp.proceed(); |
110 |
|
} |
111 |
|
|
112 |
0 |
ApiContext ctx = new ApiContext(Connection.class.getName(), "commit", "JDBC"); |
113 |
0 |
return recordExecution(ctx, sjp, facade); |
114 |
|
} |
115 |
|
|
116 |
|
public Object aroundRollback(StaticJoinPoint sjp) throws Throwable { |
117 |
0 |
MonitorFacade facade = MonitorFactory.getFacade(); |
118 |
0 |
if(! isJDBCMonitoringEnabled(facade) ) { |
119 |
0 |
return sjp.proceed(); |
120 |
|
} |
121 |
|
|
122 |
0 |
ApiContext ctx = new ApiContext(Connection.class.getName(), "rollback", "JDBC"); |
123 |
0 |
return recordExecution(ctx, sjp, facade); |
124 |
|
} |
125 |
|
|
126 |
|
public Object aroundIterateOverResultSet(StaticJoinPoint sjp) throws Throwable { |
127 |
0 |
MonitorFacade facade = MonitorFactory.getFacade(); |
128 |
0 |
if(! isJDBCMonitoringEnabled(facade) ) { |
129 |
0 |
return sjp.proceed(); |
130 |
|
} |
131 |
|
|
132 |
0 |
ApiContext apiCtx = new ApiContext(ResultSet.class.getName(), "next", "JDBC"); |
133 |
0 |
return recordExecution(apiCtx, sjp, facade); |
134 |
|
} |
135 |
|
|
136 |
|
boolean isJDBCMonitoringEnabled(MonitorFacade facade) { |
137 |
0 |
MonitorConfig cfg = facade.getConfiguration(); |
138 |
0 |
return cfg.isMonitoringEnabled() |
139 |
|
&& cfg.getProperty(InfraREDP6Factory.KEY_JDBC_MONITORING_ENABLED, true); |
140 |
|
} |
141 |
|
} |