Posts

Showing posts from April, 2019

10 Best ways to write optimized SQL in Java

1. Forgetting about NULL Misunderstanding NULL is probably the biggest mistake a Java developer can make when writing SQL. This is also (but not exclusively) due to the fact that NULL is also called UNKNOWN. If it were only called UNKNOWN, it would be easier to understand. Another reason is that JDBC maps SQL NULL to Java null when fetching data or when binding variables. This may lead to thinking that NULL = NULL (SQL) would behave the same way as null == null (Java) One of the crazier examples of misunderstanding NULL is when  NULL predicates are used with row value expressions . Another, subtle problem appears when misunderstanding the meaning of  NULL in NOT IN anti-joins . The Cure: Train yourself. There’s nothing but explicitly thinking about NULL, every time you write SQL: Is this predicate correct with respect to NULL? Does NULL affect the result of this function? 2. Processing data in Java memory Few Java developers know SQL very well. The occasional JOIN,

Aspect Oriented Programming using Google Guice

Aspect Oriented Programming using Google Guice Guice is compliant with the AOPAlliance’s specifications for aspect-oriented programming. We can implement the quintessential logging interceptor, which we will use to track message sending in our example, in only four steps. Step 1 – Implement the AOPAlliance’s  MethodInterceptor : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 public class MessageLogger implements MethodInterceptor {        @Inject      Logger logger;        @Override      public Object invoke(MethodInvocation invocation) throws Throwable {          Object[] objectArray = invocation.getArguments();          for (Object object : objectArray) {              logger.info( "Sending message: " + object.toString());          }          return invocation.proceed();      } } Step 2 – Define a plain java annotation : 1 2 3 4 @Retention (RetentionPolicy.RUNTIME) @Target (ElementType.METHOD)