1

I'm trying to add a specific piece of SQL to all SQL executed in a system using AspectJ.

I've not used AspectJ before but I believe what I need to do is create a pointcut on

call(PreparedStatement Connection.prepareStatement(String))

and provide the advice

before(Connection con, String sql): call(PreparedStatement Connection.prepareStatement(String)) && target(con) && args(sql) { sql = "exec myStordProc();" + sql; }

after which the Connection.prepareStatement() method will continue with the altered String?

Or should I be intercepting calls to prepareStatement and executeQuery and creating a piece of advice that changes this to addBatch() adding my stored procdure call as the first batch sql then the original sql finally executing with executeBatch()?

Any pointers would be much appreciated.

Thanks

Joe T
  • 141
  • 2
  • 12
  • Joe, I reckon that it's possible to do this, but it looks very scary. I strongly suggest you to try to find another solution. Ask yourself *why?* and *how else...?* – Augusto Mar 09 '11 at 10:51
  • @Augusto It does look scary, the other option I consider was creating a Wrapper/class implementing java.sql.Connection and refactoring instances of java.sql.Connection to the wrapper but I was a bit worried about refactoring that many occurrences. – Joe T Mar 09 '11 at 11:34

1 Answers1

1

If you want to modify a parameters value you would need to use around advice so that you can make the call with your modified value:

around(...): ... {
    proceed("exec myStordProc();" + sql);
}
Jonathan
  • 3,203
  • 2
  • 23
  • 25
  • thanks I'm going to give it a go using this: `public aspect AuditAspect { pointcut prepareStatement(String sql): call(PreparedStatement Connection.prepareStatement(..)) && args(sql); Object around(String sql): prepareStatement(sql) { return proceed("exec dbo.AuditDatabase_SetSessionInfo" + sql); } }` – Joe T Mar 09 '11 at 11:35