Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #35423
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Logging database statement duration in a DRY way |
| Date | 2016-08-19 22:37 -0400 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <np8fot$lea$1@gioia.aioe.org> (permalink) |
| References | <87wpjdhrbm.fsf@Equus.decebal.nl> <87pop4j41i.fsf@Equus.decebal.nl> <np8ejr$kaa$1@gioia.aioe.org> |
On 8/19/2016 10:17 PM, Arne Vajhøj wrote:
> import java.util.Map;
> import java.util.concurrent.ConcurrentHashMap;
>
> import org.aspectj.lang.JoinPoint;
> import org.aspectj.lang.ProceedingJoinPoint;
> import org.aspectj.lang.annotation.Around;
> import org.aspectj.lang.annotation.Aspect;
> import org.aspectj.lang.annotation.Pointcut;
>
> @Aspect
> public class TimerAspect {
> private static Map<String,Long> store = new
> ConcurrentHashMap<String,Long>();
> @Pointcut("call(* java.sql.Connection.prepareCall(*)) &&
> !within(TimerAspect)")
> public void start(ProceedingJoinPoint pjp,
> JoinPoint.EnclosingStaticPart esp) {}
> @Pointcut("call(* java.sql.PreparedStatement.close()) &&
> !within(TimerAspect)")
> public void end(ProceedingJoinPoint pjp,
> JoinPoint.EnclosingStaticPart esp) {}
> @Around(value = "start(pjp, esp)")
> public Object startAround(ProceedingJoinPoint pjp,
> JoinPoint.EnclosingStaticPart esp) throws Throwable {
> String loc = esp.getSignature().toString();
> long t1 = System.nanoTime();
> store.put(loc, t1);
> Object res = pjp.proceed();
> return res;
> }
> @Around(value = "end(pjp, esp)")
> public Object endAround(ProceedingJoinPoint pjp,
> JoinPoint.EnclosingStaticPart esp) throws Throwable {
> Object res = pjp.proceed();
> long t2 = System.nanoTime();
> String loc = esp.getSignature().toString();
> long t1 = store.remove(loc);
> System.out.printf("%s : %d\n", loc, t2 - t1);
> return res;
> }
> }
Make that:
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class TimerAspect {
private static Map<String,Long> store = new
ConcurrentHashMap<String,Long>();
@Pointcut("call(* java.sql.Connection.prepareCall(*)) &&
!within(TimerAspect)")
public void start(ProceedingJoinPoint pjp,
JoinPoint.EnclosingStaticPart esp) {}
@Pointcut("call(* java.sql.PreparedStatement.close()) &&
!within(TimerAspect)")
public void end(ProceedingJoinPoint pjp,
JoinPoint.EnclosingStaticPart esp) {}
@Around(value = "start(pjp, esp)")
public Object startAround(ProceedingJoinPoint pjp,
JoinPoint.EnclosingStaticPart esp) throws Throwable {
long t1 = System.nanoTime();
Object res = pjp.proceed();
store.put(res.toString(), t1);
return res;
}
@Around(value = "end(pjp, esp)")
public Object endAround(ProceedingJoinPoint pjp,
JoinPoint.EnclosingStaticPart esp) throws Throwable {
long t1 = store.remove(pjp.getTarget().toString().toString());
Object res = pjp.proceed();
long t2 = System.nanoTime();
String loc = esp.getSignature().toString();
System.out.printf("%s : %d\n", loc, t2 - t1);
return res;
}
}
It is better at handling multiple PreparedStatements in same method.
Arne
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Logging database statement duration in a DRY way Cecil Westerhof <Cecil@decebal.nl> - 2016-08-19 14:32 +0200
Re: Logging database statement duration in a DRY way Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-08-19 09:05 -0400
Re: Logging database statement duration in a DRY way Cecil Westerhof <Cecil@decebal.nl> - 2016-08-19 20:54 +0200
Re: Logging database statement duration in a DRY way Cecil Westerhof <Cecil@decebal.nl> - 2016-08-19 15:12 +0200
Re: Logging database statement duration in a DRY way Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-08-19 10:01 -0400
Re: Logging database statement duration in a DRY way Arne Vajhøj <arne@vajhoej.dk> - 2016-08-19 22:17 -0400
Re: Logging database statement duration in a DRY way Arne Vajhøj <arne@vajhoej.dk> - 2016-08-19 22:37 -0400
Re: Logging database statement duration in a DRY way Martin Gregorie <martin@address-in-sig.invalid> - 2016-08-19 16:05 +0000
Re: Logging database statement duration in a DRY way Eric Douglas <e.d.programmer@gmail.com> - 2016-08-19 10:06 -0700
Re: Logging database statement duration in a DRY way Steven Simpson <ss@my.domain.invalid> - 2016-08-19 22:34 +0100
csiph-web